Skip to content

Commit 4eef53a

Browse files
committed
Merge simple type annotations from typeshed
1 parent e55c19e commit 4eef53a

33 files changed

+233
-171
lines changed

mypy.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ ignore_missing_imports = True
5151

5252
# Even when excluding a module, import issues can show up due to following import
5353
# https://github.com/python/mypy/issues/11936#issuecomment-1466764006
54-
[mypy-setuptools.config._validate_pyproject.*,setuptools._distutils.*]
54+
[mypy-setuptools.config._validate_pyproject.*,setuptools._vendor.*,setuptools._distutils.*]
5555
follow_imports = silent
5656
# silent => ignore errors when following imports

pyproject.toml

-4
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,3 @@ formats = "zip"
211211

212212

213213
[tool.setuptools_scm]
214-
215-
216-
[tool.pytest-enabler.mypy]
217-
# Disabled due to jaraco/skeleton#143

ruff.toml

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ ignore = [
5353
# Only enforcing return type annotations for public modules
5454
"**/tests/**" = ["ANN2"]
5555
"tools/**" = ["ANN2"]
56+
# Temporarily disabling enforced return annotations for the setuptool package to progressively type from Typeshed
57+
"setuptools/**" = ["ANN2"]
5658
# Suppress nuisance warnings about module-import-not-at-top-of-file (E402) due to workaround for #4476
5759
"setuptools/__init__.py" = ["E402"]
5860
"pkg_resources/__init__.py" = ["E402"]

setuptools/__init__.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def finalize_options(self):
8989
_fetch_build_eggs(dist)
9090

9191

92-
def _fetch_build_eggs(dist):
92+
def _fetch_build_eggs(dist: Distribution):
9393
try:
9494
dist.fetch_build_eggs(dist.setup_requires)
9595
except Exception as ex:
@@ -120,10 +120,8 @@ def setup(**attrs):
120120
setup.__doc__ = distutils.core.setup.__doc__
121121

122122
if TYPE_CHECKING:
123-
from typing_extensions import TypeAlias
124-
125123
# Work around a mypy issue where type[T] can't be used as a base: https://github.com/python/mypy/issues/10962
126-
_Command: TypeAlias = distutils.core.Command
124+
from distutils.core import Command as _Command
127125
else:
128126
_Command = monkey.get_unpatched(distutils.core.Command)
129127

@@ -188,7 +186,7 @@ def _ensure_stringlike(self, option, what, default=None):
188186
)
189187
return val
190188

191-
def ensure_string_list(self, option):
189+
def ensure_string_list(self, option: str): # type: ignore[override] # Fixed in typeshed for mypy 1.12
192190
r"""Ensure that 'option' is a list of strings. If 'option' is
193191
currently a string, we split it either on /,\s*/ or /\s+/, so
194192
"foo bar baz", "foo,bar,baz", and "foo, bar baz" all become

setuptools/_path.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import sys
66
from typing import TYPE_CHECKING, Union
77

8+
from more_itertools import unique_everseen
9+
810
if TYPE_CHECKING:
911
from typing_extensions import TypeAlias
1012

11-
12-
from more_itertools import unique_everseen
13-
14-
if sys.version_info >= (3, 9):
1513
StrPath: TypeAlias = Union[str, os.PathLike[str]] # Same as _typeshed.StrPath
1614
else:
15+
# Python 3.8 support
1716
StrPath: TypeAlias = Union[str, os.PathLike]
1817

1918

setuptools/build_meta.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import tokenize
3939
import warnings
4040
from pathlib import Path
41-
from typing import TYPE_CHECKING, Dict, Iterable, Iterator, List, Union
41+
from typing import TYPE_CHECKING, Iterable, Iterator, List, Mapping, Union
4242

4343
import setuptools
4444

@@ -53,7 +53,6 @@
5353
if TYPE_CHECKING:
5454
from typing_extensions import TypeAlias
5555

56-
5756
__all__ = [
5857
'get_requires_for_build_sdist',
5958
'get_requires_for_build_wheel',
@@ -147,7 +146,7 @@ def suppress_known_deprecation():
147146
yield
148147

149148

150-
_ConfigSettings: TypeAlias = Union[Dict[str, Union[str, List[str], None]], None]
149+
_ConfigSettings: TypeAlias = Union[Mapping[str, Union[str, List[str], None]], None]
151150
"""
152151
Currently the user can run::
153152
@@ -291,7 +290,9 @@ def _arbitrary_args(self, config_settings: _ConfigSettings) -> Iterator[str]:
291290

292291

293292
class _BuildMetaBackend(_ConfigSettingsTranslator):
294-
def _get_build_requires(self, config_settings, requirements):
293+
def _get_build_requires(
294+
self, config_settings: _ConfigSettings, requirements: list[str]
295+
):
295296
sys.argv = [
296297
*sys.argv[:1],
297298
*self._global_args(config_settings),
@@ -305,7 +306,7 @@ def _get_build_requires(self, config_settings, requirements):
305306

306307
return requirements
307308

308-
def run_setup(self, setup_script='setup.py'):
309+
def run_setup(self, setup_script: str = 'setup.py'):
309310
# Note that we can reuse our build directory between calls
310311
# Correctness comes first, then optimization later
311312
__file__ = os.path.abspath(setup_script)
@@ -328,13 +329,15 @@ def run_setup(self, setup_script='setup.py'):
328329
"setup-py-deprecated.html",
329330
)
330331

331-
def get_requires_for_build_wheel(self, config_settings=None):
332+
def get_requires_for_build_wheel(self, config_settings: _ConfigSettings = None):
332333
return self._get_build_requires(config_settings, requirements=[])
333334

334-
def get_requires_for_build_sdist(self, config_settings=None):
335+
def get_requires_for_build_sdist(self, config_settings: _ConfigSettings = None):
335336
return self._get_build_requires(config_settings, requirements=[])
336337

337-
def _bubble_up_info_directory(self, metadata_directory: str, suffix: str) -> str:
338+
def _bubble_up_info_directory(
339+
self, metadata_directory: StrPath, suffix: str
340+
) -> str:
338341
"""
339342
PEP 517 requires that the .dist-info directory be placed in the
340343
metadata_directory. To comply, we MUST copy the directory to the root.
@@ -347,7 +350,7 @@ def _bubble_up_info_directory(self, metadata_directory: str, suffix: str) -> str
347350
# PEP 517 allow other files and dirs to exist in metadata_directory
348351
return info_dir.name
349352

350-
def _find_info_directory(self, metadata_directory: str, suffix: str) -> Path:
353+
def _find_info_directory(self, metadata_directory: StrPath, suffix: str) -> Path:
351354
for parent, dirs, _ in os.walk(metadata_directory):
352355
candidates = [f for f in dirs if f.endswith(suffix)]
353356

@@ -359,14 +362,14 @@ def _find_info_directory(self, metadata_directory: str, suffix: str) -> Path:
359362
raise errors.InternalError(msg)
360363

361364
def prepare_metadata_for_build_wheel(
362-
self, metadata_directory, config_settings=None
365+
self, metadata_directory: StrPath, config_settings: _ConfigSettings = None
363366
):
364367
sys.argv = [
365368
*sys.argv[:1],
366369
*self._global_args(config_settings),
367370
"dist_info",
368371
"--output-dir",
369-
metadata_directory,
372+
str(metadata_directory),
370373
"--keep-egg-info",
371374
]
372375
with no_install_setup_requires():
@@ -449,7 +452,7 @@ def build_editable(
449452
self,
450453
wheel_directory: StrPath,
451454
config_settings: _ConfigSettings = None,
452-
metadata_directory: str | None = None,
455+
metadata_directory: StrPath | None = None,
453456
):
454457
# XXX can or should we hide our editable_wheel command normally?
455458
info_dir = self._get_dist_info_dir(metadata_directory)
@@ -460,11 +463,13 @@ def build_editable(
460463
cmd, ".whl", wheel_directory, config_settings
461464
)
462465

463-
def get_requires_for_build_editable(self, config_settings=None):
466+
def get_requires_for_build_editable(
467+
self, config_settings: _ConfigSettings = None
468+
):
464469
return self.get_requires_for_build_wheel(config_settings)
465470

466471
def prepare_metadata_for_build_editable(
467-
self, metadata_directory, config_settings=None
472+
self, metadata_directory: StrPath, config_settings: _ConfigSettings = None
468473
):
469474
return self.prepare_metadata_for_build_wheel(
470475
metadata_directory, config_settings
@@ -483,7 +488,7 @@ class _BuildMetaLegacyBackend(_BuildMetaBackend):
483488
and will eventually be removed.
484489
"""
485490

486-
def run_setup(self, setup_script='setup.py'):
491+
def run_setup(self, setup_script: str = 'setup.py'):
487492
# In order to maintain compatibility with scripts assuming that
488493
# the setup.py script is in a directory on the PYTHONPATH, inject
489494
# '' into sys.path. (pypa/setuptools#1642)

setuptools/command/_requirestxt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
from packaging.requirements import Requirement
1919

2020
from .. import _reqs
21+
from .._reqs import _StrOrIter
2122

2223
# dict can work as an ordered set
2324
_T = TypeVar("_T")
2425
_Ordered = Dict[_T, None]
2526
_ordered = dict
26-
_StrOrIter = _reqs._StrOrIter
2727

2828

2929
def _prepare(

setuptools/command/bdist_egg.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
33
Build .egg distributions"""
44

5+
from __future__ import annotations
6+
57
import marshal
68
import os
79
import re
810
import sys
911
import textwrap
1012
from sysconfig import get_path, get_python_version
1113
from types import CodeType
14+
from typing import TYPE_CHECKING, Literal
1215

1316
from setuptools import Command
1417
from setuptools.extension import Library
@@ -18,6 +21,12 @@
1821
from distutils import log
1922
from distutils.dir_util import mkpath, remove_tree
2023

24+
if TYPE_CHECKING:
25+
from typing_extensions import TypeAlias
26+
27+
# Same as zipfile._ZipFileMode from typeshed
28+
_ZipFileMode: TypeAlias = Literal["r", "w", "x", "a"]
29+
2130

2231
def _get_purelib():
2332
return get_path("purelib")
@@ -431,7 +440,12 @@ def can_scan():
431440

432441

433442
def make_zipfile(
434-
zip_filename, base_dir, verbose=False, dry_run=False, compress=True, mode='w'
443+
zip_filename,
444+
base_dir,
445+
verbose: bool = False,
446+
dry_run: bool = False,
447+
compress=True,
448+
mode: _ZipFileMode = 'w',
435449
):
436450
"""Create a zip file from all the files under 'base_dir'. The output
437451
zip file will be named 'base_dir' + ".zip". Uses either the "zipfile"

setuptools/command/bdist_wheel.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ def initialize_options(self) -> None:
248248
self.relative = False
249249
self.owner = None
250250
self.group = None
251-
self.universal: bool = False
252-
self.compression: int | str = "deflated"
253-
self.python_tag: str = python_tag()
251+
self.universal = False
252+
self.compression: str | int = "deflated"
253+
self.python_tag = python_tag()
254254
self.build_number: str | None = None
255255
self.py_limited_api: str | Literal[False] = False
256256
self.plat_name_supplied = False

setuptools/command/build_ext.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def get_abi3_suffix():
8989

9090
class build_ext(_build_ext):
9191
distribution: Distribution # override distutils.dist.Distribution with setuptools.dist.Distribution
92-
editable_mode: bool = False
93-
inplace: bool = False
92+
editable_mode = False
93+
inplace = False
9494

9595
def run(self):
9696
"""Build extensions in build directory, then copy if --inplace"""
@@ -410,7 +410,7 @@ def link_shared_object(
410410
library_dirs=None,
411411
runtime_library_dirs=None,
412412
export_symbols=None,
413-
debug=False,
413+
debug: bool = False,
414414
extra_preargs=None,
415415
extra_postargs=None,
416416
build_temp=None,
@@ -445,7 +445,7 @@ def link_shared_object(
445445
library_dirs=None,
446446
runtime_library_dirs=None,
447447
export_symbols=None,
448-
debug=False,
448+
debug: bool = False,
449449
extra_preargs=None,
450450
extra_postargs=None,
451451
build_temp=None,

setuptools/command/build_py.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from more_itertools import unique_everseen
1414

15+
from setuptools._path import StrPath
16+
1517
from ..dist import Distribution
1618
from ..warnings import SetuptoolsDeprecationWarning
1719

@@ -48,14 +50,14 @@ def finalize_options(self):
4850
del self.__dict__['data_files']
4951
self.__updated_files = []
5052

51-
def copy_file(
53+
def copy_file( # type: ignore[override] # No overload, str support only
5254
self,
53-
infile,
54-
outfile,
55-
preserve_mode=True,
56-
preserve_times=True,
57-
link=None,
58-
level=1,
55+
infile: StrPath,
56+
outfile: StrPath,
57+
preserve_mode: bool = True,
58+
preserve_times: bool = True,
59+
link: str | None = None,
60+
level: object = 1,
5961
):
6062
# Overwrite base class to allow using links
6163
if link:
@@ -141,7 +143,7 @@ def find_data_files(self, package, src_dir):
141143
)
142144
return self.exclude_data_files(package, src_dir, files)
143145

144-
def get_outputs(self, include_bytecode=True) -> list[str]:
146+
def get_outputs(self, include_bytecode: bool = True) -> list[str]: # type: ignore[override] # Using a real boolean instead of 0|1
145147
"""See :class:`setuptools.commands.build.SubCommand`"""
146148
if self.editable_mode:
147149
return list(self.get_output_mapping().keys())

0 commit comments

Comments
 (0)