Skip to content

Commit a001ec8

Browse files
authored
Enforce modern annotations syntax (#4368)
1 parent 4f77bb6 commit a001ec8

29 files changed

+363
-347
lines changed

pkg_resources/__init__.py

Lines changed: 109 additions & 112 deletions
Large diffs are not rendered by default.

pkg_resources/extern/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from __future__ import annotations
12
from importlib.machinery import ModuleSpec
23
import importlib.util
34
import sys
45
from types import ModuleType
5-
from typing import Iterable, Optional, Sequence
6+
from typing import Iterable, Sequence
67

78

89
class VendorImporter:
@@ -15,7 +16,7 @@ def __init__(
1516
self,
1617
root_name: str,
1718
vendored_names: Iterable[str] = (),
18-
vendor_pkg: Optional[str] = None,
19+
vendor_pkg: str | None = None,
1920
):
2021
self.root_name = root_name
2122
self.vendored_names = set(vendored_names)
@@ -65,8 +66,8 @@ def exec_module(self, module: ModuleType):
6566
def find_spec(
6667
self,
6768
fullname: str,
68-
path: Optional[Sequence[str]] = None,
69-
target: Optional[ModuleType] = None,
69+
path: Sequence[str] | None = None,
70+
target: ModuleType | None = None,
7071
):
7172
"""Return a module spec for vendored names."""
7273
return (

pkg_resources/tests/test_pkg_resources.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import builtins
24
import sys
35
import tempfile
@@ -9,7 +11,6 @@
911
import stat
1012
import distutils.dist
1113
import distutils.command.install_egg_info
12-
from typing import List
1314

1415
from unittest import mock
1516

@@ -33,7 +34,7 @@ def __call__(self):
3334

3435

3536
class TestZipProvider:
36-
finalizers: List[EggRemover] = []
37+
finalizers: list[EggRemover] = []
3738

3839
ref_time = datetime.datetime(2013, 5, 12, 13, 25, 0)
3940
"A reference time for a file modification"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ testing = [
4040
"pytest-mypy",
4141
"pytest-enabler >= 2.2",
4242
# workaround for pypa/setuptools#3921
43-
'pytest-ruff >= 0.2.1; sys_platform != "cygwin"',
43+
'pytest-ruff >= 0.3.2; sys_platform != "cygwin"',
4444

4545
# local
4646
"virtualenv>=13.0.0",

ruff.toml

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ extend-select = [
44
"W",
55

66
# local
7-
"UP", # pyupgrade
8-
"YTT", # flake8-2020
7+
"FA", # flake8-future-annotations
8+
"F404", # late-future-import
9+
"UP", # pyupgrade
10+
"YTT", # flake8-2020
911
]
1012
ignore = [
13+
"UP015", # redundant-open-modes, explicit is preferred
14+
"UP030", # temporarily disabled
15+
"UP031", # temporarily disabled
16+
"UP032", # temporarily disabled
17+
"UP038", # Using `X | Y` in `isinstance` call is slower and more verbose https://github.com/astral-sh/ruff/issues/7871
18+
1119
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
1220
"W191",
1321
"E111",
@@ -24,19 +32,16 @@ ignore = [
2432
"ISC001",
2533
"ISC002",
2634
]
27-
extend-ignore = [
28-
"UP015", # redundant-open-modes, explicit is preferred
29-
"UP030", # temporarily disabled
30-
"UP031", # temporarily disabled
31-
"UP032", # temporarily disabled
32-
"UP036", # temporarily disabled
33-
]
3435
exclude = [
3536
"**/_vendor",
3637
"setuptools/_distutils",
3738
"setuptools/config/_validate_pyproject",
3839
]
3940

41+
[lint.per-file-ignores]
42+
# Auto-generated code
43+
"setuptools/config/_validate_pyproject/*" = ["FA100"]
44+
4045
[format]
4146
exclude = [
4247
"**/_vendor",

setuptools/_core_metadata.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
See: https://packaging.python.org/en/latest/specifications/core-metadata/
55
"""
66

7+
from __future__ import annotations
8+
79
import os
810
import stat
911
import textwrap
1012
from email import message_from_file
1113
from email.message import Message
1214
from tempfile import NamedTemporaryFile
13-
from typing import Optional, List
1415

1516
from distutils.util import rfc822_escape
1617

@@ -38,31 +39,31 @@ def rfc822_unescape(content: str) -> str:
3839
return '\n'.join((lines[0].lstrip(), textwrap.dedent('\n'.join(lines[1:]))))
3940

4041

41-
def _read_field_from_msg(msg: Message, field: str) -> Optional[str]:
42+
def _read_field_from_msg(msg: Message, field: str) -> str | None:
4243
"""Read Message header field."""
4344
value = msg[field]
4445
if value == 'UNKNOWN':
4546
return None
4647
return value
4748

4849

49-
def _read_field_unescaped_from_msg(msg: Message, field: str) -> Optional[str]:
50+
def _read_field_unescaped_from_msg(msg: Message, field: str) -> str | None:
5051
"""Read Message header field and apply rfc822_unescape."""
5152
value = _read_field_from_msg(msg, field)
5253
if value is None:
5354
return value
5455
return rfc822_unescape(value)
5556

5657

57-
def _read_list_from_msg(msg: Message, field: str) -> Optional[List[str]]:
58+
def _read_list_from_msg(msg: Message, field: str) -> list[str] | None:
5859
"""Read Message header field and return all results as list."""
5960
values = msg.get_all(field, None)
6061
if values == []:
6162
return None
6263
return values
6364

6465

65-
def _read_payload_from_msg(msg: Message) -> Optional[str]:
66+
def _read_payload_from_msg(msg: Message) -> str | None:
6667
value = str(msg.get_payload()).strip()
6768
if value == 'UNKNOWN' or not value:
6869
return None

setuptools/build_meta.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
Again, this is not a formal definition! Just a "taste" of the module.
2727
"""
2828

29+
from __future__ import annotations
30+
2931
import io
3032
import os
3133
import shlex
@@ -36,7 +38,7 @@
3638
import tempfile
3739
import warnings
3840
from pathlib import Path
39-
from typing import Dict, Iterator, List, Optional, Tuple, Union, Iterable
41+
from typing import Dict, Iterator, List, Optional, Union, Iterable
4042

4143
import setuptools
4244
import distutils
@@ -113,7 +115,7 @@ def _get_immediate_subdirectories(a_dir):
113115
]
114116

115117

116-
def _file_with_extension(directory: StrPath, extension: Union[str, Tuple[str, ...]]):
118+
def _file_with_extension(directory: StrPath, extension: str | tuple[str, ...]):
117119
matching = (f for f in os.listdir(directory) if f.endswith(extension))
118120
try:
119121
(file,) = matching
@@ -163,7 +165,7 @@ class _ConfigSettingsTranslator:
163165

164166
# See pypa/setuptools#1928 pypa/setuptools#2491
165167

166-
def _get_config(self, key: str, config_settings: _ConfigSettings) -> List[str]:
168+
def _get_config(self, key: str, config_settings: _ConfigSettings) -> list[str]:
167169
"""
168170
Get the value of a specific key in ``config_settings`` as a list of strings.
169171
@@ -371,7 +373,7 @@ def prepare_metadata_for_build_wheel(
371373
def _build_with_temp_dir(
372374
self,
373375
setup_command: Iterable[str],
374-
result_extension: Union[str, Tuple[str, ...]],
376+
result_extension: str | tuple[str, ...],
375377
result_directory: StrPath,
376378
config_settings: _ConfigSettings,
377379
arbitrary_args: Iterable[str] = (),
@@ -407,7 +409,7 @@ def build_wheel(
407409
self,
408410
wheel_directory: StrPath,
409411
config_settings: _ConfigSettings = None,
410-
metadata_directory: Optional[StrPath] = None,
412+
metadata_directory: StrPath | None = None,
411413
):
412414
with suppress_known_deprecation():
413415
return self._build_with_temp_dir(
@@ -425,9 +427,7 @@ def build_sdist(
425427
['sdist', '--formats', 'gztar'], '.tar.gz', sdist_directory, config_settings
426428
)
427429

428-
def _get_dist_info_dir(
429-
self, metadata_directory: Optional[StrPath]
430-
) -> Optional[str]:
430+
def _get_dist_info_dir(self, metadata_directory: StrPath | None) -> str | None:
431431
if not metadata_directory:
432432
return None
433433
dist_info_candidates = list(Path(metadata_directory).glob("*.dist-info"))
@@ -443,7 +443,7 @@ def build_editable(
443443
self,
444444
wheel_directory: StrPath,
445445
config_settings: _ConfigSettings = None,
446-
metadata_directory: Optional[str] = None,
446+
metadata_directory: str | None = None,
447447
):
448448
# XXX can or should we hide our editable_wheel command normally?
449449
info_dir = self._get_dist_info_dir(metadata_directory)

setuptools/command/_requirestxt.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
See https://setuptools.pypa.io/en/latest/deprecated/python_eggs.html#requires-txt
88
"""
99

10+
from __future__ import annotations
11+
1012
import io
1113
from collections import defaultdict
1214
from itertools import filterfalse
13-
from typing import Dict, List, Tuple, Mapping, TypeVar
15+
from typing import Dict, Mapping, TypeVar
1416

1517
from .. import _reqs
1618
from ..extern.jaraco.text import yield_lines
@@ -26,7 +28,7 @@
2628

2729
def _prepare(
2830
install_requires: _StrOrIter, extras_require: Mapping[str, _StrOrIter]
29-
) -> Tuple[List[str], Dict[str, List[str]]]:
31+
) -> tuple[list[str], dict[str, list[str]]]:
3032
"""Given values for ``install_requires`` and ``extras_require``
3133
create modified versions in a way that can be written in ``requires.txt``
3234
"""
@@ -54,7 +56,7 @@ def _convert_extras_requirements(
5456

5557
def _move_install_requirements_markers(
5658
install_requires: _StrOrIter, extras_require: Mapping[str, _Ordered[Requirement]]
57-
) -> Tuple[List[str], Dict[str, List[str]]]:
59+
) -> tuple[list[str], dict[str, list[str]]]:
5860
"""
5961
The ``requires.txt`` file has an specific format:
6062
- Environment markers need to be part of the section headers and

setuptools/command/build.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from typing import Dict, List, Protocol
1+
from __future__ import annotations
2+
3+
from typing import Protocol
24
from distutils.command.build import build as _build
35

46
_ORIGINAL_SUBCOMMANDS = {"build_py", "build_clib", "build_ext", "build_scripts"}
@@ -87,7 +89,7 @@ def finalize_options(self):
8789
def run(self):
8890
"""(Required by the original :class:`setuptools.Command` interface)"""
8991

90-
def get_source_files(self) -> List[str]:
92+
def get_source_files(self) -> list[str]:
9193
"""
9294
Return a list of all files that are used by the command to create the expected
9395
outputs.
@@ -98,7 +100,7 @@ def get_source_files(self) -> List[str]:
98100
All files should be strings relative to the project root directory.
99101
"""
100102

101-
def get_outputs(self) -> List[str]:
103+
def get_outputs(self) -> list[str]:
102104
"""
103105
Return a list of files intended for distribution as they would have been
104106
produced by the build.
@@ -111,7 +113,7 @@ def get_outputs(self) -> List[str]:
111113
and don't correspond to any source file already present in the project.
112114
"""
113115

114-
def get_output_mapping(self) -> Dict[str, str]:
116+
def get_output_mapping(self) -> dict[str, str]:
115117
"""
116118
Return a mapping between destination files as they would be produced by the
117119
build (dict keys) into the respective existing (source) files (dict values).

setuptools/command/build_ext.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from __future__ import annotations
2+
13
import os
24
import sys
35
import itertools
46
from importlib.machinery import EXTENSION_SUFFIXES
57
from importlib.util import cache_from_source as _compiled_file_name
6-
from typing import Dict, Iterator, List, Tuple
8+
from typing import Iterator
79
from pathlib import Path
810

911
from distutils.command.build_ext import build_ext as _du_build_ext
@@ -93,7 +95,7 @@ def run(self):
9395
if old_inplace:
9496
self.copy_extensions_to_source()
9597

96-
def _get_inplace_equivalent(self, build_py, ext: Extension) -> Tuple[str, str]:
98+
def _get_inplace_equivalent(self, build_py, ext: Extension) -> tuple[str, str]:
9799
fullname = self.get_ext_fullname(ext.name)
98100
filename = self.get_ext_filename(fullname)
99101
modpath = fullname.split('.')
@@ -125,7 +127,7 @@ def _get_equivalent_stub(self, ext: Extension, output_file: str) -> str:
125127
_, _, name = ext.name.rpartition(".")
126128
return f"{os.path.join(dir_, name)}.py"
127129

128-
def _get_output_mapping(self) -> Iterator[Tuple[str, str]]:
130+
def _get_output_mapping(self) -> Iterator[tuple[str, str]]:
129131
if not self.inplace:
130132
return
131133

@@ -265,7 +267,7 @@ def links_to_dynamic(self, ext):
265267
pkg = '.'.join(ext._full_name.split('.')[:-1] + [''])
266268
return any(pkg + libname in libnames for libname in ext.libraries)
267269

268-
def get_source_files(self) -> List[str]:
270+
def get_source_files(self) -> list[str]:
269271
return [*_build_ext.get_source_files(self), *self._get_internal_depends()]
270272

271273
def _get_internal_depends(self) -> Iterator[str]:
@@ -306,12 +308,12 @@ def skip(orig_path: str, reason: str) -> None:
306308

307309
yield path.as_posix()
308310

309-
def get_outputs(self) -> List[str]:
311+
def get_outputs(self) -> list[str]:
310312
if self.inplace:
311313
return list(self.get_output_mapping().keys())
312314
return sorted(_build_ext.get_outputs(self) + self.__get_stubs_outputs())
313315

314-
def get_output_mapping(self) -> Dict[str, str]:
316+
def get_output_mapping(self) -> dict[str, str]:
315317
"""See :class:`setuptools.commands.build.SubCommand`"""
316318
mapping = self._get_output_mapping()
317319
return dict(sorted(mapping, key=lambda x: x[0]))

0 commit comments

Comments
 (0)