Skip to content

Commit f911c0b

Browse files
author
Release Manager
committed
gh-40799: Move typing imports to TYPE_CHECKING block and enable relevant ruff checks <!-- ^ 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". --> Plus some related import cleanups. This avoid importing modules that are only imported for type checking at runtime. This should speed up how long the modules take load at runtime, but I didn't measure it. This will be more important as more of the Sage codebase gets type annotations over time. ### 📝 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: #40799 Reported by: Vincent Macri Reviewer(s): Tobias Diez, Vincent Macri
2 parents 2c1f272 + 8714565 commit f911c0b

File tree

27 files changed

+83
-40
lines changed

27 files changed

+83
-40
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 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 src/sage/
3939
4040
- name: Code style check with relint
4141
if: (success() || failure()) && steps.deps.outcome == 'success'

conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import inspect
1212
import sys
1313
import warnings
14-
from pathlib import Path
15-
from typing import Any, Iterable, Optional
14+
from typing import Any, Iterable, Optional, TYPE_CHECKING
1615

1716
import pytest
1817
from _pytest.doctest import (
@@ -32,6 +31,9 @@
3231
)
3332
from sage.doctest.parsing import SageDocTestParser, SageOutputChecker
3433

34+
if TYPE_CHECKING:
35+
from pathlib import Path
36+
3537

3638
class SageDoctestModule(DoctestModule):
3739
"""

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ select = [
241241
"F", # pyflakes - https://docs.astral.sh/ruff/rules/#pyflakes-f
242242
"I", # isort - https://docs.astral.sh/ruff/rules/#isort-i
243243
"PL", # pylint - https://docs.astral.sh/ruff/rules/#pylint-pl
244+
"TC", # flake8-type-checking - https://docs.astral.sh/ruff/rules/#flake8-type-checking-tc
244245
]
245246

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

src/sage/combinat/cluster_algebra_quiver/mutation_type.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
from copy import copy
2424
from pathlib import Path
2525
import pickle
26-
from typing import Any, Iterator
26+
from typing import Any
27+
from collections.abc import Iterator
2728

2829

2930
from sage.misc.cachefunc import cached_function

src/sage/combinat/combinat.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
# ****************************************************************************
124124
from __future__ import annotations
125125

126-
from collections.abc import Iterator
127126

128127
from sage.arith.misc import bernoulli, factorial
129128
from sage.combinat.combinat_cython import _stirling_number2
@@ -138,6 +137,10 @@
138137
from sage.rings.rational_field import QQ
139138
from sage.structure.element import Element
140139
from sage.structure.sage_object import SageObject
140+
from typing import TYPE_CHECKING
141+
142+
if TYPE_CHECKING:
143+
from collections.abc import Iterator
141144

142145
lazy_import('sage.interfaces.maxima_lib', 'maxima')
143146
lazy_import('sage.libs.pari', 'pari')

src/sage/combinat/dyck_word.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
# https://www.gnu.org/licenses/
7878
# ****************************************************************************
7979
from __future__ import annotations
80-
from collections.abc import Iterator
8180

8281
from .combinat import CombinatorialElement, catalan_number
8382
from sage.combinat.combinatorial_map import combinatorial_map
@@ -97,6 +96,10 @@
9796
from sage.combinat.set_partition import SetPartitions
9897
from sage.misc.latex import latex
9998
from sage.misc.lazy_import import lazy_import
99+
from typing import TYPE_CHECKING
100+
101+
if TYPE_CHECKING:
102+
from collections.abc import Iterator
100103

101104
lazy_import('sage.combinat.alternating_sign_matrix', 'AlternatingSignMatrices')
102105

src/sage/combinat/interval_posets.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
# https://www.gnu.org/licenses/
3131
# ****************************************************************************
3232
from __future__ import annotations
33-
from collections.abc import Iterator
3433

3534
from sage.categories.enumerated_sets import EnumeratedSets
3635
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
@@ -58,6 +57,10 @@
5857
from sage.structure.richcmp import richcmp, op_NE, op_EQ
5958
from sage.structure.unique_representation import UniqueRepresentation
6059
from sage.graphs.digraph import DiGraph
60+
from typing import TYPE_CHECKING
61+
62+
if TYPE_CHECKING:
63+
from collections.abc import Iterator
6164

6265
lazy_import('sage.combinat.dyck_word', 'DyckWords')
6366

src/sage/combinat/parking_functions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
# https://www.gnu.org/licenses/
6464
# ****************************************************************************
6565
from __future__ import annotations
66-
from collections.abc import Iterator
6766

6867
from sage.rings.integer import Integer
6968
from sage.rings.rational_field import QQ
@@ -80,6 +79,10 @@
8079
from sage.categories.sets_with_grading import SetsWithGrading
8180
from sage.structure.parent import Parent
8281
from sage.structure.unique_representation import UniqueRepresentation
82+
from typing import TYPE_CHECKING
83+
84+
if TYPE_CHECKING:
85+
from collections.abc import Iterator
8386

8487

8588
def is_a(x, n=None) -> bool:

src/sage/combinat/permutation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,9 @@
239239
# https://www.gnu.org/licenses/
240240
# ****************************************************************************
241241
from __future__ import annotations
242-
from collections.abc import Iterator
243242
import itertools
244243
import operator
245-
from typing import Iterable
244+
from typing import TYPE_CHECKING
246245

247246
from sage.arith.misc import factorial, multinomial
248247
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
@@ -271,6 +270,9 @@
271270
from sage.structure.element import Element, get_coercion_model
272271
from sage.structure.unique_representation import UniqueRepresentation
273272

273+
if TYPE_CHECKING:
274+
from collections.abc import Iterable, Iterator
275+
274276
lazy_import('sage.combinat.rsk', ['RSK', 'RSK_inverse'])
275277
lazy_import('sage.combinat.tableau', 'Tableau')
276278
lazy_import('sage.combinat.words.finite_word', 'evaluation_dict')

src/sage/combinat/plane_partition.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
# ****************************************************************************
2626

2727
from __future__ import annotations
28-
from typing import NewType
29-
from collections.abc import Iterator
28+
from typing import NewType, TYPE_CHECKING
3029

3130
from sage.structure.richcmp import richcmp, richcmp_method
3231
from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
@@ -44,6 +43,9 @@
4443
from sage.sets.family import Family
4544
from sage.sets.non_negative_integers import NonNegativeIntegers
4645

46+
if TYPE_CHECKING:
47+
from collections.abc import Iterator
48+
4749
lazy_import('sage.modules.free_module_element', 'vector')
4850

4951

0 commit comments

Comments
 (0)