Skip to content

Commit fd9d07d

Browse files
committed
Bump ruff Python compat target to 3.10
1 parent 13fb3d2 commit fd9d07d

21 files changed

+65
-88
lines changed

pymbolic/geometric_algebra/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
"""
2525

2626
from abc import ABC, abstractmethod
27-
from collections.abc import Mapping
27+
from collections.abc import Mapping, Sequence
2828
from dataclasses import dataclass
29-
from typing import Any, Dict, Generic, Sequence, TypeVar, cast
29+
from typing import Any, Generic, TypeVar, cast
3030

3131
import numpy as np
3232

@@ -590,7 +590,7 @@ def __init__(
590590
else:
591591
new_data[bits] = new_coeff
592592
else:
593-
new_data = cast(Dict[int, CoeffT], data)
593+
new_data = cast(dict[int, CoeffT], data)
594594

595595
# }}}
596596

pymbolic/geometric_algebra/primitives.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
# This is experimental, undocumented, and could go away any second.
2727
# Consider yourself warned.
2828

29-
from typing import ClassVar, Hashable
29+
from collections.abc import Hashable
30+
from typing import ClassVar
3031

3132
from pymbolic.primitives import Expression, Variable, expr_dataclass
3233
from pymbolic.typing import ExpressionT

pymbolic/interop/ast.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -457,19 +457,10 @@ def to_evaluatable_python_function(expr: ExpressionT,
457457
def foo(*, E, S):
458458
return S // 32 + E % 32
459459
"""
460-
import sys
461460

462461
from pymbolic.mapper.dependency import CachedDependencyMapper
463462

464-
if sys.version_info < (3, 9):
465-
try:
466-
from astunparse import unparse
467-
except ImportError:
468-
raise RuntimeError("'to_evaluate_python_function' needs"
469-
"astunparse for Py<3.9. Install via `pip"
470-
" install astunparse`") from None
471-
else:
472-
unparse = ast.unparse
463+
unparse = ast.unparse
473464

474465
dep_mapper = CachedDependencyMapper(composite_leaves=True)
475466
deps = sorted({dep.name for dep in dep_mapper(expr)})

pymbolic/interop/matchpy/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@
4242

4343

4444
import abc
45+
from collections.abc import Callable, Iterable, Iterator, Mapping
4546
from dataclasses import dataclass, field, fields
4647
from functools import partial
47-
from typing import Callable, ClassVar, Generic, Iterable, Iterator, Mapping, TypeVar
48+
from typing import ClassVar, Generic, TypeAlias, TypeVar
4849

4950
from matchpy import (
5051
Arity,
@@ -54,7 +55,6 @@
5455
ReplacementRule,
5556
Wildcard as BaseWildcard,
5657
)
57-
from typing_extensions import TypeAlias
5858

5959
import pymbolic.primitives as p
6060
from pymbolic.typing import ScalarT
@@ -85,7 +85,7 @@ def head(self):
8585

8686
def __lt__(self, other):
8787
# Used by matchpy internally to order subexpressions
88-
if not isinstance(other, (Expression,)):
88+
if not isinstance(other, Expression):
8989
return NotImplemented
9090
if type(other) is type(self):
9191
if self.value == other.value:

pymbolic/interop/matchpy/mapper.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

3-
from typing import Any, Callable
3+
from collections.abc import Callable
4+
from typing import Any
45

56
from pymbolic.interop.matchpy import PymbolicOp
67

pymbolic/interop/matchpy/tofrom.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from __future__ import annotations
22

3+
from collections.abc import Callable
34
from dataclasses import dataclass
4-
from typing import Any, Callable
5+
from typing import Any
56

67
import multiset
78
import numpy as np

pymbolic/mapper/__init__.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,19 @@
2424
"""
2525

2626
from abc import ABC, abstractmethod
27-
from collections.abc import Mapping
27+
from collections.abc import Callable, Hashable, Iterable, Mapping, Set
2828
from typing import (
2929
TYPE_CHECKING,
30-
AbstractSet,
31-
Callable,
30+
Concatenate,
3231
Generic,
33-
Hashable,
34-
Iterable,
32+
TypeAlias,
3533
TypeVar,
3634
cast,
3735
)
3836
from warnings import warn
3937

4038
from immutabledict import immutabledict
41-
from typing_extensions import Concatenate, ParamSpec, TypeAlias, TypeIs
39+
from typing_extensions import ParamSpec, TypeIs
4240

4341
import pymbolic.primitives as p
4442
from pymbolic.typing import ArithmeticExpressionT, ExpressionT
@@ -640,7 +638,7 @@ class CachedCombineMapper(CachedMapper, CombineMapper):
640638
CollectedT = TypeVar("CollectedT")
641639

642640

643-
class Collector(CombineMapper[AbstractSet[CollectedT], P]):
641+
class Collector(CombineMapper[Set[CollectedT], P]):
644642
"""A subclass of :class:`CombineMapper` for the common purpose of
645643
collecting data derived from an expression in a set that gets 'unioned'
646644
across children at each non-leaf node in the expression tree.
@@ -651,34 +649,34 @@ class Collector(CombineMapper[AbstractSet[CollectedT], P]):
651649
"""
652650

653651
def combine(self,
654-
values: Iterable[AbstractSet[CollectedT]]
655-
) -> AbstractSet[CollectedT]:
652+
values: Iterable[Set[CollectedT]]
653+
) -> Set[CollectedT]:
656654
import operator
657655
from functools import reduce
658656
return reduce(operator.or_, values, set())
659657

660658
def map_constant(self, expr: object,
661-
*args: P.args, **kwargs: P.kwargs) -> AbstractSet[CollectedT]:
659+
*args: P.args, **kwargs: P.kwargs) -> Set[CollectedT]:
662660
return set()
663661

664662
def map_variable(self, expr: p.Variable,
665-
*args: P.args, **kwargs: P.kwargs) -> AbstractSet[CollectedT]:
663+
*args: P.args, **kwargs: P.kwargs) -> Set[CollectedT]:
666664
return set()
667665

668666
def map_wildcard(self, expr: p.Wildcard,
669-
*args: P.args, **kwargs: P.kwargs) -> AbstractSet[CollectedT]:
667+
*args: P.args, **kwargs: P.kwargs) -> Set[CollectedT]:
670668
return set()
671669

672670
def map_dot_wildcard(self, expr: p.DotWildcard,
673-
*args: P.args, **kwargs: P.kwargs) -> AbstractSet[CollectedT]:
671+
*args: P.args, **kwargs: P.kwargs) -> Set[CollectedT]:
674672
return set()
675673

676674
def map_star_wildcard(self, expr: p.StarWildcard,
677-
*args: P.args, **kwargs: P.kwargs) -> AbstractSet[CollectedT]:
675+
*args: P.args, **kwargs: P.kwargs) -> Set[CollectedT]:
678676
return set()
679677

680678
def map_function_symbol(self, expr: p.FunctionSymbol,
681-
*args: P.args, **kwargs: P.kwargs) -> AbstractSet[CollectedT]:
679+
*args: P.args, **kwargs: P.kwargs) -> Set[CollectedT]:
682680
return set()
683681

684682

pymbolic/mapper/coefficient.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
THE SOFTWARE.
2424
"""
2525

26-
from collections.abc import Collection
27-
from typing import Literal, Mapping, TypeAlias, cast
26+
from collections.abc import Collection, Mapping
27+
from typing import Literal, TypeAlias, cast
2828

2929
import pymbolic.primitives as p
3030
from pymbolic.mapper import Mapper

pymbolic/mapper/collector.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
THE SOFTWARE.
2727
"""
2828

29-
from typing import AbstractSet, Sequence, cast
29+
from collections.abc import Sequence, Set
30+
from typing import cast
3031

3132
import pymbolic
3233
import pymbolic.primitives as p
@@ -43,7 +44,7 @@ class TermCollector(IdentityMapper[[]]):
4344
coefficients and are not used for term collection.
4445
"""
4546

46-
def __init__(self, parameters: AbstractSet[p.AlgebraicLeaf] | None = None):
47+
def __init__(self, parameters: Set[p.AlgebraicLeaf] | None = None):
4748
if parameters is None:
4849
parameters = set()
4950
self.parameters = parameters
@@ -53,7 +54,7 @@ def get_dependencies(self, expr: ExpressionT) -> DependenciesT:
5354
return DependencyMapper()(expr)
5455

5556
def split_term(self, mul_term: ExpressionT) -> tuple[
56-
AbstractSet[tuple[ArithmeticExpressionT, ArithmeticExpressionT]],
57+
Set[tuple[ArithmeticExpressionT, ArithmeticExpressionT]],
5758
ArithmeticExpressionT
5859
]:
5960
"""Returns a pair consisting of:
@@ -81,7 +82,7 @@ def exponent(term: ExpressionT) -> ArithmeticExpressionT:
8182

8283
if isinstance(mul_term, Product):
8384
terms: Sequence[ExpressionT] = mul_term.children
84-
elif isinstance(mul_term, (Power, AlgebraicLeaf)):
85+
elif isinstance(mul_term, Power | AlgebraicLeaf):
8586
terms = [mul_term]
8687
elif not bool(self.get_dependencies(mul_term)):
8788
terms = [mul_term]
@@ -114,7 +115,7 @@ def exponent(term: ExpressionT) -> ArithmeticExpressionT:
114115

115116
def map_sum(self, expr: p.Sum) -> ExpressionT:
116117
term2coeff: dict[
117-
AbstractSet[tuple[ArithmeticExpressionT, ArithmeticExpressionT]],
118+
Set[tuple[ArithmeticExpressionT, ArithmeticExpressionT]],
118119
ArithmeticExpressionT] = {}
119120
for child in expr.children:
120121
term, coeff = self.split_term(child)

pymbolic/mapper/constant_folder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def fold(self,
7070
assert is_arithmetic_expression(child)
7171

7272
if isinstance(child, klass):
73-
assert isinstance(child, (Sum, Product))
73+
assert isinstance(child, Sum | Product)
7474
queue = list(child.children) + queue
7575
else:
7676
if self.is_constant(child):

pymbolic/mapper/dependency.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@
2828
THE SOFTWARE.
2929
"""
3030

31-
from typing import AbstractSet
32-
33-
from typing_extensions import TypeAlias
31+
from collections.abc import Set
32+
from typing import TypeAlias
3433

3534
import pymbolic.primitives as p
3635
from pymbolic.mapper import CachedMapper, Collector, CSECachingMapperMixin, P
3736

3837

39-
DependenciesT: TypeAlias = AbstractSet[p.AlgebraicLeaf | p.CommonSubexpression]
38+
DependenciesT: TypeAlias = Set[p.AlgebraicLeaf | p.CommonSubexpression]
4039

4140

4241
class DependencyMapper(

pymbolic/mapper/differentiator.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def differentiate(expression,
222222
variable,
223223
func_mapper=map_math_functions_by_name,
224224
allowed_nonsmoothness="none"):
225-
if not isinstance(variable, (primitives.Variable, primitives.Subscript)):
225+
if not isinstance(variable, primitives.Variable | primitives.Subscript):
226226
variable = primitives.make_variable(variable)
227227
from pymbolic import flatten
228228
return flatten(DifferentiationMapper(

pymbolic/mapper/optimize.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def wrapper(cls: type) -> type:
302302
if not name.startswith("__") or name == "__call__":
303303
method = getattr(cls, name)
304304
if (not callable(method)
305-
or isinstance(method, (property, cached_property))):
305+
or isinstance(method, property | cached_property)):
306306
# properties don't have *args, **kwargs
307307
continue
308308

pymbolic/mapper/substitutor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3939
THE SOFTWARE.
4040
"""
41-
from typing import Any, Callable
41+
from collections.abc import Callable
42+
from typing import Any
4243

4344
from useful_types import SupportsGetItem, SupportsItems
4445

pymbolic/mapper/unifier.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def treat_mismatch(self, expr, other, urecs):
120120
raise NotImplementedError
121121

122122
def unification_record_from_equation(self, lhs, rhs):
123-
if isinstance(lhs, (tuple, list)) or isinstance(rhs, (tuple, list)):
123+
if isinstance(lhs, tuple | list) or isinstance(rhs, tuple | list):
124124
# Always force lists/tuples to agree elementwise, never
125125
# generate a unification record between them directly.
126126
# This pushes the matching process down to the elementwise

pymbolic/parser.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
THE SOFTWARE.
2626
"""
2727

28+
from collections.abc import Sequence
2829
from sys import intern
29-
from typing import ClassVar, Sequence, Tuple, Union
30+
from typing import ClassVar, TypeAlias
3031

3132
from immutabledict import immutabledict
32-
from typing_extensions import TypeAlias
3333

3434
import pytools.lex
3535
from pytools import memoize_method
@@ -128,7 +128,7 @@ def __hash__(self) -> int: # type: ignore[override]
128128

129129

130130
LexTable: TypeAlias = Sequence[
131-
Tuple[str, Union[pytools.lex.RE, Tuple[Union[str, pytools.lex.RE], ...]]]]
131+
tuple[str, pytools.lex.RE | tuple[str | pytools.lex.RE, ...]]]
132132

133133

134134
class Parser:
@@ -500,15 +500,15 @@ def parse_postfix(self, pstate, min_precedence, left_exp):
500500

501501
pstate.advance()
502502
if pstate.is_at_end() or pstate.next_tag() is _closepar:
503-
if isinstance(left_exp, (tuple, list)) \
503+
if isinstance(left_exp, tuple | list) \
504504
and not isinstance(left_exp, FinalizedContainer):
505505
# left_expr is a container with trailing commas
506506
pass
507507
else:
508508
left_exp = (left_exp,)
509509
else:
510510
new_el = self.parse_expression(pstate, _PREC_COMMA)
511-
if isinstance(left_exp, (tuple, list)) \
511+
if isinstance(left_exp, tuple | list) \
512512
and not isinstance(left_exp, FinalizedContainer):
513513
left_exp = (*left_exp, new_el)
514514
else:

pymbolic/primitives.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,23 @@
2424
"""
2525

2626
import re
27+
from collections.abc import Callable, Iterable, Mapping
2728
from dataclasses import dataclass, fields
2829
from sys import intern
2930
from typing import (
3031
TYPE_CHECKING,
3132
Any,
32-
Callable,
3333
ClassVar,
34-
Iterable,
35-
Mapping,
3634
NoReturn,
3735
Protocol,
38-
Type,
36+
TypeAlias,
3937
TypeVar,
4038
cast,
4139
)
4240
from warnings import warn
4341

4442
from immutabledict import immutabledict
45-
from typing_extensions import TypeAlias, TypeIs, dataclass_transform
43+
from typing_extensions import TypeIs, dataclass_transform
4644

4745
from . import traits
4846
from .typing import ArithmeticExpressionT, ExpressionT, NumberT, ScalarT
@@ -1042,7 +1040,7 @@ def {cls.__name__}_setstate(self, state):
10421040

10431041
# {{{ assign mapper_method
10441042

1045-
mm_cls = cast(Type[_HasMapperMethod], cls)
1043+
mm_cls = cast(type[_HasMapperMethod], cls)
10461044

10471045
snake_clsname = _CAMEL_TO_SNAKE_RE.sub("_", mm_cls.__name__).lower()
10481046
default_mapper_method_name = f"map_{snake_clsname}"
@@ -1916,7 +1914,7 @@ def is_zero(value: object) -> bool:
19161914

19171915

19181916
def wrap_in_cse(expr: ExpressionT, prefix=None) -> ExpressionT:
1919-
if isinstance(expr, (Variable, Subscript)):
1917+
if isinstance(expr, Variable | Subscript):
19201918
return expr
19211919

19221920
if isinstance(expr, CommonSubexpression):

pymbolic/traits.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def traits(x):
4040
try:
4141
return x.traits()
4242
except AttributeError:
43-
if isinstance(x, (complex, float)):
43+
if isinstance(x, complex | float):
4444
return FieldTraits()
4545
elif isinstance(x, int):
4646
return IntegerTraits()

0 commit comments

Comments
 (0)