Skip to content

Commit 5007388

Browse files
committed
config(ruff): py3.9+
Signed-off-by: nstarman <[email protected]>
1 parent ffb528d commit 5007388

22 files changed

+87
-88
lines changed

benchmark.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Tuple
2-
31
import numpy as np
42
from tests.util import benchmark
53

@@ -35,12 +33,12 @@ def f2(x):
3533

3634

3735
@dispatch
38-
def g2(x: Tuple[int]):
36+
def g2(x: tuple[int]):
3937
pass
4038

4139

4240
@dispatch
43-
def g2(x: Tuple[str]):
41+
def g2(x: tuple[str]):
4442
pass
4543

4644

check_linter_assertions.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
from collections import defaultdict
55
from collections.abc import Callable
66
from pathlib import Path
7-
from typing import Dict, List, Tuple
87

9-
FileLineInfo = Dict[Path, Dict[int, List[str]]]
8+
FileLineInfo = dict[Path, dict[int, list[str]]]
109
"""type: Type of a nested dictionary that gives for a collection of files line-wise
1110
information, where the information is of the form `list[str]`."""
1211

1312

14-
def next_noncomment_line(index: int, lines: List[str], path: Path) -> int:
13+
def next_noncomment_line(index: int, lines: list[str], path: Path) -> int:
1514
"""Starting at `index`, find the next line with code.
1615
1716
Args:
@@ -71,7 +70,7 @@ def parse_assertions(source_dir: Path, linter: str) -> FileLineInfo:
7170
return asserted_errors
7271

7372

74-
def parse_mypy_line(line: str) -> Tuple[Path, int, str, str]:
73+
def parse_mypy_line(line: str) -> tuple[Path, int, str, str]:
7574
"""Parse a line of the output of `mypy`.
7675
7776
Args:
@@ -91,7 +90,7 @@ def parse_mypy_line(line: str) -> Tuple[Path, int, str, str]:
9190
return Path(path).resolve(), int(line_number), status, message
9291

9392

94-
def parse_pyright_line(line: str) -> Tuple[Path, int, str, str]:
93+
def parse_pyright_line(line: str) -> tuple[Path, int, str, str]:
9594
"""Parse a line of the output of `pyright`.
9695
9796
Args:
@@ -113,7 +112,7 @@ def parse_pyright_line(line: str) -> Tuple[Path, int, str, str]:
113112
return Path(path.strip()).resolve(), int(line_number), status, message
114113

115114

116-
parse_line: Dict[str, Callable[[str], Tuple[Path, int, str, str]]] = {
115+
parse_line: dict[str, Callable[[str], tuple[Path, int, str, str]]] = {
117116
"mypy": parse_mypy_line,
118117
"pyright": parse_pyright_line,
119118
}

docs/types.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ that all types and type hints supported by Beartype are also supported by Plum.
88
Here are a few examples:
99

1010
```python
11-
from typing import Union, Optional, List, Dict
11+
from typing import Union, Optional
1212

1313
from plum import dispatch
1414

@@ -34,7 +34,7 @@ def f(x: list) -> str:
3434

3535

3636
@dispatch
37-
def f(x: List[int]) -> str:
37+
def f(x: list[int]) -> str:
3838
return "list of int"
3939

4040

@@ -44,11 +44,11 @@ def f(x: Optional[dict]) -> Optional[str]:
4444

4545

4646
@dispatch
47-
def f(x: Dict[int, str]) -> str:
47+
def f(x: dict[int, str]) -> str:
4848
return "dict of int to str"
4949
```
5050

51-
Although parametric types such as `List[int]` and `Dict[int, str]` are fully
51+
Although parametric types such as `list[int]` and `dict[int, str]` are fully
5252
supported, they do incur a performance penalty.
5353
For optimal performance, is recommended to use parametric types only where necessary.
5454
`Union` and `Optional` do not incur a performance penalty.
@@ -85,7 +85,7 @@ Plum already opts into this behaviour and will use it once it becomes available.
8585

8686
The type system is *covariant*, as opposed to Julia's type
8787
system, which is *invariant*.
88-
For example, this means that `List[T1]` is a subtype of `List[T2]` whenever
88+
For example, this means that `list[T1]` is a subtype of `list[T2]` whenever
8989
`T1` is a subtype of `T2`.
9090

9191
## Performance and Faithful Types

plum/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# Plum previously exported a number of types. As of recently, the user can use the
2-
# versions from `typing`. To not break backward compatibility, we still export these
3-
# types.
1+
# Plum previously exported a number of types. As of recently, the user can use
2+
# the versions from `typing`. To not break backward compatibility, we still
3+
# export these types.
4+
from typing import Dict, List, Tuple, Union # noqa: F401, UP035
5+
6+
# isort: split
47
from functools import partial
5-
from typing import Dict, List, Tuple, Union # noqa: F401
68

79
from beartype import (
810
BeartypeConf as _BeartypeConf,

plum/alias.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"""
2828

2929
from functools import wraps
30-
from typing import List, TypeVar, Union, _type_repr, get_args
30+
from typing import TypeVar, Union, _type_repr, get_args
3131

3232
__all__ = ["activate_union_aliases", "deactivate_union_aliases", "set_union_alias"]
3333

@@ -139,7 +139,7 @@ def deactivate_union_aliases() -> None:
139139
_union_type.__str__ = _original_str
140140

141141

142-
_ALIASED_UNIONS: List = []
142+
_ALIASED_UNIONS: list = []
143143

144144

145145
def set_union_alias(union: UnionT, alias: str) -> UnionT:

plum/dispatcher.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import sys
22
from dataclasses import dataclass, field
33
from functools import partial
4-
from typing import Any, Dict, Optional, Tuple, TypeVar, Union, overload
4+
from typing import Any, Optional, TypeVar, Union, overload
55

66
from .function import Function
77
from .overload import get_overloads
@@ -13,7 +13,7 @@
1313
T = TypeVar("T", bound=Callable[..., Any])
1414

1515

16-
_dataclass_kw_args: Dict[str, Any] = {}
16+
_dataclass_kw_args: dict[str, Any] = {}
1717
if sys.version_info >= (3, 10): # pragma: specific no cover 3.9
1818
_dataclass_kw_args |= {"slots": True}
1919

@@ -34,8 +34,8 @@ class Dispatcher:
3434
"""
3535

3636
warn_redefinition: bool = False
37-
functions: Dict[str, Function] = field(default_factory=dict)
38-
classes: Dict[str, Dict[str, Function]] = field(default_factory=dict)
37+
functions: dict[str, Function] = field(default_factory=dict)
38+
classes: dict[str, dict[str, Function]] = field(default_factory=dict)
3939

4040
@overload
4141
def __call__(self, method: T, /, *, precedence: int = ...) -> T: ...
@@ -74,7 +74,7 @@ def __call__(
7474
return self._add_method(method, None, precedence=precedence)
7575

7676
def multi(
77-
self, *signatures: Union[Signature, Tuple[TypeHint, ...]]
77+
self, *signatures: Union[Signature, tuple[TypeHint, ...]]
7878
) -> Callable[[Callable], Function]:
7979
"""Decorator to register multiple signatures at once.
8080

plum/function.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from copy import copy
44
from functools import wraps
55
from types import MethodType
6-
from typing import Any, Callable, List, Optional, Protocol, Tuple, TypeVar, Union
6+
from typing import Any, Callable, Optional, Protocol, TypeVar, Union
77

88
from .method import Method
99
from .resolver import AmbiguousLookupError, NotFoundLookupError, Resolver
@@ -96,12 +96,12 @@ def __init__(
9696
self._warn_redefinition = warn_redefinition
9797

9898
# Initialise pending and resolved methods.
99-
self._pending: List[Tuple[Callable, Optional[Signature], int]] = []
99+
self._pending: list[tuple[Callable, Optional[Signature], int]] = []
100100
self._resolver = Resolver(
101101
self.__name__,
102102
warn_redefinition=self._warn_redefinition,
103103
)
104-
self._resolved: List[Tuple[Callable, Signature, int]] = []
104+
self._resolved: list[tuple[Callable, Signature, int]] = []
105105

106106
@property
107107
def owner(self):
@@ -176,7 +176,7 @@ def __doc__(self, value: str) -> None:
176176
self._doc = value if value else ""
177177

178178
@property
179-
def methods(self) -> List[Signature]:
179+
def methods(self) -> list[Signature]:
180180
"""list[:class:`.signature.Signature`]: All available methods."""
181181
self._resolve_pending_registrations()
182182
return self._resolver.methods
@@ -199,7 +199,7 @@ def dispatch(
199199
return self
200200

201201
def dispatch_multi(
202-
self: Self, *signatures: Union[Signature, Tuple[TypeHint, ...]]
202+
self: Self, *signatures: Union[Signature, tuple[TypeHint, ...]]
203203
) -> Callable[[Callable], Self]:
204204
"""Decorator to extend the function with multiple signatures at once.
205205
@@ -296,8 +296,8 @@ def _resolve_pending_registrations(self) -> None:
296296
self.clear_cache(reregister=False)
297297

298298
def resolve_method(
299-
self, target: Union[Tuple[object, ...], Signature]
300-
) -> Tuple[Callable, TypeHint]:
299+
self, target: Union[tuple[object, ...], Signature]
300+
) -> tuple[Callable, TypeHint]:
301301
"""Find the method and return type for arguments.
302302
303303
Args:
@@ -336,7 +336,7 @@ def resolve_method(
336336

337337
def _handle_not_found_lookup_error(
338338
self, ex: NotFoundLookupError
339-
) -> Tuple[Callable, TypeHint]:
339+
) -> tuple[Callable, TypeHint]:
340340
if not self.owner:
341341
# Not in a class. Nothing we can do.
342342
raise ex from None
@@ -384,9 +384,9 @@ def __call__(self, *args, **kw_args):
384384

385385
def _resolve_method_with_cache(
386386
self,
387-
args: Union[Tuple[object, ...], Signature, None] = None,
388-
types: Optional[Tuple[TypeHint, ...]] = None,
389-
) -> Tuple[Callable, TypeHint]:
387+
args: Union[tuple[object, ...], Signature, None] = None,
388+
types: Optional[tuple[TypeHint, ...]] = None,
389+
) -> tuple[Callable, TypeHint]:
390390
if args is None and types is None:
391391
raise ValueError(
392392
"Arguments `args` and `types` cannot both be `None`. "
@@ -525,7 +525,7 @@ def wrapped_method(*args, **kw_args):
525525
return wrapped_method
526526

527527
@property
528-
def methods(self) -> List[Signature]:
528+
def methods(self) -> list[Signature]:
529529
"""list[:class:`.signature.Signature`]: All available methods."""
530530
return self._f.methods
531531

plum/method.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import inspect
22
import typing
33
from collections.abc import Callable
4-
from typing import List, Optional, Set, Tuple
4+
from typing import Optional
55

66
from rich.padding import Padding
77
from rich.text import Text
@@ -97,7 +97,7 @@ def __rich_console__(self, console, options):
9797

9898
def repr_mismatch(
9999
self,
100-
mismatches: Set[int] = frozenset(),
100+
mismatches: set[int] = frozenset(),
101101
varargs_matched: bool = True,
102102
) -> str:
103103
"""Version of `__repr__` that can print which arguments are mismatched. This
@@ -169,7 +169,7 @@ def __rich_console__(self, console, options):
169169
yield Padding(sum(method_repr, Text(f"[{i}] ")), (0, 4))
170170

171171

172-
def extract_arg_names(f: Callable) -> Tuple[List[str], List[str], Optional[str]]:
172+
def extract_arg_names(f: Callable) -> tuple[list[str], list[str], Optional[str]]:
173173
"""Extract the argument names for a function.
174174
175175
Args:

plum/parametric.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import contextlib
2-
from typing import Type, TypeVar, Union
2+
from typing import TypeVar, Union
33

44
from typing_extensions import deprecated
55

@@ -472,7 +472,7 @@ def type_parameter(x):
472472
)
473473

474474

475-
def type_nonparametric(q: T) -> Type[T]:
475+
def type_nonparametric(q: T) -> type[T]:
476476
"""Return the non-parametric type of an object.
477477
478478
:mod:`plum.parametric` produces parametric subtypes of classes. This method
@@ -540,7 +540,7 @@ def type_nonparametric(q: T) -> Type[T]:
540540
)
541541

542542

543-
def type_unparametrized(q: T) -> Type[T]:
543+
def type_unparametrized(q: T) -> type[T]:
544544
"""Return the unparametrized type of an object.
545545
546546
:mod:`plum.parametric` produces parametric subtypes of classes. This

plum/promotion.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"promote",
99
]
1010

11-
from typing import TYPE_CHECKING, Any, Callable, Protocol, Type, TypeVar
11+
from typing import TYPE_CHECKING, Any, Callable, Protocol, TypeVar
1212

1313
from typing_extensions import TypeAlias
1414

@@ -65,8 +65,8 @@ def __call__(self, obj: T) -> R: ...
6565

6666

6767
def add_conversion_method(
68-
type_from: Type[T],
69-
type_to: Type[R],
68+
type_from: type[T],
69+
type_to: type[R],
7070
f: _ConversionCallable[T, R],
7171
) -> None:
7272
"""Add a conversion method to convert an object from one type to another.
@@ -84,7 +84,7 @@ def perform_conversion(obj: type_from, _: type_to):
8484

8585

8686
def conversion_method(
87-
type_from: Type[T], type_to: Type[R]
87+
type_from: type[T], type_to: type[R]
8888
) -> Callable[[_ConversionCallable[T, R]], _ConversionCallable[T, R]]:
8989
"""Decorator to add a conversion method to convert an object from one
9090
type to another.

0 commit comments

Comments
 (0)