Skip to content

Commit 65fe65b

Browse files
committed
Enable all FA rules for ruff
This enables use of Python 3.10+ type annotation syntax, reducing the churn that would be caused when moving to Python 3.10.
1 parent 9d9320c commit 65fe65b

File tree

149 files changed

+1236
-1057
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+1236
-1057
lines changed

docs/pip_sphinxext.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""pip sphinx extensions"""
22

3+
from __future__ import annotations
4+
35
import optparse
46
import pathlib
57
import re
68
import sys
79
from collections.abc import Iterable, Iterator
810
from textwrap import dedent
9-
from typing import Optional, Union
1011

1112
from docutils import nodes, statemachine
1213
from docutils.parsers import rst
@@ -34,9 +35,7 @@ def convert_cli_opt_names_to_envvars(original_cli_opt_names: list[str]) -> list[
3435
class PipNewsInclude(rst.Directive):
3536
required_arguments = 1
3637

37-
def _is_version_section_title_underline(
38-
self, prev: Optional[str], curr: str
39-
) -> bool:
38+
def _is_version_section_title_underline(self, prev: str | None, curr: str) -> bool:
4039
"""Find a ==== line that marks the version section title."""
4140
if prev is None:
4241
return False
@@ -121,7 +120,7 @@ def run(self) -> list[nodes.Node]:
121120

122121
class PipOptions(rst.Directive):
123122
def _format_option(
124-
self, option: optparse.Option, cmd_name: Optional[str] = None
123+
self, option: optparse.Option, cmd_name: str | None = None
125124
) -> list[str]:
126125
bookmark_line = (
127126
f".. _`{cmd_name}_{option._long_opts[0]}`:"
@@ -158,7 +157,7 @@ def _format_option(
158157
]
159158

160159
def _format_options(
161-
self, options: Iterable[optparse.Option], cmd_name: Optional[str] = None
160+
self, options: Iterable[optparse.Option], cmd_name: str | None = None
162161
) -> None:
163162
for option in options:
164163
if option.help == optparse.SUPPRESS_HELP:
@@ -311,7 +310,7 @@ def run(self) -> list[nodes.Node]:
311310
return [node]
312311

313312

314-
def setup(app: Sphinx) -> dict[str, Union[bool, str]]:
313+
def setup(app: Sphinx) -> dict[str, bool | str]:
315314
app.add_directive("pip-command-usage", PipCommandUsage)
316315
app.add_directive("pip-command-description", PipCommandDescription)
317316
app.add_directive("pip-command-options", PipCommandOptions)

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ select = [
187187
"W",
188188
"RUF100",
189189
"UP",
190-
"FA102", # future-required-type-annotation
190+
"FA",
191191
]
192192

193193
[tool.ruff.lint.isort]

src/pip/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from typing import Optional
1+
from __future__ import annotations
22

33
__version__ = "25.2.dev0"
44

55

6-
def main(args: Optional[list[str]] = None) -> int:
6+
def main(args: list[str] | None = None) -> int:
77
"""This is an internal API only meant for use by pip's own console scripts.
88
99
For additional details, see https://github.com/pypa/pip/issues/7498.

src/pip/_internal/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from __future__ import annotations
22

33
from pip._internal.utils import _log
44

@@ -7,7 +7,7 @@
77
_log.init_logging()
88

99

10-
def main(args: Optional[list[str]] = None) -> int:
10+
def main(args: list[str] | None = None) -> int:
1111
"""This is preserved for old console scripts that may still be referencing
1212
it.
1313

src/pip/_internal/build_env.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Build Environment used for isolation during sdist building"""
22

3+
from __future__ import annotations
4+
35
import logging
46
import os
57
import pathlib
@@ -9,7 +11,7 @@
911
from collections import OrderedDict
1012
from collections.abc import Iterable
1113
from types import TracebackType
12-
from typing import TYPE_CHECKING, Optional, Union
14+
from typing import TYPE_CHECKING
1315

1416
from pip._vendor.packaging.version import Version
1517

@@ -28,7 +30,7 @@
2830
logger = logging.getLogger(__name__)
2931

3032

31-
def _dedup(a: str, b: str) -> Union[tuple[str], tuple[str, str]]:
33+
def _dedup(a: str, b: str) -> tuple[str] | tuple[str, str]:
3234
return (a, b) if a != b else (a,)
3335

3436

@@ -157,9 +159,9 @@ def __enter__(self) -> None:
157159

158160
def __exit__(
159161
self,
160-
exc_type: Optional[type[BaseException]],
161-
exc_val: Optional[BaseException],
162-
exc_tb: Optional[TracebackType],
162+
exc_type: type[BaseException] | None,
163+
exc_val: BaseException | None,
164+
exc_tb: TracebackType | None,
163165
) -> None:
164166
for varname, old_value in self._save_env.items():
165167
if old_value is None:
@@ -203,7 +205,7 @@ def check_requirements(
203205

204206
def install_requirements(
205207
self,
206-
finder: "PackageFinder",
208+
finder: PackageFinder,
207209
requirements: Iterable[str],
208210
prefix_as_string: str,
209211
*,
@@ -225,7 +227,7 @@ def install_requirements(
225227
@staticmethod
226228
def _install_requirements(
227229
pip_runnable: str,
228-
finder: "PackageFinder",
230+
finder: PackageFinder,
229231
requirements: Iterable[str],
230232
prefix: _Prefix,
231233
*,
@@ -306,9 +308,9 @@ def __enter__(self) -> None:
306308

307309
def __exit__(
308310
self,
309-
exc_type: Optional[type[BaseException]],
310-
exc_val: Optional[BaseException],
311-
exc_tb: Optional[TracebackType],
311+
exc_type: type[BaseException] | None,
312+
exc_val: BaseException | None,
313+
exc_tb: TracebackType | None,
312314
) -> None:
313315
pass
314316

@@ -317,7 +319,7 @@ def cleanup(self) -> None:
317319

318320
def install_requirements(
319321
self,
320-
finder: "PackageFinder",
322+
finder: PackageFinder,
321323
requirements: Iterable[str],
322324
prefix_as_string: str,
323325
*,

src/pip/_internal/cache.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Cache Management"""
22

3+
from __future__ import annotations
4+
35
import hashlib
46
import json
57
import logging
68
import os
79
from pathlib import Path
8-
from typing import Any, Optional
10+
from typing import Any
911

1012
from pip._vendor.packaging.tags import Tag, interpreter_name, interpreter_version
1113
from pip._vendor.packaging.utils import canonicalize_name
@@ -89,7 +91,7 @@ def get_path_for_link(self, link: Link) -> str:
8991
def get(
9092
self,
9193
link: Link,
92-
package_name: Optional[str],
94+
package_name: str | None,
9395
supported_tags: list[Tag],
9496
) -> Link:
9597
"""Returns a link to a cached item if it exists, otherwise returns the
@@ -127,7 +129,7 @@ def get_path_for_link(self, link: Link) -> str:
127129
def get(
128130
self,
129131
link: Link,
130-
package_name: Optional[str],
132+
package_name: str | None,
131133
supported_tags: list[Tag],
132134
) -> Link:
133135
candidates = []
@@ -188,7 +190,7 @@ def __init__(
188190
):
189191
self.link = link
190192
self.persistent = persistent
191-
self.origin: Optional[DirectUrl] = None
193+
self.origin: DirectUrl | None = None
192194
origin_direct_url_path = Path(self.link.file_path).parent / ORIGIN_JSON_NAME
193195
if origin_direct_url_path.exists():
194196
try:
@@ -225,7 +227,7 @@ def get_ephem_path_for_link(self, link: Link) -> str:
225227
def get(
226228
self,
227229
link: Link,
228-
package_name: Optional[str],
230+
package_name: str | None,
229231
supported_tags: list[Tag],
230232
) -> Link:
231233
cache_entry = self.get_cache_entry(link, package_name, supported_tags)
@@ -236,9 +238,9 @@ def get(
236238
def get_cache_entry(
237239
self,
238240
link: Link,
239-
package_name: Optional[str],
241+
package_name: str | None,
240242
supported_tags: list[Tag],
241-
) -> Optional[CacheEntry]:
243+
) -> CacheEntry | None:
242244
"""Returns a CacheEntry with a link to a cached item if it exists or
243245
None. The cache entry indicates if the item was found in the persistent
244246
or ephemeral cache.

src/pip/_internal/cli/autocompletion.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Logic that powers autocompletion installed by ``pip completion``."""
22

3+
from __future__ import annotations
4+
35
import optparse
46
import os
57
import sys
68
from collections.abc import Iterable
79
from itertools import chain
8-
from typing import Any, Optional
10+
from typing import Any
911

1012
from pip._internal.cli.main_parser import create_main_parser
1113
from pip._internal.commands import commands_dict, create_command
@@ -33,7 +35,7 @@ def autocomplete() -> None:
3335
options = []
3436

3537
# subcommand
36-
subcommand_name: Optional[str] = None
38+
subcommand_name: str | None = None
3739
for word in cwords:
3840
if word in subcommands:
3941
subcommand_name = word
@@ -123,7 +125,7 @@ def autocomplete() -> None:
123125

124126
def get_path_completion_type(
125127
cwords: list[str], cword: int, opts: Iterable[Any]
126-
) -> Optional[str]:
128+
) -> str | None:
127129
"""Get the type of path completion (``file``, ``dir``, ``path`` or None)
128130
129131
:param cwords: same as the environmental variable ``COMP_WORDS``

src/pip/_internal/cli/base_command.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
"""Base Command class, and related routines"""
22

3+
from __future__ import annotations
4+
35
import logging
46
import logging.config
57
import optparse
68
import os
79
import sys
810
import traceback
911
from optparse import Values
10-
from typing import Optional
1112

1213
from pip._vendor.rich import reconfigure
1314
from pip._vendor.rich import traceback as rich_traceback
@@ -60,7 +61,7 @@ def __init__(self, name: str, summary: str, isolated: bool = False) -> None:
6061
isolated=isolated,
6162
)
6263

63-
self.tempdir_registry: Optional[TempDirRegistry] = None
64+
self.tempdir_registry: TempDirRegistry | None = None
6465

6566
# Commands should add options to this option group
6667
optgroup_name = f"{self.name.capitalize()} Options"

src/pip/_internal/cli/cmdoptions.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
# The following comment should be removed at some point in the future.
1111
# mypy: strict-optional=False
12+
from __future__ import annotations
1213

1314
import importlib.util
1415
import logging
@@ -18,7 +19,7 @@
1819
from functools import partial
1920
from optparse import SUPPRESS_HELP, Option, OptionGroup, OptionParser, Values
2021
from textwrap import dedent
21-
from typing import Any, Callable, Optional
22+
from typing import Any, Callable
2223

2324
from pip._vendor.packaging.utils import canonicalize_name
2425

@@ -555,7 +556,7 @@ def only_binary() -> Option:
555556

556557

557558
# This was made a separate function for unit-testing purposes.
558-
def _convert_python_version(value: str) -> tuple[tuple[int, ...], Optional[str]]:
559+
def _convert_python_version(value: str) -> tuple[tuple[int, ...], str | None]:
559560
"""
560561
Convert a version string like "3", "37", or "3.7.3" into a tuple of ints.
561562

src/pip/_internal/cli/index_command.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
--uptodate) don't need waste time importing PipSession and friends.
77
"""
88

9+
from __future__ import annotations
10+
911
import logging
1012
import os
1113
import sys
1214
from functools import lru_cache
1315
from optparse import Values
14-
from typing import TYPE_CHECKING, Optional
16+
from typing import TYPE_CHECKING
1517

1618
from pip._vendor import certifi
1719

@@ -27,7 +29,7 @@
2729

2830

2931
@lru_cache
30-
def _create_truststore_ssl_context() -> Optional["SSLContext"]:
32+
def _create_truststore_ssl_context() -> SSLContext | None:
3133
if sys.version_info < (3, 10):
3234
logger.debug("Disabling truststore because Python version isn't 3.10+")
3335
return None
@@ -56,10 +58,10 @@ class SessionCommandMixin(CommandContextMixIn):
5658

5759
def __init__(self) -> None:
5860
super().__init__()
59-
self._session: Optional[PipSession] = None
61+
self._session: PipSession | None = None
6062

6163
@classmethod
62-
def _get_index_urls(cls, options: Values) -> Optional[list[str]]:
64+
def _get_index_urls(cls, options: Values) -> list[str] | None:
6365
"""Return a list of index urls from user-provided options."""
6466
index_urls = []
6567
if not getattr(options, "no_index", False):
@@ -72,7 +74,7 @@ def _get_index_urls(cls, options: Values) -> Optional[list[str]]:
7274
# Return None rather than an empty list
7375
return index_urls or None
7476

75-
def get_default_session(self, options: Values) -> "PipSession":
77+
def get_default_session(self, options: Values) -> PipSession:
7678
"""Get a default-managed session."""
7779
if self._session is None:
7880
self._session = self.enter_context(self._build_session(options))
@@ -85,9 +87,9 @@ def get_default_session(self, options: Values) -> "PipSession":
8587
def _build_session(
8688
self,
8789
options: Values,
88-
retries: Optional[int] = None,
89-
timeout: Optional[int] = None,
90-
) -> "PipSession":
90+
retries: int | None = None,
91+
timeout: int | None = None,
92+
) -> PipSession:
9193
from pip._internal.network.session import PipSession
9294

9395
cache_dir = options.cache_dir
@@ -134,7 +136,7 @@ def _build_session(
134136
return session
135137

136138

137-
def _pip_self_version_check(session: "PipSession", options: Values) -> None:
139+
def _pip_self_version_check(session: PipSession, options: Values) -> None:
138140
from pip._internal.self_outdated_check import pip_self_version_check as check
139141

140142
check(session, options)

0 commit comments

Comments
 (0)