diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index dba743d57e1..80e0782c470 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -129,24 +129,15 @@ jobs: - name: Build Python interface run: | - pip install --check-build-dependencies --no-build-isolation --config-settings=cmake.build-type="Developer" 'python/[test]' + pip install --check-build-dependencies --no-build-isolation --config-settings=cmake.build-type="Developer" 'python/[test,mypy]' python -c "from mpi4py import MPI; import dolfinx; assert not dolfinx.has_petsc; assert not dolfinx.has_petsc4py; assert dolfinx.has_superlu_dist" - name: Run mypy - working-directory: python - run: | - pip install mypy types-cffi scipy-stubs - mypy -p dolfinx - - - name: Run mypy # Use a venv to avoid NumPy upgrades that are incompatible with numba - working-directory: python run: | - python -m venv mypy-env - . ./mypy-env/bin/activate - pip install mypy types-cffi scipy-stubs - mypy -p dolfinx + mypy --config-file python/pyproject.toml -p dolfinx # package mode, type checks installed dolfinx (working around name collision in python/ directory) + cd python/ mypy test - mypy demo + #mypy demo # TODO: Enable this in future - name: Install gmsh and pyvista (and dependencies) run: | diff --git a/python/README.md b/python/README.md index fd2bcd88749..1d4830453c2 100644 --- a/python/README.md +++ b/python/README.md @@ -6,15 +6,29 @@ Below is guidance for building the DOLFINx Python interface. 2. Ensure the Python interface build requirements are installed: - pip install scikit-build-core - python -m scikit_build_core.build requires | python -c "import sys, json; print(' '.join(json.load(sys.stdin)))" | xargs pip install + pip install scikit-build-core + python -m scikit_build_core.build requires | python -c "import sys, json; print(' '.join(json.load(sys.stdin)))" | xargs pip install 3. Build DOLFINx Python interface: - pip install --check-build-dependencies --no-build-isolation . + pip install --check-build-dependencies --no-build-isolation . To build in Developer and editable mode for development: pip -v install --check-build-dependencies --config-settings=build-dir="build" --config-settings=cmake.build-type="Developer" --config-settings=install.strip=false --no-build-isolation -e . -Note that our Developer mode is significantly stricter than CMake's default Debug mode. +Note that Developer mode is significantly stricter than CMake's default Debug mode. + +# Type checking with mypy + +1. Install DOLFINx Python with the `[mypy]` optional dependencies set, e.g.: + + pip install .[mypy] + +2. Check with mypy, e.g.: + + mypy --config-file pyproject.toml -p dolfinx + + The `--config-file pyproject.toml` is mandatory to run mypy with the correct options. + The `-p` flag checks the built/installed package `dolfinx`, containing the C++ + bindings and Python interface. diff --git a/python/demo/demo_pyamg.py b/python/demo/demo_pyamg.py index 6708a1ff6ed..0da47e25ed4 100644 --- a/python/demo/demo_pyamg.py +++ b/python/demo/demo_pyamg.py @@ -63,7 +63,7 @@ def poisson_problem(dtype: npt.DTypeLike, solver_type: str) -> None: solver_type: pyamg solver type, either "ruge_stuben" or "smoothed_aggregation" """ - real_type = np.real(dtype(0)).dtype # type: ignore + real_type = np.real(np.zeros(0, dtype=dtype)).dtype mesh = create_box( comm=MPI.COMM_WORLD, points=[(0.0, 0.0, 0.0), (3.0, 2.0, 1.0)], @@ -84,7 +84,7 @@ def poisson_problem(dtype: npt.DTypeLike, solver_type: str) -> None: dofs = locate_dofs_topological(V=V, entity_dim=fdim, entities=facets) - bc = dirichletbc(value=dtype(0), dofs=dofs, V=V) # type: ignore + bc = dirichletbc(value=dtype(0.0), dofs=dofs, V=V) # type: ignore u, v = ufl.TrialFunction(V), ufl.TestFunction(V) x = ufl.SpatialCoordinate(mesh) diff --git a/python/pyproject.toml b/python/pyproject.toml index 304fff94481..3d051fa13cf 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -50,6 +50,7 @@ docs = [ "fenics-dolfinx[demo]", ] lint = ["ruff"] +mypy = ["mypy", "types-cffi", "types-setuptools", "scipy-stubs"] optional = ["numba"] petsc4py = ["petsc4py"] test = [ @@ -61,12 +62,11 @@ test = [ "fenics-dolfinx[optional]", ] ci = [ - "mypy", "pytest-xdist", - "types-setuptools", "fenics-dolfinx[build]", "fenics-dolfinx[docs]", "fenics-dolfinx[lint]", + "fenics-dolfinx[mypy]", "fenics-dolfinx[optional]", "fenics-dolfinx[test]", ] @@ -98,7 +98,6 @@ warn_unused_ignores = false show_error_codes = true ignore_missing_imports = true - [tool.ruff] line-length = 100 indent-width = 4