diff --git a/.github/workflows/ccpp.yml b/.github/workflows/ccpp.yml index c009dfd4f0..21fe82f6f9 100644 --- a/.github/workflows/ccpp.yml +++ b/.github/workflows/ccpp.yml @@ -132,15 +132,9 @@ jobs: pip install --check-build-dependencies --no-build-isolation --config-settings=cmake.build-type="Developer" 'python/[test]' 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 # Use a venv to avoid NumPy upgrades that are incompatible with numba + - name: Run mypy working-directory: python - run: | - python -m venv mypy-env - . ./mypy-env/bin/activate - pip install mypy types-cffi scipy-stubs - mypy -p dolfinx - # mypy test - # mypy demo + run: make lint-mypy - name: Install gmsh and pyvista (and dependencies) run: | diff --git a/python/Makefile b/python/Makefile new file mode 100644 index 0000000000..b4ec8cb73e --- /dev/null +++ b/python/Makefile @@ -0,0 +1,17 @@ +# Note: mypy is currenlty on CI tested without petsc4py! + +PYTHON = python$(py) + +lint-mypy: + $(PYTHON) -m venv dolfinx-venv-mypy + . dolfinx-venv-mypy/bin/activate && pip install mypy types-cffi scipy-stubs + . dolfinx-venv-mypy/bin/activate && mypy -p dolfinx + . dolfinx-venv-mypy/bin/activate && mypy test + . dolfinx-venv-mypy/bin/activate && mypy demo + +lint-mypy-clean: + rm -rf dolfinx-venv-mypy + +lint: lint-mypy + +clean: lint-mypy-clean diff --git a/python/demo/demo_pml.py b/python/demo/demo_pml.py index 27d4fb9464..c5634bf72d 100644 --- a/python/demo/demo_pml.py +++ b/python/demo/demo_pml.py @@ -214,8 +214,8 @@ def generate_mesh_wire( def compute_a(nu: int, m: complex, alpha: float) -> float: """Compute the Mie coefficient a_nu for a cylinder.""" - J_nu_alpha = jv(nu, alpha) - J_nu_malpha = jv(nu, m * alpha) + J_nu_alpha = jv(nu, alpha) # type: ignore + J_nu_malpha = jv(nu, m * alpha) # type: ignore J_nu_alpha_p = jvp(nu, alpha, 1) J_nu_malpha_p = jvp(nu, m * alpha, 1) diff --git a/python/demo/demo_pyamg.py b/python/demo/demo_pyamg.py index bf6e4e68c8..0da47e25ed 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 + 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) + bc = dirichletbc(value=dtype(0.0), dofs=dofs, V=V) # type: ignore u, v = ufl.TrialFunction(V), ufl.TestFunction(V) x = ufl.SpatialCoordinate(mesh) @@ -110,14 +110,14 @@ def poisson_problem(dtype: npt.DTypeLike, solver_type: str) -> None: print(ml) # Solve linear systems - print(f"\nSolve Poisson equation: {dtype.__name__}") + print(f"\nSolve Poisson equation: {dtype!s}") res: list[float] = [] tol = 1e-10 if real_type == np.float64 else 1e-6 uh.x.array[:] = ml.solve(b.array, tol=tol, residuals=res, accel="cg") for i, q in enumerate(res): print(f"Convergence history: iteration {i}, residual= {q}") - with io.XDMFFile(mesh.comm, f"out_pyamg/poisson_{dtype.__name__}.xdmf", "w") as file: + with io.XDMFFile(mesh.comm, f"out_pyamg/poisson_{dtype!s}.xdmf", "w") as file: file.write_mesh(mesh) file.write_function(uh) @@ -126,7 +126,7 @@ def poisson_problem(dtype: npt.DTypeLike, solver_type: str) -> None: # + -def nullspace_elasticty(Q: fem.FunctionSpace) -> list[np.ndarray]: +def nullspace_elasticty(Q: fem.FunctionSpace) -> npt.NDArray: """Create the elasticity (near)nulspace. Args: diff --git a/python/demo/demo_scattering_boundary_conditions.py b/python/demo/demo_scattering_boundary_conditions.py index f3bd735f6d..3c40b83574 100644 --- a/python/demo/demo_scattering_boundary_conditions.py +++ b/python/demo/demo_scattering_boundary_conditions.py @@ -220,8 +220,8 @@ def generate_mesh_wire( # + def compute_a(nu: int, m: complex, alpha: float) -> float: """Compute the a_nu coefficient.""" - J_nu_alpha = jv(nu, alpha) - J_nu_malpha = jv(nu, m * alpha) + J_nu_alpha = jv(nu, alpha) # type: ignore + J_nu_malpha = jv(nu, m * alpha) # type: ignore J_nu_alpha_p = jvp(nu, alpha, 1) J_nu_malpha_p = jvp(nu, m * alpha, 1) diff --git a/python/dolfinx/fem/bcs.py b/python/dolfinx/fem/bcs.py index 10dce42ba3..d4abcaa8a7 100644 --- a/python/dolfinx/fem/bcs.py +++ b/python/dolfinx/fem/bcs.py @@ -12,7 +12,6 @@ from __future__ import annotations -import numbers from collections.abc import Callable, Iterable import numpy as np @@ -172,7 +171,7 @@ def dof_indices(self) -> tuple[npt.NDArray[np.int32], int]: def dirichletbc( - value: Function | Constant | np.ndarray, + value: Function | Constant | np.ndarray | float | complex, dofs: npt.NDArray[np.int32], V: dolfinx.fem.FunctionSpace | None = None, ) -> DirichletBC: @@ -193,7 +192,7 @@ def dirichletbc( A representation of the boundary condition for modifying linear systems. """ - if isinstance(value, numbers.Number): + if isinstance(value, float | complex): value = np.asarray(value) try: