Skip to content

Commit

Permalink
style: make mypy check strict
Browse files Browse the repository at this point in the history
  • Loading branch information
dvp committed Sep 18, 2024
1 parent 2cd391c commit 1748d6f
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 166 deletions.
287 changes: 145 additions & 142 deletions poetry.lock

Large diffs are not rendered by default.

18 changes: 6 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -190,20 +190,14 @@ sort = "Cover"
# https://dev.to/tusharsadhwani/the-comprehensive-guide-to-mypy-561m
[tool.mypy]
python_version = "3.12"
# strict = true # TODO dvp: uncomment this to get strict control
strict = true
follow_imports = "silent"
# namespace_packages = true
warn_return_any = true
warn_unused_configs = true
show_error_codes = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
disable_error_code = ["annotation-unchecked"]
show_error_context = true
error_summary = true
warn_redundant_casts = true
warn_unreachable = true
warn_unused_ignores = true
disallow_untyped_defs = true
files = "src/mapstp/**/*.py"
exclude = '''(?x)(
data/.*\.py$ # python files given as data (for SpaceClaim)
| cli/runner\.py$ # mypy doesn't like decorators
)'''
plugins = ["numpy.typing.mypy_plugin"]

[[tool.mypy.overrides]]
Expand Down
12 changes: 7 additions & 5 deletions src/mapstp/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
if TYPE_CHECKING:
import re

from collections.abc import Iterable, Iterator
from collections.abc import Generator, Iterable, Iterator
from pathlib import Path

from mapstp.utils.io import MCNPSections
Expand Down Expand Up @@ -60,7 +60,7 @@ def extract_number_and_density(cell: int, path_info: pd.DataFrame) -> tuple[int,
"""
material_number, density, factor = path_info.loc[cell][["material_number", "density", "factor"]]

def _validate(*, res: bool, msg: str):
def _validate(*, res: bool, msg: str) -> None:
if not res:
raise PathInfoError(msg, cell, path_info)

Expand Down Expand Up @@ -137,12 +137,14 @@ def is_current_cell_specified(self: _Merger) -> bool:
"""
return self.current_cell in self.path_info.index

def _format_volume_and_comment(self: _Merger) -> Iterator[str]:
def _format_volume_and_comment(self: _Merger) -> Generator[str, None, None]:
rec = self.path_info.loc[self.current_cell][["volume", "path"]]
yield f" vol={rec.volume}"
yield f" $ stp: {rec.path}"

def _on_cell_start(self: _Merger, line: str, match: re.Match) -> Iterator[str]:
def _on_cell_start(
self: _Merger, line: str, match: re.Match[str]
) -> Generator[str, None, None]:
if self.first_cell:
line = self._on_next_cell(line, match)
self.first_cell = False
Expand All @@ -152,7 +154,7 @@ def _on_cell_start(self: _Merger, line: str, match: re.Match) -> Iterator[str]:
line = self._on_next_cell(line, match)
yield line

def _on_next_cell(self: _Merger, line: str, match: re.Match) -> str:
def _on_next_cell(self: _Merger, line: str, match: re.Match[str]) -> str:
self.current_cell = int(match["number"])
if self.is_current_cell_specified() and int(match["material"]) == 0:
line = _correct_first_line(
Expand Down
2 changes: 1 addition & 1 deletion src/mapstp/stp_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def parse(inp: TextIO) -> ParseResult:


def _process_line(
match: re.Match,
match: re.Match[str],
line: str,
links: list[Link],
products: list[Product],
Expand Down
6 changes: 5 additions & 1 deletion src/mapstp/workflow_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
if TYPE_CHECKING:
import sqlite3 as sq

from collections.abc import Generator


def save_meta_info_from_paths(con: sq.Connection, materials_index: str) -> None:
"""Store information from materials index corresponding to cells paths to SQL database.
Expand All @@ -39,7 +41,9 @@ def save_meta_info_from_paths(con: sq.Connection, materials_index: str) -> None:
""",
).fetchall()

def _iter_records():
def _iter_records() -> (
Generator[tuple[int | None, float | None, float | None, str | None, int | None], None, None]
):
for cell, path in records:
meta_info = extract_meta_info_from_path(path)
if meta_info.mnemonic:
Expand Down
11 changes: 6 additions & 5 deletions tools/reset-pyenv
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ reset() {
pyenv virtualenv-delete -f "$env" && echo "Removed environment $env"
pyenv virtualenv "$last_python" "$env" && echo "Created environment $env"
pyenv local "$env"
eval "$(pyenv init -)"
pyenv rehash
pyenv shell "$env"
pyenv activate "$env"
python --version
python -m pip install --upgrade pip setuptools wheel
poetry config --local virtualenvs.create false
poetry env info
poetry lock --no-update
poetry install
python -c "import ${pkg}; print(${pkg}.__version__)" && success "Environment $env is reset"
pyenv shell --unset
pyenv deactivate
pyenv local "$env" "3.11.10" "3.10.15" "3.9.20"
pyenv rehash
pyenv activate "$env"
python -c "import ${pkg}; print(${pkg}.__version__)" && success "Environment $env is reset"
}

reset "$@"
Expand Down

0 comments on commit 1748d6f

Please sign in to comment.