Skip to content

Commit

Permalink
Refactor type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek committed Dec 28, 2023
1 parent 7698237 commit d75c57e
Show file tree
Hide file tree
Showing 15 changed files with 264 additions and 262 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ exclude = ["docs/_build", "tests/manylinux/build-hello-world.sh", "tests/musllin

[tool.coverage.run]
branch = true
omit = ["_types.py"]

[tool.coverage.report]
exclude_lines = ["pragma: no cover", "@abc.abstractmethod", "@abc.abstractproperty"]
exclude_lines = ["pragma: no cover", "@abc.abstractmethod", "@abc.abstractproperty", "if TYPE_CHECKING:"]


[tool.mypy]
Expand Down
7 changes: 4 additions & 3 deletions src/packaging/_elffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
Based on: https://gist.github.com/lyssdod/f51579ae8d93c8657a5564aefc2ffbca
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 +88,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
18 changes: 10 additions & 8 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 @@ -64,15 +66,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 @@ -91,7 +93,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 @@ -135,12 +137,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 @@ -160,7 +162,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
7 changes: 4 additions & 3 deletions src/packaging/_musllinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
This module implements logic to detect if the currently running Python is
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 +19,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 +30,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
28 changes: 11 additions & 17 deletions src/packaging/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
The docstring for each __parse_* function contains ENBF-inspired grammar representing
the implementation.
"""
from __future__ import annotations

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

from ._tokenizer import DEFAULT_RULES, Tokenizer

if TYPE_CHECKING:
from ._types import MarkerAtom, MarkerItem, MarkerList, MarkerVar


class Node:
def __init__(self, value: str) -> None:
Expand Down Expand Up @@ -39,22 +43,12 @@ def serialize(self) -> str:
return str(self)


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]


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 +81,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 +150,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 +169,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
3 changes: 2 additions & 1 deletion src/packaging/_structures.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# This file is dual licensed under the terms of the Apache License, Version
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.
from __future__ import annotations


class InfinityType:
Expand All @@ -25,7 +26,7 @@ def __gt__(self, other: object) -> bool:
def __ge__(self, other: object) -> bool:
return True

def __neg__(self: object) -> "NegativeInfinityType":
def __neg__(self: object) -> NegativeInfinityType:
return NegativeInfinity


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
46 changes: 46 additions & 0 deletions src/packaging/_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Types used internally.
This module defines types separately so that there is no runtime cost.
"""
from __future__ import annotations

from typing import Any, Callable, List, NewType, Sequence, Tuple, TypeVar, Union

from ._parser import Op, Value, Variable
from ._structures import InfinityType, NegativeInfinityType
from .version import Version

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]

UnparsedVersion = Union[Version, str]
UnparsedVersionVar = TypeVar("UnparsedVersionVar", bound=UnparsedVersion)
CallableOperator = Callable[[Version, str], bool]

PythonVersion = Sequence[int]
MacVersion = Tuple[int, int]

BuildTag = Union[Tuple[()], Tuple[int, str]]
NormalizedName = NewType("NormalizedName", str)

LocalType = Tuple[Union[int, str], ...]

CmpPrePostDevType = Union[InfinityType, NegativeInfinityType, Tuple[str, int]]
CmpLocalType = Union[
NegativeInfinityType,
Tuple[Union[Tuple[int, str], Tuple[NegativeInfinityType, Union[int, str]]], ...],
]
CmpKey = Tuple[
int,
Tuple[int, ...],
CmpPrePostDevType,
CmpPrePostDevType,
CmpPrePostDevType,
CmpLocalType,
]
Loading

0 comments on commit d75c57e

Please sign in to comment.