Skip to content

Commit

Permalink
mypy config update (#237)
Browse files Browse the repository at this point in the history
* Ran pre-commit autoupdate for newer mypy and added mypy config

* Removed all prior type ignore comments

* Removed redundant cast

* Ran mypy_clean_slate to add type ignores
  • Loading branch information
jamesbraza authored Mar 2, 2024
1 parent c418b9d commit a16f5d9
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 56 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ repos:
- id: black
language_version: python3
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.3.0"
rev: v1.8.0
hooks:
- id: mypy
args: [--pretty, --ignore-missing-imports]
additional_dependencies: [types-requests]
additional_dependencies: [types-requests, types-setuptools]
- repo: https://github.com/PyCQA/isort
rev: "5.12.0"
hooks:
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pyzotero
python-dotenv
pymupdf
build
types-setuptools
types-requests
langchain_openai
langchain_community
Expand Down
2 changes: 1 addition & 1 deletion paperqa/contrib/zotero.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def get_pdf(self, item: dict) -> Union[Path, None]:
if pdf_key is None:
return None

pdf_path: Path = Path(self.storage / (pdf_key + ".pdf")) # type: ignore
pdf_path: Path = Path(self.storage / (pdf_key + ".pdf")) # type: ignore[operator]

if not pdf_path.exists():
pdf_path.parent.mkdir(parents=True, exist_ok=True)
Expand Down
10 changes: 5 additions & 5 deletions paperqa/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ async def aadd(
# get first name and year from citation
match = re.search(r"([A-Z][a-z]+)", citation)
if match is not None:
author = match.group(1) # type: ignore
author = match.group(1)
else:
# panicking - no word??
raise ValueError(
Expand All @@ -433,7 +433,7 @@ async def aadd(
year = ""
match = re.search(r"(\d{4})", citation)
if match is not None:
year = match.group(1) # type: ignore
year = match.group(1)
docname = f"{author}{year}"
docname = self._get_unique_name(docname)
doc = Doc(docname=docname, citation=citation, dockey=dockey)
Expand Down Expand Up @@ -619,7 +619,7 @@ async def aget_evidence(
) -> Answer:
if len(self.docs) == 0 and self.docs_index is None:
# do we have no docs?
return answer
return answer # type: ignore[unreachable]
self._build_texts_index(keys=answer.dockey_filter)
_k = k
if answer.dockey_filter is not None:
Expand Down Expand Up @@ -663,13 +663,13 @@ async def process(match):
score = 5
else:
if self.prompts.json_summary:
summary_chain = self.summary_llm_model.make_chain(
summary_chain = self.summary_llm_model.make_chain( # type: ignore[union-attr]
client=self._client,
prompt=self.prompts.summary_json,
system_prompt=self.prompts.summary_json_system,
)
else:
summary_chain = self.summary_llm_model.make_chain(
summary_chain = self.summary_llm_model.make_chain( # type: ignore[union-attr]
client=self._client,
prompt=self.prompts.summary,
system_prompt=self.prompts.system,
Expand Down
18 changes: 9 additions & 9 deletions paperqa/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ async def execute(
f for f in callbacks if not is_coroutine_callable(f)
]
async_callbacks = [f for f in callbacks if is_coroutine_callable(f)]
completion = self.achat_iter(client, messages) # type: ignore
completion = self.achat_iter(client, messages)
text_result = []
async for chunk in completion: # type: ignore
async for chunk in completion: # type: ignore[attr-defined]
if chunk:
if result.seconds_to_first_token == 0:
result.seconds_to_first_token = (
Expand Down Expand Up @@ -250,12 +250,12 @@ async def execute(
]
async_callbacks = [f for f in callbacks if is_coroutine_callable(f)]

completion = self.acomplete_iter( # type: ignore
completion = self.acomplete_iter(
client,
formatted_prompt,
)
text_result = []
async for chunk in completion: # type: ignore
async for chunk in completion: # type: ignore[attr-defined]
if chunk:
if result.seconds_to_first_token == 0:
result.seconds_to_first_token = (
Expand Down Expand Up @@ -289,7 +289,7 @@ def _check_client(self, client: Any) -> AsyncOpenAI:
raise ValueError(
f"Your client is not a required AsyncOpenAI client. It is a {type(client)}"
)
return cast(AsyncOpenAI, client)
return client

@model_validator(mode="after")
@classmethod
Expand Down Expand Up @@ -323,14 +323,14 @@ async def acomplete_iter(self, client: Any, prompt: str) -> Any:
async def achat(self, client: Any, messages: list[dict[str, str]]) -> str:
aclient = self._check_client(client)
completion = await aclient.chat.completions.create(
messages=messages, **process_llm_config(self.config) # type: ignore
messages=messages, **process_llm_config(self.config)
)
return completion.choices[0].message.content or ""

async def achat_iter(self, client: Any, messages: list[dict[str, str]]) -> Any:
aclient = self._check_client(client)
completion = await aclient.chat.completions.create(
messages=messages, **process_llm_config(self.config), stream=True # type: ignore
messages=messages, **process_llm_config(self.config), stream=True
)
async for chunk in cast(AsyncGenerator, completion):
yield chunk.choices[0].delta.content
Expand Down Expand Up @@ -635,13 +635,13 @@ def add_texts_and_embeddings(self, texts: Sequence[Embeddable]) -> None:
else:
raise ValueError("Only embeddings of type Text are supported")
if self._store is None:
self._store = self._store_builder( # type: ignore
self._store = self._store_builder(
vec_store_text_and_embeddings,
texts,
)
if self._store is None or not hasattr(self._store, "add_embeddings"):
raise ValueError("store_builder did not return a valid vectorstore")
self._store.add_embeddings( # type: ignore
self._store.add_embeddings(
vec_store_text_and_embeddings,
metadatas=texts,
)
Expand Down
2 changes: 1 addition & 1 deletion paperqa/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def remove_computed(cls, data: Any) -> Any:
data.pop("used_contexts", None)
return data

@computed_field # type: ignore
@computed_field # type: ignore[misc]
@property
def used_contexts(self) -> set[str]:
"""Return the used contexts."""
Expand Down
49 changes: 49 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[tool.mypy]
# Type-checks the interior of functions without type annotations.
check_untyped_defs = true
# Allows enabling one or multiple error codes globally. Note: This option will
# override disabled error codes from the disable_error_code option.
enable_error_code = [
"ignore-without-code",
"mutable-override",
"redundant-cast",
"redundant-expr",
"redundant-self",
"truthy-bool",
"truthy-iterable",
"unreachable",
"unused-awaitable",
"unused-ignore",
]
# Shows a short summary line after error messages.
error_summary = false
# Use visually nicer output in error messages: use soft word wrap, show source
# code snippets, and show error location markers.
pretty = true
# Shows column numbers in error messages.
show_column_numbers = true
# Shows error codes in error messages.
# SEE: https://mypy.readthedocs.io/en/stable/error_codes.html#error-codes
show_error_codes = true
# Prefixes each error with the relevant context.
show_error_context = true
# Warns about casting an expression to its inferred type.
warn_redundant_casts = true
# Shows a warning when encountering any code inferred to be unreachable or
# redundant after performing type analysis.
warn_unreachable = true
# Warns about per-module sections in the config file that do not match any
# files processed when invoking mypy.
warn_unused_configs = true
# Warns about unneeded `# type: ignore` comments.
warn_unused_ignores = true

[[tool.mypy.overrides]]
# Suppresses error messages about imports that cannot be resolved.
ignore_missing_imports = true
# Per-module configuration options
module = [
"fitz",
"pyzotero", # SEE: https://github.com/urschrei/pyzotero/issues/110
"sentence_transformers", # SEE: https://github.com/UKPLab/sentence-transformers/issues/1723
]
Loading

0 comments on commit a16f5d9

Please sign in to comment.