Skip to content

Commit e204bfa

Browse files
author
Release Manager
committed
gh-41108: Simplify typing annotations (ruff UP006) <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes #12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes #12345". --> Replace things like `List[T]` with `list[T]` in type annotations. Enables this rule in CI and in configuration files. To do this I used ruff to fix [UP006](https://docs.astral.sh/ruff/rules/non-pep585-annotation/), then fixed F401 on the changed files (to remove the UP006 unused imports left behind by UP006). There are several other ruff UP rules that have autofix and can easily be enabled. To keep the PR a reasonable size we only do UP006 here. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. URL: #41108 Reported by: Vincent Macri Reviewer(s): Tobias Diez, Vincent Macri
2 parents 589e999 + 4e3f7c9 commit e204bfa

26 files changed

+153
-161
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
if: (success() || failure()) && steps.deps.outcome == 'success'
3636
run: |
3737
uv run --frozen --only-group lint -- ruff check --output-format github --ignore E402,E721,E731,E741,E742,E743,F401,F402,F403,F405,F821,F841,I001,PLC0206,PLC0208,PLC1802,PLC2401,PLC3002,PLC0415,PLE0302,PLR0124,PLR0402,PLR0911,PLR0912,PLR0913,PLR0915,PLR1704,PLR1711,PLR1714,PLR1716,PLR1733,PLR1736,PLR2004,PLR5501,PLW0120,PLW0211,PLW0602,PLW0603,PLW0642,PLW1508,PLW1510,PLW1641,PLW2901,PLW3301
38-
uv run --frozen --only-group lint -- ruff check --output-format github --preview --select E111,E115,E21,E221,E222,E225,E227,E228,E25,E271,E272,E275,E302,E303,E305,E306,E401,E502,E701,E702,E703,E71,W291,W293,W391,W605,TC src/sage/
38+
uv run --frozen --only-group lint -- ruff check --output-format github --preview --select E111,E115,E21,E221,E222,E225,E227,E228,E25,E271,E272,E275,E302,E303,E305,E306,E401,E502,E701,E702,E703,E71,W291,W293,W391,W605,TC,UP006 src/sage/
3939
4040
- name: Code style check with relint
4141
if: (success() || failure()) && steps.deps.outcome == 'success'

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ select = [
250250
"I", # isort - https://docs.astral.sh/ruff/rules/#isort-i
251251
"PL", # pylint - https://docs.astral.sh/ruff/rules/#pylint-pl
252252
"TC", # flake8-type-checking - https://docs.astral.sh/ruff/rules/#flake8-type-checking-tc
253+
"UP006", # pyupgrade -- https://docs.astral.sh/ruff/rules/#pyupgrade-up
253254
]
254255

255256
[tool.ruff.lint.per-file-ignores]

src/sage/misc/abstract_method.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Callable, Dict, List, Type, Union
1+
from typing import Callable, Union
22

33
def abstract_method(f: Callable = None, optional: bool = False) -> Callable:
44
...
@@ -13,11 +13,11 @@ class AbstractMethod:
1313
def _sage_src_lines_(self) -> Union[str, int]:
1414
...
1515

16-
def __get__(self, instance: object, cls: Type) -> Union[Callable, NotImplementedError]:
16+
def __get__(self, instance: object, cls: type) -> Union[Callable, NotImplementedError]:
1717
...
1818

1919
def is_optional(self) -> bool:
2020
...
2121

22-
def abstract_methods_of_class(cls: Type) -> Dict[str, List[str]]:
22+
def abstract_methods_of_class(cls: type) -> dict[str, list[str]]:
2323
...

src/sage/misc/binary_tree.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, List
1+
from typing import Optional
22

33
class binary_tree_node:
44
key: int
@@ -39,7 +39,7 @@ LIST_INORDER: int
3939
LIST_KEYS: int
4040
LIST_VALUES: int
4141

42-
def binary_tree_list(node: binary_tree_node, behavior: int) -> List[object]:
42+
def binary_tree_list(node: binary_tree_node, behavior: int) -> list[object]:
4343
...
4444

4545
class BinaryTree:
@@ -78,10 +78,10 @@ class BinaryTree:
7878
def is_empty(self) -> bool:
7979
...
8080

81-
def keys(self, order: str = 'inorder') -> List[int]:
81+
def keys(self, order: str = 'inorder') -> list[int]:
8282
...
8383

84-
def values(self, order: str = 'inorder') -> List[object]:
84+
def values(self, order: str = 'inorder') -> list[object]:
8585
...
8686

8787
def _headkey_(self) -> int:

src/sage/misc/c3.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from typing import List
21

3-
def C3_algorithm(start: object, bases: str, attribute: str, proper: bool) -> List[object]:
2+
def C3_algorithm(start: object, bases: str, attribute: str, proper: bool) -> list[object]:
43
...

src/sage/misc/cachefunc.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable, Dict, Tuple
1+
from typing import Any, Callable
22

33
def dict_key(o: Any) -> Any:
44
...
@@ -45,7 +45,7 @@ class CachedMethod:
4545
def __call__(self, inst: Any, *args: Any, **kwds: Any) -> Any:
4646
...
4747

48-
def _get_instance_cache(self, inst: Any) -> Dict:
48+
def _get_instance_cache(self, inst: Any) -> dict:
4949
...
5050

5151
def __get__(self, inst: Any, cls: Any) -> Any:
@@ -60,15 +60,15 @@ class CachedInParentMethod(CachedMethod):
6060
do_pickle: bool | None = None) -> None:
6161
...
6262

63-
def _get_instance_cache(self, inst: Any) -> Dict:
63+
def _get_instance_cache(self, inst: Any) -> dict:
6464
...
6565

6666
def __get__(self, inst: Any, cls: Any) -> Any:
6767
...
6868

6969
class CachedMethodCaller(CachedFunction):
7070
def __init__(self, cachedmethod: CachedMethod, inst: Any,
71-
cache: Dict | None = None, name: str | None = None,
71+
cache: dict | None = None, name: str | None = None,
7272
key: Callable | None = None,
7373
do_pickle: bool | None = None) -> None:
7474
...
@@ -113,5 +113,5 @@ class CachedMethodCallerNoArgs(CachedFunction):
113113
...
114114

115115
class GloballyCachedMethodCaller(CachedMethodCaller):
116-
def get_key_args_kwds(self, args: Tuple, kwds: Dict) -> Any:
116+
def get_key_args_kwds(self, args: tuple, kwds: dict) -> Any:
117117
...

src/sage/misc/citation.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from typing import List
21

3-
def get_systems(cmd: str) -> List[str]:
2+
def get_systems(cmd: str) -> list[str]:
43
...
54

65
def cython_profile_enabled() -> bool:

src/sage/rings/abc.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Type, Union, Callable, Dict, List
1+
from typing import Union, Callable
22

33
def abc(f: Callable = None, optional: bool = False) -> Callable:
44
...
@@ -13,11 +13,11 @@ class ABC:
1313
def _sage_src_lines_(self) -> Union[str, int]:
1414
...
1515

16-
def __get__(self, instance: object, cls: Type) -> Union[Callable, NotImplementedError]:
16+
def __get__(self, instance: object, cls: type) -> Union[Callable, NotImplementedError]:
1717
...
1818

1919
def is_optional(self) -> bool:
2020
...
2121

22-
def abstract_methods_of_class(cls: Type) -> Dict[str, List[str]]:
22+
def abstract_methods_of_class(cls: type) -> dict[str, list[str]]:
2323
...

src/sage/rings/bernoulli_mod_p.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import List
21

3-
def verify_bernoulli_mod_p(data: List[int]) -> bool:
2+
def verify_bernoulli_mod_p(data: list[int]) -> bool:
43
...
54

6-
def bernoulli_mod_p(p: int) -> List[int]:
5+
def bernoulli_mod_p(p: int) -> list[int]:
76
...
87

98
def bernoulli_mod_p_single(p: int, k: int) -> int:

src/sage/rings/factorint.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import List, Tuple
21

3-
def aurifeuillian(n: int, m: int, F: int = None, check: bool = True) -> List[int]:
2+
def aurifeuillian(n: int, m: int, F: int = None, check: bool = True) -> list[int]:
43
...
54

6-
def factor_aurifeuillian(n: int, check: bool = True) -> List[int]:
5+
def factor_aurifeuillian(n: int, check: bool = True) -> list[int]:
76
...
87

98
def factor_cunningham(m: int, proof: bool = None) -> int:

0 commit comments

Comments
 (0)