Skip to content

Commit

Permalink
Run mypy on conftest.py files separately to avoid duplicate module error
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-ballarin committed Nov 26, 2023
1 parent 4611a9f commit 59c43dd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ jobs:
- name: Update mypy configuration
if: startsWith(matrix.backend, 'none') == true
run: |
sed -i 's@\[tool\.mypy\]@[tool.mypy]\nexclude = "(^rbnicsx/backends|^tests/unit/backends)"@g' pyproject.toml
echo "[[tool.mypy.overrides]]" >> pyproject.toml
echo 'module = "dolfinx"' >> pyproject.toml
echo "ignore_missing_imports = true" >> pyproject.toml
echo "[[tool.mypy.overrides]]" >> pyproject.toml
echo 'module = "dolfinx.*"' >> pyproject.toml
echo "ignore_missing_imports = true" >> pyproject.toml
sed -i 's@exclude = "(^\\\\.eggs|^build|^dist|conftest\\\\.py\$)"@exclude = "(^\\\\.eggs|^build|^dist|conftest\\\\.py\$|^rbnicsx/backends|^tests/unit/backends)"@g' pyproject.toml
- name: Run ruff on python files
run: |
python3 -m ruff .
Expand All @@ -103,7 +103,9 @@ jobs:
python3 -m isort --check --diff .
- name: Run mypy on python files
run: |
python3 -m mypy .
python3 -m mypy --exclude=conftest.py .
python3 -m mypy tests/unit/conftest.py
python3 -m mypy tutorials/conftest.py
- name: Run yamllint on workflows
run: |
python3 -m yamllint -d "{extends: default, rules: {document-start: {present: false}, line-length: disable, truthy: {check-keys: false}}}" .
Expand Down
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ known_first_party = ["rbnicsx"]
check_untyped_defs = true
disallow_any_unimported = true
disallow_untyped_defs = true
exclude = "(^\\.eggs|^build|^dist|conftest\\.py$)"
implicit_reexport = true
no_implicit_optional = true
pretty = true
Expand Down Expand Up @@ -128,6 +127,14 @@ ignore_missing_imports = true
module = "plum"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "scipy"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "scipy.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "slepc4py"
ignore_missing_imports = true
Expand Down
4 changes: 2 additions & 2 deletions rbnicsx/_backends/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def project_matrix_block( # type: ignore[no-any-unimported]
assert all(len(row) == len(a[0]) for row in a[1:]), "Matrix of forms has incorrect rows"
assert len(B[1]) == len(a[0])

M = [len(B_i) for B_i in B[0]]
N = [len(B_j) for B_j in B[1]]
M = [len(B_i) for B_i in B[0]] # type: ignore[arg-type]
N = [len(B_j) for B_j in B[1]] # type: ignore[arg-type]
assert A.size == (sum(M), sum(N))
with BlockMatSubMatrixWrapper(A, M, N) as A_wrapper:
for (i, j, A_ij) in A_wrapper:
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
def to_dense_matrix() -> typing.Callable[ # type: ignore[no-any-unimported]
[petsc4py.PETSc.Mat], np.typing.NDArray[petsc4py.PETSc.ScalarType]]:
"""Fixture that returns a function to convert the local part of a sparse PETSc Mat into a dense numpy ndarray."""
def _(mat: petsc4py.PETSc.Mat) -> np.typing.NDArray[petsc4py.PETSc.ScalarType]:
def _(mat: petsc4py.PETSc.Mat) -> np.typing.NDArray[petsc4py.PETSc.ScalarType]: # type: ignore[no-any-unimported]
"""Convert the local part of a sparse PETSc Mat into a dense numpy ndarray."""
ai, aj, av = mat.getValuesCSR()
return scipy.sparse.csr_matrix((av, aj, ai), shape=(mat.getLocalSize()[0], mat.getSize()[1])).toarray()
return scipy.sparse.csr_matrix( # type: ignore[no-any-return]
(av, aj, ai), shape=(mat.getLocalSize()[0], mat.getSize()[1])).toarray()
return _


Expand All @@ -37,7 +38,7 @@ def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption("--skip-backends", action="store_true", help="Skip tests which require backends to be installed")


def pytest_ignore_collect(
def pytest_ignore_collect( # type: ignore[no-any-unimported]
collection_path: pathlib.Path, path: _pytest.compat.LEGACY_PATH, config: pytest.Config
) -> bool:
"""Honor the --skip-backends option to skip tests which require backends to be installed."""
Expand Down

0 comments on commit 59c43dd

Please sign in to comment.