Skip to content

Commit 6079542

Browse files
authored
Type-check on all Python versions (#4352)
2 parents 4cc8ad3 + 6452a6b commit 6079542

File tree

9 files changed

+59
-31
lines changed

9 files changed

+59
-31
lines changed

Diff for: mypy.ini

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[mypy]
22
# CI should test for all versions, local development gets hints for oldest supported
3-
# Some upstream typeshed distutils stubs fixes are necessary before we can start testing on Python 3.12
4-
python_version = 3.8
3+
# But our testing setup doesn't allow passing CLI arguments, so local devs have to set this manually.
4+
# python_version = 3.8
55
strict = False
66
warn_unused_ignores = True
77
warn_redundant_casts = True
@@ -30,15 +30,19 @@ disable_error_code = attr-defined
3030
[mypy-pkg_resources.tests.*]
3131
disable_error_code = import-not-found
3232

33-
# - distutils._modified has different errors on Python 3.8 [import-untyped], on Python 3.9+ [import-not-found]
33+
# - distutils doesn't exist on Python 3.12, unfortunately, this means typing
34+
# will be missing for subclasses of distutils on Python 3.12 until either:
35+
# - support for `SETUPTOOLS_USE_DISTUTILS=stdlib` is dropped (#3625)
36+
# for setuptools to import `_distutils` directly
37+
# - or non-stdlib distutils typings are exposed
3438
# - All jaraco modules are still untyped
3539
# - _validate_project sometimes complains about trove_classifiers (#4296)
3640
# - wheel appears to be untyped
37-
[mypy-distutils._modified,jaraco.*,trove_classifiers,wheel.*]
41+
[mypy-distutils.*,jaraco.*,trove_classifiers,wheel.*]
3842
ignore_missing_imports = True
3943

4044
# Even when excluding a module, import issues can show up due to following import
4145
# https://github.com/python/mypy/issues/11936#issuecomment-1466764006
42-
[mypy-setuptools.config._validate_pyproject.*]
46+
[mypy-setuptools.config._validate_pyproject.*,setuptools._distutils.*]
4347
follow_imports = silent
4448
# silent => ignore errors when following imports

Diff for: setuptools/__init__.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
"""Extensions to the 'distutils' for large or complex distributions"""
2+
# mypy: disable_error_code=override
3+
# Command.reinitialize_command has an extra **kw param that distutils doesn't have
4+
# Can't disable on the exact line because distutils doesn't exists on Python 3.12
5+
# and mypy isn't aware of distutils_hack, causing distutils.core.Command to be Any,
6+
# and a [unused-ignore] to be raised on 3.12+
27

38
from __future__ import annotations
49

@@ -114,8 +119,10 @@ def setup(**attrs):
114119
setup.__doc__ = distutils.core.setup.__doc__
115120

116121
if TYPE_CHECKING:
122+
from typing_extensions import TypeAlias
123+
117124
# Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962
118-
_Command = distutils.core.Command
125+
_Command: TypeAlias = distutils.core.Command
119126
else:
120127
_Command = monkey.get_unpatched(distutils.core.Command)
121128

@@ -207,7 +214,7 @@ def ensure_string_list(self, option):
207214
"'%s' must be a list of strings (got %r)" % (option, val)
208215
)
209216

210-
@overload # type:ignore[override] # Extra **kw param
217+
@overload
211218
def reinitialize_command(
212219
self, command: str, reinit_subcommands: bool = False, **kw
213220
) -> _Command: ...

Diff for: setuptools/build_meta.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,10 @@ def _build_with_temp_dir(
387387

388388
# Build in a temporary directory, then copy to the target.
389389
os.makedirs(result_directory, exist_ok=True)
390-
temp_opts = {"prefix": ".tmp-", "dir": result_directory}
391390

392-
with tempfile.TemporaryDirectory(**temp_opts) as tmp_dist_dir:
391+
with tempfile.TemporaryDirectory(
392+
prefix=".tmp-", dir=result_directory
393+
) as tmp_dist_dir:
393394
sys.argv = [
394395
*sys.argv[:1],
395396
*self._global_args(config_settings),

Diff for: setuptools/command/build_ext.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
get_config_var("LDSHARED")
3333
# Not publicly exposed in typeshed distutils stubs, but this is done on purpose
3434
# See https://github.com/pypa/setuptools/pull/4228#issuecomment-1959856400
35-
from distutils.sysconfig import _config_vars as _CONFIG_VARS # type: ignore # noqa
35+
from distutils.sysconfig import _config_vars as _CONFIG_VARS # noqa: E402
3636

3737

3838
def _customize_compiler_for_shlib(compiler):

Diff for: setuptools/command/sdist.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import contextlib
24
import os
35
from itertools import chain
@@ -46,7 +48,7 @@ class sdist(orig.sdist):
4648
]
4749

4850
distribution: Distribution # override distutils.dist.Distribution with setuptools.dist.Distribution
49-
negative_opt = {}
51+
negative_opt: dict[str, str] = {}
5052

5153
README_EXTENSIONS = ['', '.rst', '.txt', '.md']
5254
READMES = tuple('README{0}'.format(ext) for ext in README_EXTENSIONS)

Diff for: setuptools/config/setupcfg.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
Generic,
2525
Iterable,
2626
Iterator,
27+
List,
2728
Tuple,
2829
TypeVar,
2930
Union,
31+
cast,
3032
)
3133

3234
from packaging.markers import default_environment as marker_env
@@ -108,7 +110,8 @@ def _apply(
108110
filenames = [*other_files, filepath]
109111

110112
try:
111-
_Distribution.parse_config_files(dist, filenames=filenames) # type: ignore[arg-type] # TODO: fix in distutils stubs
113+
# TODO: Temporary cast until mypy 1.12 is released with upstream fixes from typeshed
114+
_Distribution.parse_config_files(dist, filenames=cast(List[str], filenames))
112115
handlers = parse_configuration(
113116
dist, dist.command_options, ignore_option_errors=ignore_option_errors
114117
)

Diff for: setuptools/dist.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ def check_packages(dist, attr, value):
195195

196196

197197
if TYPE_CHECKING:
198+
from typing_extensions import TypeAlias
199+
198200
# Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962
199-
_Distribution = distutils.core.Distribution
201+
_Distribution: TypeAlias = distutils.core.Distribution
200202
else:
201203
_Distribution = get_unpatched(distutils.core.Distribution)
202204

Diff for: setuptools/errors.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,36 @@
33
Provides exceptions used by setuptools modules.
44
"""
55

6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
9+
610
from distutils import errors as _distutils_errors
711

12+
if TYPE_CHECKING:
13+
from typing_extensions import TypeAlias
14+
815
# Re-export errors from distutils to facilitate the migration to PEP632
916

10-
ByteCompileError = _distutils_errors.DistutilsByteCompileError
11-
CCompilerError = _distutils_errors.CCompilerError
12-
ClassError = _distutils_errors.DistutilsClassError
13-
CompileError = _distutils_errors.CompileError
14-
ExecError = _distutils_errors.DistutilsExecError
15-
FileError = _distutils_errors.DistutilsFileError
16-
InternalError = _distutils_errors.DistutilsInternalError
17-
LibError = _distutils_errors.LibError
18-
LinkError = _distutils_errors.LinkError
19-
ModuleError = _distutils_errors.DistutilsModuleError
20-
OptionError = _distutils_errors.DistutilsOptionError
21-
PlatformError = _distutils_errors.DistutilsPlatformError
22-
PreprocessError = _distutils_errors.PreprocessError
23-
SetupError = _distutils_errors.DistutilsSetupError
24-
TemplateError = _distutils_errors.DistutilsTemplateError
25-
UnknownFileError = _distutils_errors.UnknownFileError
17+
ByteCompileError: TypeAlias = _distutils_errors.DistutilsByteCompileError
18+
CCompilerError: TypeAlias = _distutils_errors.CCompilerError
19+
ClassError: TypeAlias = _distutils_errors.DistutilsClassError
20+
CompileError: TypeAlias = _distutils_errors.CompileError
21+
ExecError: TypeAlias = _distutils_errors.DistutilsExecError
22+
FileError: TypeAlias = _distutils_errors.DistutilsFileError
23+
InternalError: TypeAlias = _distutils_errors.DistutilsInternalError
24+
LibError: TypeAlias = _distutils_errors.LibError
25+
LinkError: TypeAlias = _distutils_errors.LinkError
26+
ModuleError: TypeAlias = _distutils_errors.DistutilsModuleError
27+
OptionError: TypeAlias = _distutils_errors.DistutilsOptionError
28+
PlatformError: TypeAlias = _distutils_errors.DistutilsPlatformError
29+
PreprocessError: TypeAlias = _distutils_errors.PreprocessError
30+
SetupError: TypeAlias = _distutils_errors.DistutilsSetupError
31+
TemplateError: TypeAlias = _distutils_errors.DistutilsTemplateError
32+
UnknownFileError: TypeAlias = _distutils_errors.UnknownFileError
2633

2734
# The root error class in the hierarchy
28-
BaseError = _distutils_errors.DistutilsError
35+
BaseError: TypeAlias = _distutils_errors.DistutilsError
2936

3037

3138
class InvalidConfigError(OptionError):

Diff for: setuptools/extension.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ def _have_cython():
2727
# for compatibility
2828
have_pyrex = _have_cython
2929
if TYPE_CHECKING:
30+
from typing_extensions import TypeAlias
31+
3032
# Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962
31-
_Extension = distutils.core.Extension
33+
_Extension: TypeAlias = distutils.core.Extension
3234
else:
3335
_Extension = get_unpatched(distutils.core.Extension)
3436

0 commit comments

Comments
 (0)