Skip to content

Commit 1b257b7

Browse files
committed
Enable flake8-type-checking
1 parent 03f3615 commit 1b257b7

26 files changed

+102
-59
lines changed

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
('py:class', 'OptionDummy'), # undocumented
175175
('py:class', 'setuptools.dist.Distribution'), # undocumented
176176
('py:class', 'UnixCCompiler'), # undocumented
177+
('py:class', 'StrPath'), # type import
177178
('py:exc', 'CompileError'), # undocumented
178179
('py:exc', 'DistutilsExecError'), # undocumented
179180
('py:exc', 'DistutilsFileError'), # undocumented

ruff.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ extend-select = [
3636
"TRY", # tryceratops
3737
"UP", # pyupgrade
3838
"YTT", # flake8-2020
39+
# Helps prevent circular imports, reduce runtime cost of typing symbols,
40+
# and prevent leaking implementations details into modules
41+
"TC", # flake8-type-checking
3942
]
4043
ignore = [
4144
# upstream
@@ -67,14 +70,14 @@ ignore = [
6770
"UP015", # redundant-open-modes, explicit is preferred
6871
# Only enforcing return type annotations for public functions
6972
"ANN202", # missing-return-type-private-function
73+
# stdlib is not at risk of circular import, is clearly not public API,
74+
# and assume it's gonna be included in the import chain at some point anyway
75+
"TC003", # typing-only-standard-library-import
7076
]
7177

7278
[lint.per-file-ignores]
7379
# Suppress nuisance warnings about module-import-not-at-top-of-file (E402) due to workaround for #4476
7480
"setuptools/__init__.py" = ["E402"]
75-
# pkg_resources is due for removal, not worth fixing existing errors
76-
"pkg_resources/__init__.py" = ["E402", "ANN204"]
77-
"pkg_resources/tests/test_resources.py" = ["PT031"]
7881

7982
[lint.isort]
8083
combine-as-imports = true
@@ -89,6 +92,9 @@ sections.delayed = ["distutils"]
8992
[lint.flake8-annotations]
9093
ignore-fully-untyped = true
9194

95+
[lint.flake8-type-checking]
96+
quote-annotations = true
97+
9298
[format]
9399
# Enable preview to get hugged parenthesis unwrapping and other nice surprises
94100
# See https://github.com/jaraco/skeleton/pull/133#issuecomment-2239538373

setuptools/command/_requirestxt.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
from collections import defaultdict
1414
from collections.abc import Mapping
1515
from itertools import filterfalse
16-
from typing import TypeVar
16+
from typing import TYPE_CHECKING, TypeVar
1717

1818
from jaraco.text import yield_lines
1919
from packaging.requirements import Requirement
2020

2121
from .. import _reqs
22-
from .._reqs import _StrOrIter
22+
23+
if TYPE_CHECKING:
24+
from .._reqs import _StrOrIter
2325

2426
# dict can work as an ordered set
2527
_T = TypeVar("_T")

setuptools/command/bdist_rpm.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
from ..dist import Distribution
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
25
from ..warnings import SetuptoolsDeprecationWarning
36

47
import distutils.command.bdist_rpm as orig
58

9+
if TYPE_CHECKING:
10+
from ..dist import Distribution
11+
612

713
class bdist_rpm(orig.bdist_rpm):
814
"""

setuptools/command/bdist_wheel.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from collections.abc import Iterable, Sequence
1717
from email.generator import BytesGenerator
1818
from glob import iglob
19-
from typing import Literal, cast
19+
from typing import TYPE_CHECKING, Literal, cast
2020
from zipfile import ZIP_DEFLATED, ZIP_STORED
2121

2222
from packaging import tags, version as _packaging_version
@@ -26,10 +26,12 @@
2626
from .._core_metadata import _safe_license_file
2727
from .._normalization import safer_name
2828
from ..warnings import SetuptoolsDeprecationWarning
29-
from .egg_info import egg_info as egg_info_cls
3029

3130
from distutils import log
3231

32+
if TYPE_CHECKING:
33+
from .egg_info import egg_info as egg_info_cls
34+
3335

3436
def safe_version(version: str) -> str:
3537
"""
@@ -233,7 +235,9 @@ def finalize_options(self) -> None:
233235
self.bdist_dir = os.path.join(bdist_base, "wheel")
234236

235237
if self.dist_info_dir is None:
236-
egg_info = cast(egg_info_cls, self.distribution.get_command_obj("egg_info"))
238+
egg_info = cast(
239+
"egg_info_cls", self.distribution.get_command_obj("egg_info")
240+
)
237241
egg_info.ensure_finalized() # needed for correct `wheel_dist_name`
238242

239243
self.data_dir = self.wheel_dist_name + ".data"
@@ -492,7 +496,7 @@ def license_paths(self) -> Iterable[str]:
492496
metadata = self.distribution.get_option_dict("metadata")
493497
if setuptools_major_version >= 42:
494498
# Setuptools recognizes the license_files option but does not do globbing
495-
patterns = cast(Sequence[str], self.distribution.metadata.license_files)
499+
patterns = cast("Sequence[str]", self.distribution.metadata.license_files)
496500
else:
497501
# Prior to those, wheel is entirely responsible for handling license files
498502
if "license_files" in metadata:

setuptools/command/build.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import annotations
22

3-
from typing import Protocol
4-
5-
from ..dist import Distribution
3+
from typing import TYPE_CHECKING, Protocol
64

75
from distutils.command.build import build as _build
86

7+
if TYPE_CHECKING:
8+
from ..dist import Distribution
9+
910
_ORIGINAL_SUBCOMMANDS = {"build_py", "build_clib", "build_ext", "build_scripts"}
1011

1112

setuptools/command/build_clib.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
from ..dist import Distribution
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
25
from ..modified import newer_pairwise_group
36

47
import distutils.command.build_clib as orig
58
from distutils import log
69
from distutils.errors import DistutilsSetupError
710

11+
if TYPE_CHECKING:
12+
from ..dist import Distribution
13+
814

915
class build_clib(orig.build_clib):
1016
"""

setuptools/command/build_ext.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from pathlib import Path
1212
from typing import TYPE_CHECKING
1313

14-
from setuptools.dist import Distribution
1514
from setuptools.errors import BaseError
1615
from setuptools.extension import Extension, Library
1716

@@ -20,6 +19,8 @@
2019
from distutils.sysconfig import customize_compiler, get_config_var
2120

2221
if TYPE_CHECKING:
22+
from setuptools.dist import Distribution
23+
2324
# Cython not installed on CI tests, causing _build_ext to be `Any`
2425
from distutils.command.build_ext import build_ext as _build_ext
2526
else:

setuptools/command/build_py.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@
1010
from functools import partial
1111
from glob import glob
1212
from pathlib import Path
13-
from typing import Any
13+
from typing import TYPE_CHECKING, Any
1414

1515
from more_itertools import unique_everseen
1616

17-
from .._path import StrPath, StrPathT
18-
from ..dist import Distribution
1917
from ..warnings import SetuptoolsDeprecationWarning
2018

2119
import distutils.command.build_py as orig
2220
import distutils.errors
2321
from distutils.util import convert_path
2422

23+
if TYPE_CHECKING:
24+
from .._path import StrPath, StrPathT
25+
from ..dist import Distribution
26+
2527
_IMPLICIT_DATA_FILES = ('*.pyi', 'py.typed')
2628

2729

setuptools/command/develop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class develop(Command):
3131
def run(self) -> None:
3232
# Casting because mypy doesn't understand bool mult conditionals
3333
cmd = cast(
34-
list[str],
34+
"list[str]",
3535
[sys.executable, '-m', 'pip', 'install', '-e', '.', '--use-pep517']
3636
+ ['--target', self.install_dir] * bool(self.install_dir)
3737
+ ['--no-deps'] * self.no_deps

0 commit comments

Comments
 (0)