From 24a6297164bc422f09edfa65131ea04d0f2176e9 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:13:51 +0100 Subject: [PATCH 01/19] Add ignore all mypy config to pyproject.toml --- pyproject.toml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 555a334d2..4def28f48 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -82,3 +82,22 @@ allowed-confusables = ["𝐚", "𝐀", "∕", "γ", "⨯", "∨"] [tool.ruff.lint.pydocstyle] convention = "google" + +[tool.mypy] +disable_error_code = [ + "arg-type", + "assignment", + "attr-defined", + "has-type", + "import-not-found", + "import-untyped", + "index", + "method-assign", + "misc", + "name-defined", + "operator", + "override", + "return-value", + "type-var", + "var-annotated", +] From 6d58936aab0afcee297b6ea5cb78cc9d03584abd Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:14:05 +0100 Subject: [PATCH 02/19] Add py.typed --- ufl/py.typed | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ufl/py.typed diff --git a/ufl/py.typed b/ufl/py.typed new file mode 100644 index 000000000..e69de29bb From 367897c0c194ef6ba2645aa17400a4c3febbdfc0 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:14:22 +0100 Subject: [PATCH 03/19] Add mypy check to CI --- .github/workflows/pythonapp.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/pythonapp.yml b/.github/workflows/pythonapp.yml index 8a636e91b..c038a1261 100644 --- a/.github/workflows/pythonapp.yml +++ b/.github/workflows/pythonapp.yml @@ -46,6 +46,8 @@ jobs: ruff format --check . - name: Install UFL run: python -m pip install .[ci] + - name: Lint with mypy + run: mypy -p ufl - name: Run unit tests run: | python -m pytest -n auto --cov=ufl/ --junitxml=junit/test-results-${{ matrix.os }}-${{ matrix.python-version }}.xml test/ From aba7a3d97b6e857dfefc9d7f66e88f73d596167b Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:16:05 +0100 Subject: [PATCH 04/19] Add mypy to CI dependency --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 4def28f48..2b23743b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,7 @@ issues = "https://github.com/FEniCS/ufl/issues" funding = "https://numfocus.org/donate" [project.optional-dependencies] -lint = ["ruff"] +lint = ["mypy", "ruff"] docs = ["sphinx", "sphinx_rtd_theme"] test = ["pytest"] ci = [ From 6ac4a31b2745b46ff1e454f36c6453718ce67a5e Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:29:31 +0100 Subject: [PATCH 05/19] Fix mypy error code 'index' --- pyproject.toml | 1 - ufl/cell.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2b23743b6..c72edb91a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -91,7 +91,6 @@ disable_error_code = [ "has-type", "import-not-found", "import-untyped", - "index", "method-assign", "misc", "name-defined", diff --git a/ufl/cell.py b/ufl/cell.py index 6d565d7fd..01fa16613 100644 --- a/ufl/cell.py +++ b/ufl/cell.py @@ -184,7 +184,7 @@ def peak_types(self) -> typing.Tuple[AbstractCell, ...]: return self.sub_entity_types(tdim - 3) -_sub_entity_celltypes = { +_sub_entity_celltypes: typing.Dict[str, list[tuple[str, ...]]] = { "vertex": [("vertex",)], "interval": [tuple("vertex" for i in range(2)), ("interval",)], "triangle": [ From 6993281d219c523ea8e7feea9974a810ad4e7826 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:39:44 +0100 Subject: [PATCH 06/19] Fix mypy error code 'name-defined' --- pyproject.toml | 1 - ufl/objects.py | 4 ++-- ufl/pullback.py | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c72edb91a..2b774075d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -93,7 +93,6 @@ disable_error_code = [ "import-untyped", "method-assign", "misc", - "name-defined", "operator", "override", "return-value", diff --git a/ufl/objects.py b/ufl/objects.py index 64704d275..bd71556c5 100644 --- a/ufl/objects.py +++ b/ufl/objects.py @@ -20,10 +20,10 @@ globals()[measure_name] = Measure(integral_type) # TODO: Firedrake hack, remove later -ds_tb = ds_b + ds_t # noqa: F821 +ds_tb = ds_b + ds_t # type: ignore # noqa: F821 # Default measure dX including both uncut and cut cells -dX = dx + dC # noqa: F821 +dX = dx + dC # type: ignore # noqa: F821 # Create objects for builtin known cell types vertex = Cell("vertex") diff --git a/ufl/pullback.py b/ufl/pullback.py index 696ae9653..057d5e618 100644 --- a/ufl/pullback.py +++ b/ufl/pullback.py @@ -462,7 +462,7 @@ class SymmetricPullback(AbstractPullback): """Pull back for an element with symmetry.""" def __init__( - self, element: _AbstractFiniteElement, symmetry: typing.Dict[typing.tuple[int, ...], int] + self, element: _AbstractFiniteElement, symmetry: typing.Dict[typing.Tuple[int, ...], int] ): """Initalise. From f887c4205b8b5f6d104b8f027978473cf01ae85c Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:50:55 +0100 Subject: [PATCH 07/19] Fix mypy error code 'return-value' --- pyproject.toml | 1 - ufl/cell.py | 16 ++++++++-------- ufl/functionspace.py | 2 +- ufl/objects.py | 2 +- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2b774075d..4b95bb151 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,7 +95,6 @@ disable_error_code = [ "misc", "operator", "override", - "return-value", "type-var", "var-annotated", ] diff --git a/ufl/cell.py b/ufl/cell.py index 01fa16613..c58ccef55 100644 --- a/ufl/cell.py +++ b/ufl/cell.py @@ -358,7 +358,7 @@ def __init__(self, *cells: Cell): if not isinstance(self._tdim, numbers.Integral): raise ValueError("Expecting integer topological_dimension.") - def sub_cells(self) -> typing.List[AbstractCell]: + def sub_cells(self) -> typing.Tuple[AbstractCell, ...]: """Return list of cell factors.""" return self._cells @@ -398,21 +398,21 @@ def num_sub_entities(self, dim: int) -> int: def sub_entities(self, dim: int) -> typing.Tuple[AbstractCell, ...]: """Get the sub-entities of the given dimension.""" if dim < 0 or dim > self._tdim: - return [] + return () if dim == 0: - return [Cell("vertex") for i in range(self.num_sub_entities(0))] + return tuple(Cell("vertex") for i in range(self.num_sub_entities(0))) if dim == self._tdim: - return [self] + return (self,) raise NotImplementedError(f"TensorProductCell.sub_entities({dim}) is not implemented.") def sub_entity_types(self, dim: int) -> typing.Tuple[AbstractCell, ...]: """Get the unique sub-entity types of the given dimension.""" if dim < 0 or dim > self._tdim: - return [] + return () if dim == 0: - return [Cell("vertex")] + return (Cell("vertex"),) if dim == self._tdim: - return [self] + return (self,) raise NotImplementedError(f"TensorProductCell.sub_entities({dim}) is not implemented.") def _lt(self, other) -> bool: @@ -434,7 +434,7 @@ def _ufl_hash_data_(self) -> typing.Hashable: """UFL hash data.""" return tuple(c._ufl_hash_data_() for c in self._cells) - def reconstruct(self, **kwargs: typing.Any) -> Cell: + def reconstruct(self, **kwargs: typing.Any) -> Cell | TensorProductCell: """Reconstruct this cell, overwriting properties by those in kwargs.""" for key, value in kwargs.items(): raise TypeError(f"reconstruct() got unexpected keyword argument '{key}'") diff --git a/ufl/functionspace.py b/ufl/functionspace.py index 558b6bd73..fc78dd6c2 100644 --- a/ufl/functionspace.py +++ b/ufl/functionspace.py @@ -80,7 +80,7 @@ def components(self) -> typing.Dict[typing.Tuple[int, ...], int]: if len(self._ufl_element.sub_elements) == 0: return {(): 0} - components = {} + components: dict[tuple[int, ...], int] = {} offset = 0 c_offset = 0 for s in self.ufl_sub_spaces(): diff --git a/ufl/objects.py b/ufl/objects.py index bd71556c5..c88af62b9 100644 --- a/ufl/objects.py +++ b/ufl/objects.py @@ -20,7 +20,7 @@ globals()[measure_name] = Measure(integral_type) # TODO: Firedrake hack, remove later -ds_tb = ds_b + ds_t # type: ignore # noqa: F821 +ds_tb = ds_b + ds_t # type: ignore # noqa: F821 # Default measure dX including both uncut and cut cells dX = dx + dC # type: ignore # noqa: F821 From 95c76d5ae6fec1677d17f9b305240c529d881cbd Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 13:52:43 +0100 Subject: [PATCH 08/19] Fix mypy error code 'has-type' --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 4b95bb151..f8c49ef96 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -88,7 +88,6 @@ disable_error_code = [ "arg-type", "assignment", "attr-defined", - "has-type", "import-not-found", "import-untyped", "method-assign", From 35c2d5f82d41739dfd96bbbb1161dbcd81d50b95 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 14:00:07 +0100 Subject: [PATCH 09/19] Add types-colorama to typing dependecies --- pyproject.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index f8c49ef96..bce4aff6d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,6 +25,7 @@ funding = "https://numfocus.org/donate" [project.optional-dependencies] lint = ["mypy", "ruff"] +typing = ["types-colorama"] docs = ["sphinx", "sphinx_rtd_theme"] test = ["pytest"] ci = [ @@ -35,6 +36,7 @@ ci = [ "fenics-ufl[docs]", "fenics-ufl[lint]", "fenics-ufl[test]", + "fenics-ufl[typing]", ] [tool.setuptools] From 8d69bd1600f8256352bdcf222054aa41e8196895 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 14:03:55 +0100 Subject: [PATCH 10/19] Fix mypy error code 'import-untyped' --- pyproject.toml | 1 - ufl/mathfunctions.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bce4aff6d..bdf453a4d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -91,7 +91,6 @@ disable_error_code = [ "assignment", "attr-defined", "import-not-found", - "import-untyped", "method-assign", "misc", "operator", diff --git a/ufl/mathfunctions.py b/ufl/mathfunctions.py index 4c2d87996..bdf57b19e 100644 --- a/ufl/mathfunctions.py +++ b/ufl/mathfunctions.py @@ -421,7 +421,7 @@ def evaluate(self, x, mapping, component, index_values): """Evaluate.""" a = self.ufl_operands[1].evaluate(x, mapping, component, index_values) try: - import scipy.special + import scipy.special # type: ignore except ImportError: raise ValueError( "You must have scipy installed to evaluate bessel functions in python." From ddccf07f4469f7a5adf898eb8293f22e31a30266 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 14:12:46 +0100 Subject: [PATCH 11/19] Change base class of TensorProductCell to Cell --- ufl/cell.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ufl/cell.py b/ufl/cell.py index c58ccef55..7af0b3548 100644 --- a/ufl/cell.py +++ b/ufl/cell.py @@ -340,7 +340,7 @@ def reconstruct(self, **kwargs: typing.Any) -> Cell: return Cell(self._cellname) -class TensorProductCell(AbstractCell): +class TensorProductCell(Cell): """Tensor product cell.""" __slots__ = ("_cells", "_tdim") @@ -434,7 +434,7 @@ def _ufl_hash_data_(self) -> typing.Hashable: """UFL hash data.""" return tuple(c._ufl_hash_data_() for c in self._cells) - def reconstruct(self, **kwargs: typing.Any) -> Cell | TensorProductCell: + def reconstruct(self, **kwargs: typing.Any) -> Cell: """Reconstruct this cell, overwriting properties by those in kwargs.""" for key, value in kwargs.items(): raise TypeError(f"reconstruct() got unexpected keyword argument '{key}'") From 28e3e6b887c4b7fff042c7e65bd9c9e4cfef84a5 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 14:16:15 +0100 Subject: [PATCH 12/19] Fix mypy error code 'override' --- pyproject.toml | 1 - ufl/finiteelement.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index bdf453a4d..7b5d254d6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -94,7 +94,6 @@ disable_error_code = [ "method-assign", "misc", "operator", - "override", "type-var", "var-annotated", ] diff --git a/ufl/finiteelement.py b/ufl/finiteelement.py index 3aceef689..9c4fb4b1d 100644 --- a/ufl/finiteelement.py +++ b/ufl/finiteelement.py @@ -53,7 +53,7 @@ def __hash__(self) -> int: """Return a hash.""" @_abc.abstractmethod - def __eq__(self, other: AbstractFiniteElement) -> bool: + def __eq__(self, other: object) -> bool: """Check if this element is equal to another element.""" @_abc.abstractproperty @@ -112,7 +112,7 @@ def sub_elements(self) -> _typing.List: of sub-elements. """ - def __ne__(self, other: AbstractFiniteElement) -> bool: + def __ne__(self, other: object) -> bool: """Check if this element is different to another element.""" return not self.__eq__(other) From 7f777aa9f323105be72bdcf7d6b63dc85fa4df25 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 14:29:35 +0100 Subject: [PATCH 13/19] Switch to tpying.Tuple --- ufl/functionspace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ufl/functionspace.py b/ufl/functionspace.py index fc78dd6c2..42477fc10 100644 --- a/ufl/functionspace.py +++ b/ufl/functionspace.py @@ -80,7 +80,7 @@ def components(self) -> typing.Dict[typing.Tuple[int, ...], int]: if len(self._ufl_element.sub_elements) == 0: return {(): 0} - components: dict[tuple[int, ...], int] = {} + components: dict[typing.Tuple[int, ...], int] = {} offset = 0 c_offset = 0 for s in self.ufl_sub_spaces(): From a036787272bd399c647daf4cc997d0343a5d2410 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 18:20:58 +0100 Subject: [PATCH 14/19] Removed unused code --- ufl/exprequals.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/ufl/exprequals.py b/ufl/exprequals.py index cf0359d31..6df5ff53b 100644 --- a/ufl/exprequals.py +++ b/ufl/exprequals.py @@ -1,12 +1,5 @@ """Expr equals.""" -from collections import defaultdict - -hash_total = defaultdict(int) -hash_collisions = defaultdict(int) -hash_equals = defaultdict(int) -hash_notequals = defaultdict(int) - def expr_equals(self, other): """Checks whether the two expressions are represented the exact same way. From 9ff27d7dcbc9dcf8a4b8372c3a70b8b09e570a6a Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 18:46:49 +0100 Subject: [PATCH 15/19] Fix mypy error code 'var-annotated' --- pyproject.toml | 1 - ufl/algorithms/formsplitter.py | 3 ++- ufl/algorithms/signature.py | 4 +++- ufl/algorithms/transformer.py | 3 ++- ufl/constantvalue.py | 5 +++-- ufl/core/expr.py | 7 ++++--- ufl/core/multiindex.py | 6 ++++-- ufl/core/ufl_type.py | 8 ++++---- ufl/corealg/multifunction.py | 3 ++- 9 files changed, 24 insertions(+), 16 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 7b5d254d6..5c5a684af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,5 +95,4 @@ disable_error_code = [ "misc", "operator", "type-var", - "var-annotated", ] diff --git a/ufl/algorithms/formsplitter.py b/ufl/algorithms/formsplitter.py index a81176b5c..aeb955310 100644 --- a/ufl/algorithms/formsplitter.py +++ b/ufl/algorithms/formsplitter.py @@ -8,6 +8,7 @@ # # Modified by Cecile Daversin-Catty, 2018 +import typing from typing import Optional import numpy as np @@ -127,7 +128,7 @@ def extract_blocks(form, i: Optional[int] = None, j: Optional[None] = None): num_sub_elements = arguments[0].ufl_element().num_sub_elements forms = [] for pi in range(num_sub_elements): - form_i = [] + form_i: typing.List[typing.Optional[object]] = [] for pj in range(num_sub_elements): f = fs.split(form, pi, pj) if f.empty(): diff --git a/ufl/algorithms/signature.py b/ufl/algorithms/signature.py index 807fa5c00..9395ecd12 100644 --- a/ufl/algorithms/signature.py +++ b/ufl/algorithms/signature.py @@ -6,6 +6,7 @@ # SPDX-License-Identifier: LGPL-3.0-or-later import hashlib +import typing from ufl.algorithms.domain_analysis import canonicalize_metadata from ufl.classes import ( @@ -20,6 +21,7 @@ Label, MultiIndex, ) +from ufl.core.ufl_type import UFLObject from ufl.corealg.traversal import traverse_unique_terminals, unique_post_traversal @@ -94,7 +96,7 @@ def compute_terminal_hashdata(expressions, renumbering): def compute_expression_hashdata(expression, terminal_hashdata) -> bytes: """Compute expression hashdata.""" - cache = {} + cache: typing.Dict[UFLObject, bytes] = {} for expr in unique_post_traversal(expression): # Uniquely traverse tree and hash each node diff --git a/ufl/algorithms/transformer.py b/ufl/algorithms/transformer.py index 23f49acc2..c20948a28 100644 --- a/ufl/algorithms/transformer.py +++ b/ufl/algorithms/transformer.py @@ -14,6 +14,7 @@ # Modified by Anders Logg, 2009-2010 import inspect +import typing from ufl.algorithms.map_integrands import map_integrands from ufl.classes import Variable, all_ufl_classes @@ -35,7 +36,7 @@ class Transformer(object): transform expression trees from one representation to another. """ - _handlers_cache = {} + _handlers_cache: typing.Dict[type, typing.Tuple[str, bool]] = {} def __init__(self, variable_cache=None): """Initialise.""" diff --git a/ufl/constantvalue.py b/ufl/constantvalue.py index 007350680..3ac0385cb 100644 --- a/ufl/constantvalue.py +++ b/ufl/constantvalue.py @@ -10,6 +10,7 @@ # Modified by Massimiliano Leoni, 2016. import numbers +import typing from math import atan2 import ufl @@ -65,7 +66,7 @@ class Zero(ConstantValue): __slots__ = ("ufl_free_indices", "ufl_index_dimensions", "ufl_shape") - _cache = {} + _cache: typing.Dict[typing.Tuple[int], typing.Self] = {} def __getnewargs__(self): """Get new args.""" @@ -362,7 +363,7 @@ class IntValue(RealValue): __slots__ = () - _cache = {} + _cache: typing.Dict[int, typing.Self] = {} def __getnewargs__(self): """Get new args.""" diff --git a/ufl/core/expr.py b/ufl/core/expr.py index 93149e2f6..0ef5d4c75 100644 --- a/ufl/core/expr.py +++ b/ufl/core/expr.py @@ -16,9 +16,10 @@ # Modified by Anders Logg, 2008 # Modified by Massimiliano Leoni, 2016 +import typing import warnings -from ufl.core.ufl_type import UFLType, update_ufl_type_attributes +from ufl.core.ufl_type import UFLObject, UFLType, update_ufl_type_attributes class Expr(object, metaclass=UFLType): @@ -204,10 +205,10 @@ def __init__(self): # A global dict mapping language_operator_name to the type it # produces - _ufl_language_operators_ = {} + _ufl_language_operators_: typing.Dict[str, object] = {} # List of all terminal modifier types - _ufl_terminal_modifiers_ = [] + _ufl_terminal_modifiers_: typing.List[UFLObject] = [] # --- Mechanism for profiling object creation and deletion --- diff --git a/ufl/core/multiindex.py b/ufl/core/multiindex.py index 416fb8c82..6a6bef73b 100644 --- a/ufl/core/multiindex.py +++ b/ufl/core/multiindex.py @@ -8,6 +8,8 @@ # # Modified by Massimiliano Leoni, 2016. +import typing + from ufl.core.terminal import Terminal from ufl.core.ufl_type import ufl_type from ufl.utils.counted import Counted @@ -30,7 +32,7 @@ class FixedIndex(IndexBase): __slots__ = ("_hash", "_value") - _cache = {} + _cache: typing.Dict[int, typing.Self] = {} def __getnewargs__(self): """Get new args.""" @@ -116,7 +118,7 @@ class MultiIndex(Terminal): __slots__ = ("_indices",) - _cache = {} + _cache: typing.Dict[typing.Tuple[int], typing.Self] = {} def __getnewargs__(self): """Get new args.""" diff --git a/ufl/core/ufl_type.py b/ufl/core/ufl_type.py index c409d05fb..e0b6d8ce4 100644 --- a/ufl/core/ufl_type.py +++ b/ufl/core/ufl_type.py @@ -416,18 +416,18 @@ class UFLType(type): _ufl_handler_name_ = "ufl_type" # A global array of all Expr and BaseForm subclasses, indexed by typecode - _ufl_all_classes_ = [] + _ufl_all_classes_: typing.List[UFLObject] = [] # A global set of all handler names added - _ufl_all_handler_names_ = set() + _ufl_all_handler_names_: typing.Set[str] = set() # A global array of the number of initialized objects for each # typecode - _ufl_obj_init_counts_ = [] + _ufl_obj_init_counts_: typing.List[int] = [] # A global array of the number of deleted objects for each # typecode - _ufl_obj_del_counts_ = [] + _ufl_obj_del_counts_: typing.List[int] = [] # Type trait: If the type is abstract. An abstract class cannot # be instantiated and does not need all properties specified. diff --git a/ufl/corealg/multifunction.py b/ufl/corealg/multifunction.py index 559ec98f2..ba17fb3f5 100644 --- a/ufl/corealg/multifunction.py +++ b/ufl/corealg/multifunction.py @@ -7,6 +7,7 @@ # # Modified by Massimiliano Leoni, 2016 +import typing from inspect import signature from ufl.core.expr import Expr @@ -46,7 +47,7 @@ class MultiFunction(object): algorithm object. Of course Python's function call overhead still applies. """ - _handlers_cache = {} + _handlers_cache: typing.Dict[type, typing.Tuple[typing.List[str], bool]] = {} def __init__(self): """Initialise.""" From e5c95aff091f5bb62e412190d0ca33ffefa18a3d Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 18:56:42 +0100 Subject: [PATCH 16/19] Replace typing.Self with older python variant --- ufl/constantvalue.py | 4 ++-- ufl/core/multiindex.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ufl/constantvalue.py b/ufl/constantvalue.py index 3ac0385cb..c97e7cff7 100644 --- a/ufl/constantvalue.py +++ b/ufl/constantvalue.py @@ -66,7 +66,7 @@ class Zero(ConstantValue): __slots__ = ("ufl_free_indices", "ufl_index_dimensions", "ufl_shape") - _cache: typing.Dict[typing.Tuple[int], typing.Self] = {} + _cache: typing.Dict[typing.Tuple[int], 'Zero'] = {} def __getnewargs__(self): """Get new args.""" @@ -363,7 +363,7 @@ class IntValue(RealValue): __slots__ = () - _cache: typing.Dict[int, typing.Self] = {} + _cache: typing.Dict[int, 'IntValue'] = {} def __getnewargs__(self): """Get new args.""" diff --git a/ufl/core/multiindex.py b/ufl/core/multiindex.py index 6a6bef73b..d9c930839 100644 --- a/ufl/core/multiindex.py +++ b/ufl/core/multiindex.py @@ -32,7 +32,7 @@ class FixedIndex(IndexBase): __slots__ = ("_hash", "_value") - _cache: typing.Dict[int, typing.Self] = {} + _cache: typing.Dict[int, 'FixedIndex'] = {} def __getnewargs__(self): """Get new args.""" @@ -118,7 +118,7 @@ class MultiIndex(Terminal): __slots__ = ("_indices",) - _cache: typing.Dict[typing.Tuple[int], typing.Self] = {} + _cache: typing.Dict[typing.Tuple[int], 'MultiIndex'] = {} def __getnewargs__(self): """Get new args.""" From 9f25a0f638d91ff07ba987837b6b8bacde0d4389 Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 18:59:36 +0100 Subject: [PATCH 17/19] Fix mypy error code 'method-assign' --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 5c5a684af..f54517cf6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -91,7 +91,6 @@ disable_error_code = [ "assignment", "attr-defined", "import-not-found", - "method-assign", "misc", "operator", "type-var", From c64d04731f3993f355f13487758cf9dcefccc55a Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 19:04:34 +0100 Subject: [PATCH 18/19] Fix mypy error code 'import-not-found' --- pyproject.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f54517cf6..552c6b6a7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -90,7 +90,6 @@ disable_error_code = [ "arg-type", "assignment", "attr-defined", - "import-not-found", "misc", "operator", "type-var", From cdbb08afecb96fccceb5d94d740e51a9fd391c6e Mon Sep 17 00:00:00 2001 From: schnellerhase <56360279+schnellerhase@users.noreply.github.com> Date: Sun, 2 Mar 2025 19:07:10 +0100 Subject: [PATCH 19/19] Ruff --- ufl/constantvalue.py | 4 ++-- ufl/core/multiindex.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ufl/constantvalue.py b/ufl/constantvalue.py index c97e7cff7..aa72c59c7 100644 --- a/ufl/constantvalue.py +++ b/ufl/constantvalue.py @@ -66,7 +66,7 @@ class Zero(ConstantValue): __slots__ = ("ufl_free_indices", "ufl_index_dimensions", "ufl_shape") - _cache: typing.Dict[typing.Tuple[int], 'Zero'] = {} + _cache: typing.Dict[typing.Tuple[int], "Zero"] = {} def __getnewargs__(self): """Get new args.""" @@ -363,7 +363,7 @@ class IntValue(RealValue): __slots__ = () - _cache: typing.Dict[int, 'IntValue'] = {} + _cache: typing.Dict[int, "IntValue"] = {} def __getnewargs__(self): """Get new args.""" diff --git a/ufl/core/multiindex.py b/ufl/core/multiindex.py index d9c930839..a5074b388 100644 --- a/ufl/core/multiindex.py +++ b/ufl/core/multiindex.py @@ -32,7 +32,7 @@ class FixedIndex(IndexBase): __slots__ = ("_hash", "_value") - _cache: typing.Dict[int, 'FixedIndex'] = {} + _cache: typing.Dict[int, "FixedIndex"] = {} def __getnewargs__(self): """Get new args.""" @@ -118,7 +118,7 @@ class MultiIndex(Terminal): __slots__ = ("_indices",) - _cache: typing.Dict[typing.Tuple[int], 'MultiIndex'] = {} + _cache: typing.Dict[typing.Tuple[int], "MultiIndex"] = {} def __getnewargs__(self): """Get new args."""