Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernise type annotations using FA rules from ruff #785

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ extend-select = [
"B",
"E",
"F",
"FA",
"I",
"N",
"UP",
Expand Down
8 changes: 5 additions & 3 deletions src/packaging/_elffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
ELF header: https://refspecs.linuxfoundation.org/elf/gabi4+/ch4.eheader.html
"""

from __future__ import annotations

import enum
import os
import struct
from typing import IO, Optional, Tuple
from typing import IO


class ELFInvalid(ValueError):
Expand Down Expand Up @@ -87,11 +89,11 @@ def __init__(self, f: IO[bytes]) -> None:
except struct.error as e:
raise ELFInvalid("unable to parse machine and section information") from e

def _read(self, fmt: str) -> Tuple[int, ...]:
def _read(self, fmt: str) -> tuple[int, ...]:
return struct.unpack(fmt, self._f.read(struct.calcsize(fmt)))

@property
def interpreter(self) -> Optional[str]:
def interpreter(self) -> str | None:
"""
The path recorded in the ``PT_INTERP`` section header.
"""
Expand Down
20 changes: 11 additions & 9 deletions src/packaging/_manylinux.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

import collections
import contextlib
import functools
import os
import re
import sys
import warnings
from typing import Dict, Generator, Iterator, NamedTuple, Optional, Sequence, Tuple
from typing import Generator, Iterator, NamedTuple, Sequence

from ._elffile import EIClass, EIData, ELFFile, EMachine

Expand All @@ -17,7 +19,7 @@
# `os.PathLike` not a generic type until Python 3.9, so sticking with `str`
# as the type for `path` until then.
@contextlib.contextmanager
def _parse_elf(path: str) -> Generator[Optional[ELFFile], None, None]:
def _parse_elf(path: str) -> Generator[ELFFile | None, None, None]:
try:
with open(path, "rb") as f:
yield ELFFile(f)
Expand Down Expand Up @@ -72,15 +74,15 @@ def _have_compatible_abi(executable: str, archs: Sequence[str]) -> bool:
# For now, guess what the highest minor version might be, assume it will
# be 50 for testing. Once this actually happens, update the dictionary
# with the actual value.
_LAST_GLIBC_MINOR: Dict[int, int] = collections.defaultdict(lambda: 50)
_LAST_GLIBC_MINOR: dict[int, int] = collections.defaultdict(lambda: 50)


class _GLibCVersion(NamedTuple):
major: int
minor: int


def _glibc_version_string_confstr() -> Optional[str]:
def _glibc_version_string_confstr() -> str | None:
"""
Primary implementation of glibc_version_string using os.confstr.
"""
Expand All @@ -90,7 +92,7 @@ def _glibc_version_string_confstr() -> Optional[str]:
# https://github.com/python/cpython/blob/fcf1d003bf4f0100c/Lib/platform.py#L175-L183
try:
# Should be a string like "glibc 2.17".
version_string: Optional[str] = os.confstr("CS_GNU_LIBC_VERSION")
version_string: str | None = os.confstr("CS_GNU_LIBC_VERSION")
assert version_string is not None
_, version = version_string.rsplit()
except (AssertionError, AttributeError, OSError, ValueError):
Expand All @@ -99,7 +101,7 @@ def _glibc_version_string_confstr() -> Optional[str]:
return version


def _glibc_version_string_ctypes() -> Optional[str]:
def _glibc_version_string_ctypes() -> str | None:
"""
Fallback implementation of glibc_version_string using ctypes.
"""
Expand Down Expand Up @@ -143,12 +145,12 @@ def _glibc_version_string_ctypes() -> Optional[str]:
return version_str


def _glibc_version_string() -> Optional[str]:
def _glibc_version_string() -> str | None:
"""Returns glibc version string, or None if not using glibc."""
return _glibc_version_string_confstr() or _glibc_version_string_ctypes()


def _parse_glibc_version(version_str: str) -> Tuple[int, int]:
def _parse_glibc_version(version_str: str) -> tuple[int, int]:
"""Parse glibc version.

We use a regexp instead of str.split because we want to discard any
Expand All @@ -168,7 +170,7 @@ def _parse_glibc_version(version_str: str) -> Tuple[int, int]:


@functools.lru_cache
def _get_glibc_version() -> Tuple[int, int]:
def _get_glibc_version() -> tuple[int, int]:
version_str = _glibc_version_string()
if version_str is None:
return (-1, -1)
Expand Down
8 changes: 5 additions & 3 deletions src/packaging/_musllinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
linked against musl, and what musl version is used.
"""

from __future__ import annotations

import functools
import re
import subprocess
import sys
from typing import Iterator, NamedTuple, Optional, Sequence
from typing import Iterator, NamedTuple, Sequence

from ._elffile import ELFFile

Expand All @@ -18,7 +20,7 @@ class _MuslVersion(NamedTuple):
minor: int


def _parse_musl_version(output: str) -> Optional[_MuslVersion]:
def _parse_musl_version(output: str) -> _MuslVersion | None:
lines = [n for n in (n.strip() for n in output.splitlines()) if n]
if len(lines) < 2 or lines[0][:4] != "musl":
return None
Expand All @@ -29,7 +31,7 @@ def _parse_musl_version(output: str) -> Optional[_MuslVersion]:


@functools.lru_cache
def _get_musl_version(executable: str) -> Optional[_MuslVersion]:
def _get_musl_version(executable: str) -> _MuslVersion | None:
"""Detect currently-running musl runtime version.

This is done by checking the specified executable's dynamic linking
Expand Down
24 changes: 11 additions & 13 deletions src/packaging/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
the implementation.
"""

from __future__ import annotations

import ast
from typing import Any, List, NamedTuple, Optional, Tuple, Union
from typing import NamedTuple, Sequence, Tuple, Union

from ._tokenizer import DEFAULT_RULES, Tokenizer

Expand Down Expand Up @@ -41,20 +43,16 @@ def serialize(self) -> str:

MarkerVar = Union[Variable, Value]
MarkerItem = Tuple[MarkerVar, Op, MarkerVar]
# MarkerAtom = Union[MarkerItem, List["MarkerAtom"]]
# MarkerList = List[Union["MarkerList", MarkerAtom, str]]
# mypy does not support recursive type definition
# https://github.com/python/mypy/issues/731
MarkerAtom = Any
MarkerList = List[Any]
MarkerAtom = Union[MarkerItem, Sequence["MarkerAtom"]]
MarkerList = Sequence[Union["MarkerList", MarkerAtom, str]]


class ParsedRequirement(NamedTuple):
name: str
url: str
extras: List[str]
extras: list[str]
specifier: str
marker: Optional[MarkerList]
marker: MarkerList | None


# --------------------------------------------------------------------------------------
Expand Down Expand Up @@ -87,7 +85,7 @@ def _parse_requirement(tokenizer: Tokenizer) -> ParsedRequirement:

def _parse_requirement_details(
tokenizer: Tokenizer,
) -> Tuple[str, str, Optional[MarkerList]]:
) -> tuple[str, str, MarkerList | None]:
"""
requirement_details = AT URL (WS requirement_marker?)?
| specifier WS? (requirement_marker)?
Expand Down Expand Up @@ -156,7 +154,7 @@ def _parse_requirement_marker(
return marker


def _parse_extras(tokenizer: Tokenizer) -> List[str]:
def _parse_extras(tokenizer: Tokenizer) -> list[str]:
"""
extras = (LEFT_BRACKET wsp* extras_list? wsp* RIGHT_BRACKET)?
"""
Expand All @@ -175,11 +173,11 @@ def _parse_extras(tokenizer: Tokenizer) -> List[str]:
return extras


def _parse_extras_list(tokenizer: Tokenizer) -> List[str]:
def _parse_extras_list(tokenizer: Tokenizer) -> list[str]:
"""
extras_list = identifier (wsp* ',' wsp* identifier)*
"""
extras: List[str] = []
extras: list[str] = []

if not tokenizer.check("IDENTIFIER"):
return extras
Expand Down
18 changes: 10 additions & 8 deletions src/packaging/_tokenizer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import annotations

import contextlib
import re
from dataclasses import dataclass
from typing import Dict, Iterator, NoReturn, Optional, Tuple, Union
from typing import Iterator, NoReturn

from .specifiers import Specifier

Expand All @@ -21,7 +23,7 @@ def __init__(
message: str,
*,
source: str,
span: Tuple[int, int],
span: tuple[int, int],
) -> None:
self.span = span
self.message = message
Expand All @@ -34,7 +36,7 @@ def __str__(self) -> str:
return "\n ".join([self.message, self.source, marker])


DEFAULT_RULES: "Dict[str, Union[str, re.Pattern[str]]]" = {
DEFAULT_RULES: dict[str, str | re.Pattern[str]] = {
"LEFT_PARENTHESIS": r"\(",
"RIGHT_PARENTHESIS": r"\)",
"LEFT_BRACKET": r"\[",
Expand Down Expand Up @@ -96,13 +98,13 @@ def __init__(
self,
source: str,
*,
rules: "Dict[str, Union[str, re.Pattern[str]]]",
rules: dict[str, str | re.Pattern[str]],
) -> None:
self.source = source
self.rules: Dict[str, re.Pattern[str]] = {
self.rules: dict[str, re.Pattern[str]] = {
name: re.compile(pattern) for name, pattern in rules.items()
}
self.next_token: Optional[Token] = None
self.next_token: Token | None = None
self.position = 0

def consume(self, name: str) -> None:
Expand Down Expand Up @@ -154,8 +156,8 @@ def raise_syntax_error(
self,
message: str,
*,
span_start: Optional[int] = None,
span_end: Optional[int] = None,
span_start: int | None = None,
span_end: int | None = None,
) -> NoReturn:
"""Raise ParserSyntaxError at the given position."""
span = (
Expand Down
36 changes: 15 additions & 21 deletions src/packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

from __future__ import annotations

import operator
import os
import platform
import sys
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

from ._parser import (
MarkerAtom,
MarkerList,
Op,
Value,
Variable,
)
from ._parser import (
parse_marker as _parse_marker,
)
from typing import Any, Callable

from ._parser import MarkerAtom, MarkerList, Op, Value, Variable
from ._parser import parse_marker as _parse_marker
from ._tokenizer import ParserSyntaxError
from .specifiers import InvalidSpecifier, Specifier
from .utils import canonicalize_name
Expand Down Expand Up @@ -69,7 +63,7 @@ def _normalize_extra_values(results: Any) -> Any:


def _format_marker(
marker: Union[List[str], MarkerAtom, str], first: Optional[bool] = True
marker: list[str] | MarkerAtom | str, first: bool | None = True
) -> str:
assert isinstance(marker, (list, tuple, str))

Expand All @@ -96,7 +90,7 @@ def _format_marker(
return marker


_operators: Dict[str, Operator] = {
_operators: dict[str, Operator] = {
"in": lambda lhs, rhs: lhs in rhs,
"not in": lambda lhs, rhs: lhs not in rhs,
"<": operator.lt,
Expand All @@ -116,14 +110,14 @@ def _eval_op(lhs: str, op: Op, rhs: str) -> bool:
else:
return spec.contains(lhs, prereleases=True)

oper: Optional[Operator] = _operators.get(op.serialize())
oper: Operator | None = _operators.get(op.serialize())
if oper is None:
raise UndefinedComparison(f"Undefined {op!r} on {lhs!r} and {rhs!r}.")

return oper(lhs, rhs)


def _normalize(*values: str, key: str) -> Tuple[str, ...]:
def _normalize(*values: str, key: str) -> tuple[str, ...]:
# PEP 685 – Comparison of extra names for optional distribution dependencies
# https://peps.python.org/pep-0685/
# > When comparing extra names, tools MUST normalize the names being
Expand All @@ -135,8 +129,8 @@ def _normalize(*values: str, key: str) -> Tuple[str, ...]:
return values


def _evaluate_markers(markers: MarkerList, environment: Dict[str, str]) -> bool:
groups: List[List[bool]] = [[]]
def _evaluate_markers(markers: MarkerList, environment: dict[str, str]) -> bool:
groups: list[list[bool]] = [[]]

for marker in markers:
assert isinstance(marker, (list, tuple, str))
Expand Down Expand Up @@ -165,15 +159,15 @@ def _evaluate_markers(markers: MarkerList, environment: Dict[str, str]) -> bool:
return any(all(item) for item in groups)


def format_full_version(info: "sys._version_info") -> str:
def format_full_version(info: sys._version_info) -> str:
version = "{0.major}.{0.minor}.{0.micro}".format(info)
kind = info.releaselevel
if kind != "final":
version += kind[0] + str(info.serial)
return version


def default_environment() -> Dict[str, str]:
def default_environment() -> dict[str, str]:
iver = format_full_version(sys.implementation.version)
implementation_name = sys.implementation.name
return {
Expand Down Expand Up @@ -232,7 +226,7 @@ def __eq__(self, other: Any) -> bool:

return str(self) == str(other)

def evaluate(self, environment: Optional[Dict[str, str]] = None) -> bool:
def evaluate(self, environment: dict[str, str] | None = None) -> bool:
"""Evaluate a marker.

Return the boolean from evaluating the given marker against the
Expand Down
Loading