Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errors and add mypy to CI #2190

Merged
merged 68 commits into from
May 25, 2022
Merged

Fix errors and add mypy to CI #2190

merged 68 commits into from
May 25, 2022

Conversation

jhale
Copy link
Member

@jhale jhale commented May 20, 2022

Some of the MetaClass stuff seems to be tricky to type, although my Python typing knowledge is admittedly limited.

@jhale
Copy link
Member Author

jhale commented May 20, 2022

Would close #1988 once merged.

@jhale jhale marked this pull request as draft May 20, 2022 07:32
@jhale jhale requested a review from garth-wells May 20, 2022 08:12
@jhale
Copy link
Member Author

jhale commented May 20, 2022

The type checkers do not like constructs like this where we use a raise of TypeError to detect a C++ or Python object.

https://github.com/FEniCS/dolfinx/blob/main/python/dolfinx/fem/bcs.py#L59

dolfinx/fem/bcs.py:62: error: "Iterable[Union[Any, FunctionSpace]]" has no attribute "_cpp_object"

I would propose changing to e.g. isinstance?

@jhale jhale requested a review from mscroggs May 20, 2022 08:13
@jhale
Copy link
Member Author

jhale commented May 20, 2022

I've made some progress:

  • Number of type errors in DOLFINx Python API is now down to 32 errors in 3 files. Most are related to our use of MetaClasses or Single Dispatch, which are obviously tricky areas with respect to typing. I would need some help fixing these.
  • mypy enabled non-blocking in CI.
  • Install py.typed files into DOLFINx Python so that user's IDEs actually pickup on type hints.
  • There are still many easy mistakes to fix in our demos, or, our DOLFINx API typing. This would need discussion in terms of a) do we want to open up what can be passed to the API? or b) Happy with the API typing, and fix the demos.

DOLFINx Python errors:

dolfinx/fem/assemble.py:170: error: Dispatch type "ndarray[Any, Any]" must be subtype of fallback function first argument "FormMetaClass"
dolfinx/fem/assemble.py:230: error: Too many arguments for "assemble_matrix"
dolfinx/fem/assemble.py:230: error: Argument 1 to "assemble_matrix" has incompatible type "MatrixCSRMetaClass"; expected "FormMetaClass"
dolfinx/fem/assemble.py:230: error: Argument 2 to "assemble_matrix" has incompatible type "FormMetaClass"; expected "Optional[List[DirichletBCMetaClass]]"
dolfinx/fem/assemble.py:230: error: Argument 3 to "assemble_matrix" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
dolfinx/fem/assemble.py:235: error: Dispatch type "MatrixCSRMetaClass" must be subtype of fallback function first argument "FormMetaClass"
dolfinx/fem/forms.py:168: error: Missing return statement
dolfinx/fem/forms.py:185: error: Item "Iterable[FormMetaClass]" of "Union[FormMetaClass, Iterable[FormMetaClass]]" has no attribute "function_spaces"
dolfinx/fem/petsc.py:181: error: Too many arguments for "assemble_vector"
dolfinx/fem/petsc.py:203: error: Too many arguments for "assemble_vector"
dolfinx/fem/petsc.py:220: error: Too many arguments for "assemble_vector_nest"
dolfinx/fem/petsc.py:234: error: Too many arguments for "assemble_vector"
dolfinx/fem/petsc.py:256: error: Too many arguments for "assemble_vector_block"
dolfinx/fem/petsc.py:256: error: Argument 2 to "assemble_vector_block" has incompatible type "List[FormMetaClass]"; expected "List[List[FormMetaClass]]"
dolfinx/fem/petsc.py:256: error: Argument 3 to "assemble_vector_block" has incompatible type "List[List[FormMetaClass]]"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:256: error: Argument 5 to "assemble_vector_block" has incompatible type "Optional[Any]"; expected "float"
dolfinx/fem/petsc.py:291: error: Argument 1 to "bcs_by_block" has incompatible type "Iterable[Optional[FunctionSpace]]"; expected "Iterable[FunctionSpace]"
dolfinx/fem/petsc.py:302: error: Argument 1 to "bcs_by_block" has incompatible type "Iterable[Optional[FunctionSpace]]"; expected "Iterable[FunctionSpace]"
dolfinx/fem/petsc.py:325: error: Too many arguments for "assemble_matrix"
dolfinx/fem/petsc.py:325: error: Argument 2 to "assemble_matrix" has incompatible type "FormMetaClass"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:325: error: Argument 3 to "assemble_matrix" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
dolfinx/fem/petsc.py:354: error: Argument 2 to "assemble_matrix_nest" has incompatible type "List[List[FormMetaClass]]"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:371: error: Too many arguments for "assemble_matrix"
dolfinx/fem/petsc.py:371: error: Argument 2 to "assemble_matrix" has incompatible type "FormMetaClass"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:371: error: Argument 3 to "assemble_matrix" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
dolfinx/fem/petsc.py:391: error: Too many arguments for "assemble_matrix_block"
dolfinx/fem/petsc.py:391: error: Argument 2 to "assemble_matrix_block" has incompatible type "List[List[FormMetaClass]]"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:391: error: Argument 3 to "assemble_matrix_block" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
dolfinx/fem/petsc.py:461: error: Argument 1 to "bcs_by_block" has incompatible type "Iterable[Optional[FunctionSpace]]"; expected "Iterable[FunctionSpace]"
dolfinx/fem/petsc.py:468: error: Argument 3 to "apply_lifting" has incompatible type "Iterable[Iterable[DirichletBCMetaClass]]"; expected "List[List[DirichletBCMetaClass]]"
dolfinx/fem/petsc.py:564: error: "assemble_matrix" gets multiple values for keyword argument "bcs"
dolfinx/fem/petsc.py:697: error: Argument 3 to "assemble_matrix" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
Found 32 errors in 3 files (checked 21 source files)

Demo errors:

dolfinx/fem/assemble.py:170: error: Dispatch type "ndarray[Any, Any]" must be subtype of fallback function first argument "FormMetaClass"
dolfinx/fem/assemble.py:230: error: Too many arguments for "assemble_matrix"
dolfinx/fem/assemble.py:230: error: Argument 1 to "assemble_matrix" has incompatible type "MatrixCSRMetaClass"; expected "FormMetaClass"
dolfinx/fem/assemble.py:230: error: Argument 2 to "assemble_matrix" has incompatible type "FormMetaClass"; expected "Optional[List[DirichletBCMetaClass]]"
dolfinx/fem/assemble.py:230: error: Argument 3 to "assemble_matrix" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
dolfinx/fem/assemble.py:235: error: Dispatch type "MatrixCSRMetaClass" must be subtype of fallback function first argument "FormMetaClass"
dolfinx/fem/forms.py:168: error: Missing return statement
dolfinx/fem/forms.py:185: error: Item "Iterable[FormMetaClass]" of "Union[FormMetaClass, Iterable[FormMetaClass]]" has no attribute "function_spaces"
dolfinx/fem/petsc.py:181: error: Too many arguments for "assemble_vector"
dolfinx/fem/petsc.py:203: error: Too many arguments for "assemble_vector"
dolfinx/fem/petsc.py:220: error: Too many arguments for "assemble_vector_nest"
dolfinx/fem/petsc.py:234: error: Too many arguments for "assemble_vector"
dolfinx/fem/petsc.py:256: error: Too many arguments for "assemble_vector_block"
dolfinx/fem/petsc.py:256: error: Argument 2 to "assemble_vector_block" has incompatible type "List[FormMetaClass]"; expected "List[List[FormMetaClass]]"
dolfinx/fem/petsc.py:256: error: Argument 3 to "assemble_vector_block" has incompatible type "List[List[FormMetaClass]]"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:256: error: Argument 5 to "assemble_vector_block" has incompatible type "Optional[Any]"; expected "float"
dolfinx/fem/petsc.py:291: error: Argument 1 to "bcs_by_block" has incompatible type "Iterable[Optional[FunctionSpace]]"; expected "Iterable[FunctionSpace]"
dolfinx/fem/petsc.py:302: error: Argument 1 to "bcs_by_block" has incompatible type "Iterable[Optional[FunctionSpace]]"; expected "Iterable[FunctionSpace]"
dolfinx/fem/petsc.py:325: error: Too many arguments for "assemble_matrix"
dolfinx/fem/petsc.py:325: error: Argument 2 to "assemble_matrix" has incompatible type "FormMetaClass"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:325: error: Argument 3 to "assemble_matrix" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
dolfinx/fem/petsc.py:354: error: Argument 2 to "assemble_matrix_nest" has incompatible type "List[List[FormMetaClass]]"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:371: error: Too many arguments for "assemble_matrix"
dolfinx/fem/petsc.py:371: error: Argument 2 to "assemble_matrix" has incompatible type "FormMetaClass"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:371: error: Argument 3 to "assemble_matrix" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
dolfinx/fem/petsc.py:391: error: Too many arguments for "assemble_matrix_block"
dolfinx/fem/petsc.py:391: error: Argument 2 to "assemble_matrix_block" has incompatible type "List[List[FormMetaClass]]"; expected "List[DirichletBCMetaClass]"
dolfinx/fem/petsc.py:391: error: Argument 3 to "assemble_matrix_block" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
dolfinx/fem/petsc.py:461: error: Argument 1 to "bcs_by_block" has incompatible type "Iterable[Optional[FunctionSpace]]"; expected "Iterable[FunctionSpace]"
dolfinx/fem/petsc.py:468: error: Argument 3 to "apply_lifting" has incompatible type "Iterable[Iterable[DirichletBCMetaClass]]"; expected "List[List[DirichletBCMetaClass]]"
dolfinx/fem/petsc.py:564: error: "assemble_matrix" gets multiple values for keyword argument "bcs"
dolfinx/fem/petsc.py:697: error: Argument 3 to "assemble_matrix" has incompatible type "List[DirichletBCMetaClass]"; expected "float"
demo/demo_types.py:40: error: Argument "points" to "create_rectangle" has incompatible type "Tuple[Tuple[float, float], Tuple[float, float]]"; expected "List[ndarray[Any, Any]]"
demo/demo_types.py:40: error: Argument "n" to "create_rectangle" has incompatible type "Tuple[int, int]"; expected "List[Any]"
demo/demo_types.py:59: error: Argument "marker" to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_types.py:61: error: Argument "entities" to "locate_dofs_topological" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_stokes.py:140: error: Argument 3 to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_stokes.py:141: error: Argument 1 to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "Union[Function, Constant]"
demo/demo_stokes.py:141: error: Argument 2 to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_stokes.py:141: error: Argument 3 to "locate_dofs_topological" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_stokes.py:146: error: Argument 3 to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_stokes.py:147: error: Argument 2 to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_stokes.py:147: error: Argument 3 to "locate_dofs_topological" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_stokes.py:206: error: Argument 1 to "bcs_by_block" has incompatible type "Iterable[Optional[FunctionSpace]]"; expected "Iterable[FunctionSpace]"
demo/demo_stokes.py:207: error: Argument 2 to "set_bc_nest" has incompatible type "Iterable[Iterable[DirichletBCMetaClass]]"; expected "List[List[DirichletBCMetaClass]]"
demo/demo_stokes.py:422: error: Argument 3 to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_stokes.py:423: error: Argument 3 to "locate_dofs_topological" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_stokes.py:424: error: Argument 2 to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_stokes.py:430: error: Argument 3 to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_stokes.py:431: error: Argument 3 to "locate_dofs_topological" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_stokes.py:432: error: Argument 2 to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_stokes.py:439: error: Argument 2 to "locate_dofs_geometrical" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_stokes.py:440: error: Argument 2 to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_static-condensation.py:65: error: Argument 3 to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_static-condensation.py:66: error: Argument 4 to "meshtags" has incompatible type "int"; expected "ndarray[Any, Any]"
demo/demo_static-condensation.py:75: error: Argument 3 to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_static-condensation.py:76: error: Argument 3 to "locate_dofs_topological" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_static-condensation.py:77: error: Argument 2 to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_poisson.py:88: error: Argument "points" to "create_rectangle" has incompatible type "Tuple[Tuple[float, float], Tuple[float, float]]"; expected "List[ndarray[Any, Any]]"
demo/demo_poisson.py:88: error: Argument "n" to "create_rectangle" has incompatible type "Tuple[int, int]"; expected "List[Any]"
demo/demo_poisson.py:106: error: Argument "marker" to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_poisson.py:113: error: Argument "entities" to "locate_dofs_topological" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_poisson.py:119: error: Argument "dofs" to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_poisson.py:158: error: Missing positional argument "dim" in call to "create_vtk_mesh"
demo/demo_lagrange_variants.py:117: error: Argument "points" to "create_rectangle" has incompatible type "Tuple[Tuple[float, float], Tuple[float, float]]"; expected "List[ndarray[Any, Any]]"
demo/demo_lagrange_variants.py:117: error: Argument "n" to "create_rectangle" has incompatible type "Tuple[int, int]"; expected "List[Any]"
demo/demo_lagrange_variants.py:122: error: Argument "marker" to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_lagrange_variants.py:125: error: Argument "entities" to "locate_dofs_topological" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_lagrange_variants.py:127: error: Argument "dofs" to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_lagrange_variants.py:180: error: Argument 2 to "eval" of "Function" has incompatible type "List[int]"; expected "ndarray[Any, Any]"
demo/demo_interpolation-io.py:36: error: Argument 2 to "create_rectangle" has incompatible type "Tuple[Tuple[float, float], Tuple[float, float]]"; expected "List[ndarray[Any, Any]]"
demo/demo_interpolation-io.py:36: error: Argument 3 to "create_rectangle" has incompatible type "Tuple[int, int]"; expected "List[Any]"
demo/demo_interpolation-io.py:44: error: Argument 3 to "locate_entities" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_interpolation-io.py:45: error: Argument 3 to "locate_entities" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_interpolation-io.py:55: error: Argument 2 to "VectorFunctionSpace" has incompatible type "Tuple[str, int]"; expected "ElementMetaData"
demo/demo_interpolation-io.py:73: error: Missing positional argument "dim" in call to "create_vtk_mesh"
demo/demo_gmsh.py:123: error: Argument 4 to "meshtags_from_entities" has incompatible type "signedinteger[_32Bit]"; expected "ndarray[Any, Any]"
demo/demo_gmsh.py:183: error: Argument 4 to "meshtags_from_entities" has incompatible type "signedinteger[_32Bit]"; expected "ndarray[Any, Any]"
demo/demo_gmsh.py:260: error: Argument 4 to "meshtags_from_entities" has incompatible type "signedinteger[_32Bit]"; expected "ndarray[Any, Any]"
demo/demo_elasticity.py:119: error: Argument 2 to "VectorFunctionSpace" has incompatible type "Tuple[str, int]"; expected "ElementMetaData"
demo/demo_elasticity.py:130: error: Argument "marker" to "locate_entities_boundary" has incompatible type "Callable[[Any], Any]"; expected "FunctionType"
demo/demo_elasticity.py:132: error: Argument 1 to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "Union[Function, Constant]"
demo/demo_elasticity.py:133: error: Argument 2 to "dirichletbc" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_elasticity.py:133: error: Argument "entities" to "locate_dofs_topological" has incompatible type "ndarray[Any, Any]"; expected "List[int]"
demo/demo_cahn-hilliard.py:304: error: Missing positional argument "dim" in call to "create_vtk_mesh"
Found 85 errors in 12 files (checked 14 source files)

@jhale jhale merged commit 6c631eb into main May 25, 2022
@jhale jhale deleted the jhale/mypy-fixes-and-ci branch May 25, 2022 14:57
@francesco-ballarin
Copy link
Member

Can @jhale point me to which part of the diff requires python >= 3.8? I know that mypy wheels have dropped support for older python versions (at least on linux) so that must be part of the reason, but I am wondering if there are other changes in the diff that require python >= 3.8.

I am happy to keep around a weekly CI on python 3.7 for this project by shipping a minor patch on the python version check in setup.py. Admittedly, I only run a very basic demo there, but that's representative of the basic usage of the library (e.g. for teaching).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants