Skip to content

Commit

Permalink
All fixes for passing pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbraza committed Sep 10, 2024
1 parent e233339 commit d9a9ac0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 43 deletions.
8 changes: 4 additions & 4 deletions paperqa/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,16 @@ def configure_cli_logging(verbosity: int = 0) -> None:

rich_handler.setFormatter(logging.Formatter("%(message)s", datefmt="[%X]"))

module_logger = logging.getLogger(__name__.split(".")[0])
module_logger = logging.getLogger(__name__.split(".", maxsplit=1)[0])

if not any(isinstance(h, RichHandler) for h in module_logger.handlers):
module_logger.addHandler(rich_handler)

for logger_name, logger in logging.Logger.manager.loggerDict.items():
if isinstance(logger, logging.Logger) and (
for logger_name, logger_ in logging.Logger.manager.loggerDict.items():
if isinstance(logger_, logging.Logger) and (
log_level := verbosity_map.get(min(verbosity, 2), {}).get(logger_name)
):
logger.setLevel(log_level)
logger_.setLevel(log_level)

if verbosity > 0:
print(f"PaperQA version: {__version__}")
Expand Down
3 changes: 3 additions & 0 deletions paperqa/agents/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ def results(self) -> dict[str, dict[str, float]]:
return result


# pylint: disable=missing-kwoa


class AgentCallback(AsyncCallbackHandler):
"""
Callback handler used to monitor the agent, for debugging.
Expand Down
24 changes: 15 additions & 9 deletions paperqa/agents/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@

import anyio
from pydantic import BaseModel
from tantivy import Document, Index, Schema, SchemaBuilder, Searcher
from tantivy import ( # pylint: disable=no-name-in-module
Document,
Index,
Schema,
SchemaBuilder,
Searcher,
)
from tenacity import (
RetryError,
retry,
Expand All @@ -41,15 +47,15 @@ class AsyncRetryError(Exception):
class RobustEncoder(json.JSONEncoder):
"""JSON encoder that can handle UUID and set objects."""

def default(self, obj):
if isinstance(obj, UUID):
def default(self, o):
if isinstance(o, UUID):
# if the obj is uuid, we simply return the value of uuid
return str(obj)
if isinstance(obj, set):
return list(obj)
if isinstance(obj, os.PathLike):
return str(obj)
return json.JSONEncoder.default(self, obj)
return str(o)
if isinstance(o, set):
return list(o)
if isinstance(o, os.PathLike):
return str(o)
return json.JSONEncoder.default(self, o)


class SearchDocumentStorage(StrEnum):
Expand Down
13 changes: 4 additions & 9 deletions paperqa/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,14 @@

logger = logging.getLogger(__name__)

DEFAULT_CLIENTS: (
Collection[type[MetadataPostProcessor | MetadataProvider]]
| Sequence[Collection[type[MetadataPostProcessor | MetadataProvider]]]
) = {
DEFAULT_CLIENTS: Collection[type[MetadataPostProcessor | MetadataProvider]] = {
CrossrefProvider,
SemanticScholarProvider,
JournalQualityPostProcessor,
}

ALL_CLIENTS: (
Collection[type[MetadataPostProcessor | MetadataProvider]]
| Sequence[Collection[type[MetadataPostProcessor | MetadataProvider]]]
) = DEFAULT_CLIENTS | { # type: ignore[operator]
ALL_CLIENTS: Collection[type[MetadataPostProcessor | MetadataProvider]] = {
*DEFAULT_CLIENTS,
UnpaywallProvider,
}

Expand Down Expand Up @@ -63,7 +58,7 @@ def __repr__(self) -> str:


class DocMetadataClient:
def __init__(
def __init__( # pylint: disable=dangerous-default-value
self,
session: aiohttp.ClientSession | None = None,
clients: (
Expand Down
13 changes: 5 additions & 8 deletions paperqa/contrib/zotero.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ def __init__(
" from the text 'Your userID for use in API calls is [XXXXXX]'."
" Then, set the environment variable ZOTERO_USER_ID to this value."
)
else: # noqa: RET506
library_id = os.environ["ZOTERO_USER_ID"]
library_id = os.environ["ZOTERO_USER_ID"]

if api_key is None:
self.logger.info("Attempting to get ZOTERO_API_KEY from `os.environ`...")
Expand All @@ -97,8 +96,7 @@ def __init__(
" with access to your library."
" Then, set the environment variable ZOTERO_API_KEY to this value."
)
else: # noqa: RET506
api_key = os.environ["ZOTERO_API_KEY"]
api_key = os.environ["ZOTERO_API_KEY"]

self.logger.info(f"Using library ID: {library_id} with type: {library_type}.")

Expand All @@ -124,7 +122,7 @@ def get_pdf(self, item: dict) -> Path | None:
An item from `pyzotero`. Should have a `key` field, and also have an entry
`links->attachment->attachmentType == application/pdf`.
"""
if type(item) != dict: # noqa: E721
if not isinstance(item, dict):
raise TypeError("Pass the full item of the paper. The item must be a dict.")

pdf_key = _extract_pdf_key(item)
Expand Down Expand Up @@ -310,8 +308,7 @@ def _get_collection_id(self, collection_name: str) -> str:

def _get_citation_key(item: dict) -> str:
if (
"data" not in item
or "creators" not in item["data"]
"creators" not in item.get("data", {})
or len(item["data"]["creators"]) == 0
or "lastName" not in item["data"]["creators"][0]
or "title" not in item["data"]
Expand Down Expand Up @@ -341,7 +338,7 @@ def _extract_pdf_key(item: dict) -> str | None:

attachments = item["links"]["attachment"]

if type(attachments) != dict: # noqa: E721
if not isinstance(attachments, dict):
# Find first attachment with attachmentType == application/pdf:
for attachment in attachments:
# TODO: This assumes there's only one PDF attachment.
Expand Down
20 changes: 11 additions & 9 deletions paperqa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,16 @@ async def map_fxn_summary(
if not success:
score = extract_score(context)

c = Context(
context=context,
text=Text(
text=text.text,
name=text.name,
doc=text.doc.__class__(**text.doc.model_dump(exclude={"embedding"})),
return (
Context(
context=context,
text=Text(
text=text.text,
name=text.name,
doc=text.doc.__class__(**text.doc.model_dump(exclude={"embedding"})),
),
score=score, # pylint: disable=possibly-used-before-assignment
**extras,
),
score=score,
**extras,
llm_result,
)
return c, llm_result
4 changes: 2 additions & 2 deletions paperqa/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async def acomplete_iter(self, prompt: str) -> AsyncIterable[Chunk]: # noqa: AR
Only the last tuple will be non-zero.
"""
raise NotImplementedError
if False: # type: ignore[unreachable]
if False: # type: ignore[unreachable] # pylint: disable=using-constant-test
yield # Trick mypy: https://github.com/python/mypy/issues/5070#issuecomment-1050834495

async def achat(self, messages: Iterable[dict[str, str]]) -> Chunk:
Expand All @@ -157,7 +157,7 @@ async def achat_iter(
Only the last tuple will be non-zero.
"""
raise NotImplementedError
if False: # type: ignore[unreachable]
if False: # type: ignore[unreachable] # pylint: disable=using-constant-test
yield # Trick mypy: https://github.com/python/mypy/issues/5070#issuecomment-1050834495

def infer_llm_type(self) -> str:
Expand Down
4 changes: 2 additions & 2 deletions paperqa/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def available_for_inference(self) -> list["ParsingOptions"]:
def _get_parse_type(opt: ParsingOptions, config: "ParsingSettings") -> str:
if opt == ParsingOptions.PAPERQA_DEFAULT:
return config.parser_version_string
assert_never()
assert_never(opt)


class ChunkingOptions(StrEnum):
Expand Down Expand Up @@ -117,7 +117,7 @@ def chunk_type(self, chunking_selection: ChunkingOptions | None = None) -> str:
f"{self.parser_version_string}|{chunking_selection.value}"
f"|tokens={self.chunk_size}|overlap={self.overlap}"
)
assert_never()
assert_never(chunking_selection)

@property
def parser_version_string(self) -> str:
Expand Down

0 comments on commit d9a9ac0

Please sign in to comment.