From e6ea2b525b085c1fcb63f1be9d222dcebd7c3e7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Dokken?= Date: Tue, 31 May 2022 10:12:37 +0000 Subject: [PATCH 1/5] Add proper ignores to mypy and add stubs to meshtags --- python/dolfinx/fem/bcs.py | 10 +++++----- python/dolfinx/fem/forms.py | 10 +++++----- python/dolfinx/la.py | 4 ++-- python/dolfinx/mesh.py | 21 ++++++++++++++++++++- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/python/dolfinx/fem/bcs.py b/python/dolfinx/fem/bcs.py index 6684a0e498a..161e89c252b 100644 --- a/python/dolfinx/fem/bcs.py +++ b/python/dolfinx/fem/bcs.py @@ -121,21 +121,21 @@ class initialiser. This class is combined with different if V is not None: try: - super().__init__(_value, dofs, V) # type: ignore + super().__init__(_value, dofs, V) # type: ignore[call-arg] except TypeError: - super().__init__(_value, dofs, V._cpp_object) # type: ignore + super().__init__(_value, dofs, V._cpp_object) # type: ignore[call-arg] else: - super().__init__(_value, dofs) # type: ignore + super().__init__(_value, dofs) # type: ignore[call-arg] @property def g(self): """The boundary condition value(s)""" - return self.value # type: ignore + return self.value @property def function_space(self) -> dolfinx.fem.FunctionSpace: """The function space on which the boundary condition is defined""" - return super().function_space # type: ignore + return super().function_space # type: ignore[misc] def dirichletbc(value: typing.Union[Function, Constant, np.ndarray], diff --git a/python/dolfinx/fem/forms.py b/python/dolfinx/fem/forms.py index 191e665ed57..ed2012cd196 100644 --- a/python/dolfinx/fem/forms.py +++ b/python/dolfinx/fem/forms.py @@ -51,7 +51,7 @@ def __init__(self, form, V: list[_cpp.fem.FunctionSpace], coeffs, constants, self._ufcx_form = form ffi = cffi.FFI() super().__init__(ffi.cast("uintptr_t", ffi.addressof(self._ufcx_form)), - V, coeffs, constants, subdomains, mesh) # type: ignore + V, coeffs, constants, subdomains, mesh) # type: ignore[call-arg] @property def ufcx_form(self): @@ -66,22 +66,22 @@ def code(self) -> str: @property def function_spaces(self) -> typing.List[FunctionSpace]: """Function spaces on which this form is defined""" - return super().function_spaces # type: ignore + return super().function_spaces # type: ignore[misc] @property def dtype(self) -> np.dtype: """dtype of this form""" - return super().dtype # type: ignore + return super().dtype # type: ignore[misc] @property def mesh(self) -> Mesh: """Mesh on which this form is defined""" - return super().mesh # type: ignore + return super().mesh # type: ignore[misc] @property def integral_types(self): """Integral types in the form""" - return super().integral_types # type: ignore + return super().integral_types # type: ignore[misc] form_types = typing.Union[FormMetaClass, _cpp.fem.Form_float32, _cpp.fem.Form_float64, diff --git a/python/dolfinx/la.py b/python/dolfinx/la.py index bf19240a4b7..16b11eab0af 100644 --- a/python/dolfinx/la.py +++ b/python/dolfinx/la.py @@ -95,11 +95,11 @@ def __init__(self, map, bs): and not created using the class initialiser. """ - super().__init__(map, bs) # type: ignore + super().__init__(map, bs) @property def array(self) -> np.ndarray: - return super().array # type: ignore + return super().array # type: ignore[misc] def vector(map, bs=1, dtype=np.float64) -> VectorMetaClass: diff --git a/python/dolfinx/mesh.py b/python/dolfinx/mesh.py index d66a4053163..45ad1454160 100644 --- a/python/dolfinx/mesh.py +++ b/python/dolfinx/mesh.py @@ -216,7 +216,7 @@ def __init__(self, mesh: Mesh, dim: int, indices: np.ndarray, values: np.ndarray directly. """ - super().__init__(mesh, dim, indices.astype(np.int32), values) # type: ignore + super().__init__(mesh, dim, indices.astype(np.int32), values) # type: ignore[call-arg] def ufl_id(self) -> int: """Object identifier. @@ -230,6 +230,25 @@ def ufl_id(self) -> int: """ return id(self) + @property + def dim(self) -> int: + """Topological dimension of the entities""" + return super().dim # type: ignore[misc] + + @property + def values(self) -> np.ndarray: + """ The values corresponding to each entity""" + return super().values # type: ignore[misc] + + @property + def indices(self) -> numpy.typing.NDArray[np.int32]: + """ Entity indices (local to process)""" + return super().indices # type: ignore[misc] + + def find(self, value: typing.Union[int, np.int8, np.int32, np.int64, np.float64]): + """ Find all entities marked with input value""" + return super().find(value) # type:ignore[misc] + def meshtags(mesh: Mesh, dim: int, indices: np.ndarray, values: typing.Union[np.ndarray, int, float]) -> MeshTagsMetaClass: From 03e797a0a206c6f51106517dc80a5d1183644840 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Dokken?= Date: Tue, 31 May 2022 11:19:09 +0000 Subject: [PATCH 2/5] Expose name property --- python/dolfinx/mesh.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/python/dolfinx/mesh.py b/python/dolfinx/mesh.py index 45ad1454160..fa0b4452239 100644 --- a/python/dolfinx/mesh.py +++ b/python/dolfinx/mesh.py @@ -245,6 +245,10 @@ def indices(self) -> numpy.typing.NDArray[np.int32]: """ Entity indices (local to process)""" return super().indices # type: ignore[misc] + @property + def name(self) -> str: + return super().name # type: ignore[misc] + def find(self, value: typing.Union[int, np.int8, np.int32, np.int64, np.float64]): """ Find all entities marked with input value""" return super().find(value) # type:ignore[misc] From f86f463c9a006a5a5c3b702349a3af1fca088b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Dokken?= Date: Tue, 31 May 2022 12:26:01 +0000 Subject: [PATCH 3/5] Rename C++ property to avoid recursion error when adding setter in python --- python/dolfinx/mesh.py | 6 +++++- python/dolfinx/wrappers/mesh.cpp | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/python/dolfinx/mesh.py b/python/dolfinx/mesh.py index fa0b4452239..e1503df4f24 100644 --- a/python/dolfinx/mesh.py +++ b/python/dolfinx/mesh.py @@ -247,7 +247,11 @@ def indices(self) -> numpy.typing.NDArray[np.int32]: @property def name(self) -> str: - return super().name # type: ignore[misc] + return super()._name # type: ignore[misc] + + @name.setter + def name(self, name: str): + super().__setattr__("_name", name) def find(self, value: typing.Union[int, np.int8, np.int32, np.int64, np.float64]): """ Find all entities marked with input value""" diff --git a/python/dolfinx/wrappers/mesh.cpp b/python/dolfinx/wrappers/mesh.cpp index ec740507b99..ed61204ade0 100644 --- a/python/dolfinx/wrappers/mesh.cpp +++ b/python/dolfinx/wrappers/mesh.cpp @@ -107,7 +107,7 @@ void declare_meshtags(py::module& m, std::string type) })) .def_property_readonly("dtype", [](const dolfinx::mesh::MeshTags& self) { return py::dtype::of(); }) - .def_readwrite("name", &dolfinx::mesh::MeshTags::name) + .def_readwrite("_name", &dolfinx::mesh::MeshTags::name) .def_property_readonly("dim", &dolfinx::mesh::MeshTags::dim) .def_property_readonly("mesh", &dolfinx::mesh::MeshTags::mesh) .def_property_readonly("values", From 802c5b01b919140816c1047d0c64cbb7f28b2a9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Dokken?= Date: Tue, 31 May 2022 14:17:13 +0000 Subject: [PATCH 4/5] Use _name for C++ meshtag --- python/demo/demo_gmsh.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/demo/demo_gmsh.py b/python/demo/demo_gmsh.py index 8e19d0259db..352e93f011d 100644 --- a/python/demo/demo_gmsh.py +++ b/python/demo/demo_gmsh.py @@ -121,7 +121,7 @@ msh.topology.create_connectivity(2, 0) mt = meshtags_from_entities(msh, 2, create_adjacencylist(entities), values) -mt.name = "ball_d1_surface" +mt._name = "ball_d1_surface" with XDMFFile(MPI.COMM_WORLD, "out_gmsh/mesh.xdmf", "w") as file: file.write_mesh(msh) @@ -181,7 +181,7 @@ entities, values = distribute_entity_data(msh, 2, marked_facets, facet_values) msh.topology.create_connectivity(2, 0) mt = meshtags_from_entities(msh, 2, create_adjacencylist(entities), values) -mt.name = "ball_d2_surface" +mt._name = "ball_d2_surface" with XDMFFile(MPI.COMM_WORLD, "out_gmsh/mesh.xdmf", "a") as file: file.write_mesh(msh) msh.topology.create_connectivity(2, 3) @@ -258,7 +258,7 @@ entities, values = distribute_entity_data(msh, 2, marked_facets, facet_values) msh.topology.create_connectivity(2, 0) mt = meshtags_from_entities(msh, 2, create_adjacencylist(entities), values) -mt.name = "hex_d2_surface" +mt._name = "hex_d2_surface" with XDMFFile(MPI.COMM_WORLD, "out_gmsh/mesh.xdmf", "a") as file: file.write_mesh(msh) From 3c4a7d8f3491638e71682523e2ed2addbf68ede3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Dokken?= Date: Tue, 31 May 2022 15:04:57 +0000 Subject: [PATCH 5/5] Various fixes --- python/test/conftest.py | 4 ++-- python/test/unit/fem/test_custom_assembler.py | 5 +++-- python/test/unit/io/test_xdmf_meshtags.py | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/python/test/conftest.py b/python/test/conftest.py index bd7f2efcbf6..3080be7d5e9 100644 --- a/python/test/conftest.py +++ b/python/test/conftest.py @@ -120,8 +120,8 @@ def _create_tempdir(request): # Assigning a function member variables is a bit of a nasty hack -_create_tempdir._sequencenumber = defaultdict(int) # type: ignore -_create_tempdir._basepaths = set() # type: ignore +_create_tempdir._sequencenumber = defaultdict(int) # type: ignore[attr-defined] +_create_tempdir._basepaths = set() # type: ignore[attr-defined] @pytest.fixture(scope="function") diff --git a/python/test/unit/fem/test_custom_assembler.py b/python/test/unit/fem/test_custom_assembler.py index 3be30cd5757..91557008da0 100644 --- a/python/test/unit/fem/test_custom_assembler.py +++ b/python/test/unit/fem/test_custom_assembler.py @@ -88,8 +88,9 @@ # Get the PETSc MatSetValuesLocal function via ctypes # ctypes does not support static types well, ignore type check errors MatSetValues_ctypes = petsc_lib_ctypes.MatSetValuesLocal -MatSetValues_ctypes.argtypes = [ctypes.c_void_p, ctypes_index, ctypes.POINTER( # type: ignore - ctypes_index), ctypes_index, ctypes.POINTER(ctypes_index), ctypes.c_void_p, ctypes.c_int] # type: ignore +MatSetValues_ctypes.argtypes = [ + ctypes.c_void_p, ctypes_index, ctypes.POINTER(ctypes_index), # type: ignore[list-item,arg-type] + ctypes_index, ctypes.POINTER(ctypes_index), ctypes.c_void_p, ctypes.c_int] # type: ignore[list-item,arg-type] del petsc_lib_ctypes diff --git a/python/test/unit/io/test_xdmf_meshtags.py b/python/test/unit/io/test_xdmf_meshtags.py index 0977b0e6612..b4d7c8d63a8 100644 --- a/python/test/unit/io/test_xdmf_meshtags.py +++ b/python/test/unit/io/test_xdmf_meshtags.py @@ -69,8 +69,8 @@ def test_3d(tempdir, cell_type, encoding): mt_lines_in = file.read_meshtags(mesh_in, "lines") units = file.read_information("units") assert units == "mm" - assert mt_in.name == "facets" - assert mt_lines_in.name == "lines" + assert mt_in._name == "facets" + assert mt_lines_in._name == "lines" with XDMFFile(comm, Path(tempdir, "meshtags_3d_out.xdmf"), "w", encoding=encoding) as file: file.write_mesh(mesh_in)