Skip to content

Extend mypy checks to test/#4135

Merged
jhale merged 23 commits into
mainfrom
schnellerhase/mypy-demo/test
May 27, 2026
Merged

Extend mypy checks to test/#4135
jhale merged 23 commits into
mainfrom
schnellerhase/mypy-demo/test

Conversation

@schnellerhase
Copy link
Copy Markdown
Contributor

@schnellerhase schnellerhase commented Mar 20, 2026

TODO in future

  • Automatic stub generation
  • Enable checks on python/demo/

Edited by @jhale

@schnellerhase schnellerhase force-pushed the schnellerhase/mypy-demo/test branch from 6f80a70 to 9b9ae01 Compare March 20, 2026 14:39
@schnellerhase schnellerhase self-assigned this Mar 20, 2026
@schnellerhase schnellerhase added ci Continuous Integration type-hints labels Mar 20, 2026
@schnellerhase schnellerhase force-pushed the schnellerhase/mypy-demo/test branch from c3e3fca to adb6872 Compare March 20, 2026 15:11
@schnellerhase schnellerhase mentioned this pull request Mar 24, 2026
13 tasks
Comment thread python/Makefile Outdated
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.

Comment thread python/demo/demo_pyamg.py

# +
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.

Comment thread python/demo/demo_pyamg.py
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.

@jhale
Copy link
Copy Markdown
Member

jhale commented Apr 27, 2026

This can be merged once the Makefile is removed. Please add instructions to the python/README.md.

@schnellerhase
Copy link
Copy Markdown
Contributor Author

This can be merged once the Makefile is removed. Please add instructions to the python/README.md.

#4154 addresses this.

I do think we need a way to allow for local replication of CI failures during development. If it is not makefiles then we should think about another way to do this.

@jhale
Copy link
Copy Markdown
Member

jhale commented Apr 29, 2026

Please put developer documentation in python/README.md

@schnellerhase schnellerhase marked this pull request as draft April 30, 2026 15:47
finsberg added a commit to scientificcomputing/dolfinx-adjoint that referenced this pull request May 7, 2026
@jhale jhale added this to the 0.11.0 milestone May 13, 2026
@schnellerhase
Copy link
Copy Markdown
Contributor Author

Setup for #4216

# Note: mypy is currenlty on CI tested without petsc4py!

PYTHON = python$(py)

lint-mypy:
	$(PYTHON) -m venv dolfinx-venv-mypy --system-site-packages
	. dolfinx-venv-mypy/bin/activate && pip install mypy types-cffi scipy-stubs scikit-build-core nanobind
	. dolfinx-venv-mypy/bin/activate && pip install git+https://github.com/fenics/ufl.git
	. dolfinx-venv-mypy/bin/activate && pip install git+https://github.com/fenics/basix.git --config-settings=build-dir=build/basix
	. dolfinx-venv-mypy/bin/activate && pip install git+https://github.com/fenics/ffcx.git
	. dolfinx-venv-mypy/bin/activate && pip install . --no-build-isolation --config-settings=build-dir=build/dolfinx -Ccmake.build-type="Developer"
	. 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

@jhale jhale marked this pull request as ready for review May 26, 2026 11:36
@jhale jhale changed the title Extend mypy checks to test/ and demo/ Extend mypy checks to test/ May 26, 2026
@jhale
Copy link
Copy Markdown
Member

jhale commented May 27, 2026

Remaining Spack error is pyamg problem, going to merge.

@jhale jhale enabled auto-merge May 27, 2026 06:15
Comment thread .github/workflows/ccpp.yml Outdated
@jhale jhale disabled auto-merge May 27, 2026 08:09
@jhale jhale enabled auto-merge May 27, 2026 08:09
@jhale jhale dismissed jorgensd’s stale review May 27, 2026 10:33

Makefile removed, it is not necessary, instructions added to README.md

@jhale jhale added this pull request to the merge queue May 27, 2026
Merged via the queue into main with commit 89d37fc May 27, 2026
19 of 20 checks passed
@jhale jhale deleted the schnellerhase/mypy-demo/test branch May 27, 2026 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continuous Integration type-hints

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants