Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
17 changes: 17 additions & 0 deletions python/Makefile
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I see the convenience of having this makefile, as it assumes:

  • that there is a python executable, not python3.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added optional py variable, following https://gitlab.com/petsc/petsc/-/blob/main/src/binding/petsc4py/makefile?ref_type=heads#L9.

Invoking with make py=3 lint-mypy will now use the python3 executable. If not provided defaults to python.

Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions python/demo/demo_pml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions python/demo/demo_pyamg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand All @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this kind of show that the type hint for dirichletbc is wrong?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem arises from the explicit dtype() invocation:

demo/demo_pyamg.py:87: error: "dtype[Any]" not callable  [operator]

I tried multiple fixes but I am not sure how to correctly create a scalar of a certain type in a typing compliant way. Arrays work fine, but constructor of a dtype seems to be a different case.


u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
x = ufl.SpatialCoordinate(mesh)
Expand All @@ -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)

Expand All @@ -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:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this then have a type, otherwise I don't see the point of changing it to npt.NDArray, could just be np.ndarray.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this should hold a type, but it needs the generic type of FunctionSpace - which will be introduced after this PR in #4131. So it is a first step into the right direction, dropping the list, but not quite perfect.

"""Create the elasticity (near)nulspace.

Args:
Expand Down
4 changes: 2 additions & 2 deletions python/demo/demo_scattering_boundary_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 2 additions & 3 deletions python/dolfinx/fem/bcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

from __future__ import annotations

import numbers
from collections.abc import Callable, Iterable

import numpy as np
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Loading