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

Initial mypy support #356

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
24a6297
Add ignore all mypy config to pyproject.toml
schnellerhase Mar 2, 2025
6d58936
Add py.typed
schnellerhase Mar 2, 2025
367897c
Add mypy check to CI
schnellerhase Mar 2, 2025
aba7a3d
Add mypy to CI dependency
schnellerhase Mar 2, 2025
6ac4a31
Fix mypy error code 'index'
schnellerhase Mar 2, 2025
6993281
Fix mypy error code 'name-defined'
schnellerhase Mar 2, 2025
f887c42
Fix mypy error code 'return-value'
schnellerhase Mar 2, 2025
95c76d5
Fix mypy error code 'has-type'
schnellerhase Mar 2, 2025
35c2d5f
Add types-colorama to typing dependecies
schnellerhase Mar 2, 2025
8d69bd1
Fix mypy error code 'import-untyped'
schnellerhase Mar 2, 2025
ddccf07
Change base class of TensorProductCell to Cell
schnellerhase Mar 2, 2025
28e3e6b
Fix mypy error code 'override'
schnellerhase Mar 2, 2025
7f777aa
Switch to tpying.Tuple
schnellerhase Mar 2, 2025
a036787
Removed unused code
schnellerhase Mar 2, 2025
9ff27d7
Fix mypy error code 'var-annotated'
schnellerhase Mar 2, 2025
e5c95af
Replace typing.Self with older python variant
schnellerhase Mar 2, 2025
9f25a0f
Fix mypy error code 'method-assign'
schnellerhase Mar 2, 2025
c64d047
Fix mypy error code 'import-not-found'
schnellerhase Mar 2, 2025
cdbb08a
Ruff
schnellerhase Mar 2, 2025
4687473
Merge branch 'main' into mypy
schnellerhase Mar 6, 2025
57c69ce
Merge branch 'main' into mypy
jorgensd Mar 7, 2025
ee07111
Merge branch 'main' into mypy
jorgensd Mar 7, 2025
43c292d
Merge branch 'main' into mypy
garth-wells Mar 15, 2025
c559532
Merge branch 'main' into mypy
schnellerhase Mar 21, 2025
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
Prev Previous commit
Next Next commit
Fix mypy error code 'var-annotated'
schnellerhase committed Mar 2, 2025
commit 9ff27d7dcbc9dcf8a4b8372c3a70b8b09e570a6a
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -95,5 +95,4 @@ disable_error_code = [
"misc",
"operator",
"type-var",
"var-annotated",
]
3 changes: 2 additions & 1 deletion ufl/algorithms/formsplitter.py
Original file line number Diff line number Diff line change
@@ -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():
4 changes: 3 additions & 1 deletion ufl/algorithms/signature.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion ufl/algorithms/transformer.py
Original file line number Diff line number Diff line change
@@ -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."""
5 changes: 3 additions & 2 deletions ufl/constantvalue.py
Original file line number Diff line number Diff line change
@@ -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."""
7 changes: 4 additions & 3 deletions ufl/core/expr.py
Original file line number Diff line number Diff line change
@@ -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 ---

6 changes: 4 additions & 2 deletions ufl/core/multiindex.py
Original file line number Diff line number Diff line change
@@ -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."""
8 changes: 4 additions & 4 deletions ufl/core/ufl_type.py
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion ufl/corealg/multifunction.py
Original file line number Diff line number Diff line change
@@ -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."""