From 437dcd1dcd4111bb1bda6d714041d11f77273f09 Mon Sep 17 00:00:00 2001 From: Francesco Ballarin Date: Sat, 25 Nov 2023 19:41:48 +0100 Subject: [PATCH] Run mypy on conftest.py files separately to avoid duplicate module error --- .github/workflows/ci.yml | 9 +++++++-- pyproject.toml | 9 ++++++++- rbnicsx/_backends/projection.py | 4 ++-- tests/unit/conftest.py | 7 ++++--- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e99a48b..d86a6bc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -81,16 +81,19 @@ jobs: python3 -m pip install --check-build-dependencies --no-build-isolation --config-settings=build-dir="build" --config-settings=cmake.build-type="Debug" --verbose .[docs,lint,tests,tutorials] fi shell: bash + - name: Clean build files + run: | + git clean -xdf - 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 . @@ -99,7 +102,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}}}" . diff --git a/pyproject.toml b/pyproject.toml index 33ef572..0dc0278 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 @@ -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 diff --git a/rbnicsx/_backends/projection.py b/rbnicsx/_backends/projection.py index 9ad0cbe..000d82a 100644 --- a/rbnicsx/_backends/projection.py +++ b/rbnicsx/_backends/projection.py @@ -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: diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index d836eaa..22a0db1 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -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 _ @@ -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."""