Skip to content
Closed
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
6 changes: 3 additions & 3 deletions benchmarks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import annotations

from typing import TYPE_CHECKING, Any, TypeVar

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Callable
from typing import Any, TypeVar

F = TypeVar("F", bound="Callable[..., Any]")
F = TypeVar("F", bound="Callable[..., Any]")


def add_attributes(**attrs: object) -> Callable[[F], F]:
Expand Down
3 changes: 2 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import time
import urllib.request
from pathlib import Path
from typing import IO, TYPE_CHECKING

import nox

import packaging.version # will always be present with nox

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Generator
from typing import IO

nox.needs_version = ">=2025.02.09"
nox.options.reuse_existing_virtualenvs = True
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ ignore = [
flake8-comprehensions.allow-dict-calls-with-keyword-arguments = true
flake8-unused-arguments.ignore-variadic-names = true

[tool.ruff.lint.flake8-tidy-imports.banned-api]
"typing.TYPE_CHECKING".msg = "Use TYPE_CHECKING=False instead"

[tool.ruff.lint.per-file-ignores]
"tests/test_*.py" = ["PYI024", "PLR", "SIM201", "T20", "S301"]
"tasks/*.py" = ["T20"]
Expand Down
5 changes: 4 additions & 1 deletion src/packaging/_elffile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
import enum
import os
import struct
from typing import IO

TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import IO


class ELFInvalid(ValueError):
Expand Down
3 changes: 2 additions & 1 deletion src/packaging/_manylinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
import re
import sys
import warnings
from typing import TYPE_CHECKING, NamedTuple
from typing import NamedTuple

from ._elffile import EIClass, EIData, ELFFile, EMachine

TYPE_CHECKING = False
if TYPE_CHECKING:
Comment on lines +10 to 15

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pointless code churn

from collections.abc import Generator, Iterator, Sequence

Expand Down
3 changes: 2 additions & 1 deletion src/packaging/_musllinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import re
import subprocess
import sys
from typing import TYPE_CHECKING, NamedTuple
from typing import NamedTuple

from ._elffile import ELFFile

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Iterator, Sequence

Expand Down
8 changes: 2 additions & 6 deletions src/packaging/_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@

import enum
import functools
from typing import (
TYPE_CHECKING,
Any,
Final,
)

from .version import InvalidVersion, Version

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Callable, Iterable, Iterator, Sequence
from typing import Union
from typing import Any, Final, Union

__all__ = [
"FULL_RANGE",
Expand Down
3 changes: 2 additions & 1 deletion src/packaging/_tokenizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import contextlib
import re
from dataclasses import dataclass
from typing import TYPE_CHECKING, NoReturn

from .specifiers import Specifier

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Generator, Mapping
from typing import NoReturn


@dataclass
Expand Down
22 changes: 10 additions & 12 deletions src/packaging/direct_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@
import re
import urllib.parse
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, Protocol, TypeVar

TYPE_CHECKING = False
if TYPE_CHECKING: # pragma: no cover
import sys
from collections.abc import Collection
from typing import Any, Protocol, TypeVar

if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self

_T = TypeVar("_T")

class _FromMappingProtocol(Protocol): # pragma: no cover
@classmethod
def _from_dict(cls, d: Mapping[str, Any]) -> Self: ...

_FromMappingProtocolT = TypeVar("_FromMappingProtocolT", bound=_FromMappingProtocol)

__all__ = [
"ArchiveInfo",
"DirInfo",
Expand All @@ -28,17 +37,6 @@ def __dir__() -> list[str]:
return __all__


_T = TypeVar("_T")


class _FromMappingProtocol(Protocol): # pragma: no cover
@classmethod
def _from_dict(cls, d: Mapping[str, Any]) -> Self: ...


_FromMappingProtocolT = TypeVar("_FromMappingProtocolT", bound=_FromMappingProtocol)


def _json_dict_factory(data: list[tuple[str, Any]]) -> dict[str, Any]:
return {key: value for key, value in data if value is not None}

Expand Down
5 changes: 4 additions & 1 deletion src/packaging/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import contextlib
import dataclasses
import sys
import typing

TYPE_CHECKING = False
if TYPE_CHECKING:
import typing

__all__ = ["ExceptionGroup"]

Expand Down
3 changes: 2 additions & 1 deletion src/packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import platform
import sys
from collections.abc import Set as AbstractSet
from typing import TYPE_CHECKING, Callable, Literal, TypedDict, Union, cast
from typing import Callable, Literal, TypedDict, Union, cast

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

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Mapping

Expand Down
3 changes: 2 additions & 1 deletion src/packaging/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from . import version as version_module
from .errors import ExceptionGroup, _ErrorCollector

if typing.TYPE_CHECKING:
TYPE_CHECKING = False
if TYPE_CHECKING:
from .licenses import NormalizedLicenseExpression

T = typing.TypeVar("T")
Expand Down
2 changes: 1 addition & 1 deletion src/packaging/pylock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from dataclasses import dataclass
from datetime import datetime
from typing import (
TYPE_CHECKING,
Any,
Callable,
Protocol,
Expand All @@ -32,6 +31,7 @@
)
from .version import Version

TYPE_CHECKING = False
if TYPE_CHECKING: # pragma: no cover
from collections.abc import Collection, Iterator
from pathlib import Path
Expand Down
3 changes: 1 addition & 2 deletions src/packaging/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
# for complete details.
from __future__ import annotations

from typing import TYPE_CHECKING

from ._parser import parse_requirement as _parse_requirement
from ._tokenizer import ParserSyntaxError
from .markers import Marker, _normalize_extra_values
from .specifiers import SpecifierSet
from .utils import canonicalize_name

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Iterator

Expand Down
14 changes: 7 additions & 7 deletions src/packaging/specifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@

import abc
import re
import sys
import typing
from typing import (
TYPE_CHECKING,
Any,
Callable,
Final,
Expand All @@ -35,14 +33,16 @@
from .utils import canonicalize_version
from .version import InvalidVersion, Version

if sys.version_info >= (3, 10):
from typing import TypeGuard # pragma: no cover
elif TYPE_CHECKING:
from typing_extensions import TypeGuard

TYPE_CHECKING = False
if TYPE_CHECKING:
import sys
from collections.abc import Iterable, Iterator, Sequence

if sys.version_info >= (3, 10):
from typing import TypeGuard # pragma: no cover
elif TYPE_CHECKING:
from typing_extensions import TypeGuard

from ._ranges import VersionRange


Expand Down
7 changes: 2 additions & 5 deletions src/packaging/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
import sysconfig
from collections.abc import Iterable, Iterator, Sequence
from importlib.machinery import EXTENSION_SUFFIXES
from typing import (
TYPE_CHECKING,
TypeVar,
cast,
)
from typing import TypeVar, cast

from . import _manylinux, _musllinux

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Callable
from collections.abc import Set as AbstractSet
Expand Down
8 changes: 4 additions & 4 deletions src/packaging/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import re
import sys
import typing
from typing import (
Any,
Callable,
Expand All @@ -22,12 +21,13 @@
Union,
)

if typing.TYPE_CHECKING:
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing_extensions import Self, Unpack

if sys.version_info >= (3, 13): # pragma: no cover
from warnings import deprecated as _deprecated
elif typing.TYPE_CHECKING:
elif TYPE_CHECKING:
from typing_extensions import deprecated as _deprecated
else: # pragma: no cover
import functools
Expand Down Expand Up @@ -137,7 +137,7 @@ class _BaseVersion:
# This can also be a normal member (see the packaging_legacy package);
# we are just requiring it to be readable. Actually defining a property
# has runtime effect on subclasses, so it's typing only.
if typing.TYPE_CHECKING:
if TYPE_CHECKING:

@property
def _key(self) -> tuple[Any, ...]: ...
Expand Down
5 changes: 2 additions & 3 deletions tests/property/test_specifier_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from __future__ import annotations

import typing

import pytest
from hypothesis import assume, given
from hypothesis import strategies as st
Expand All @@ -20,7 +18,8 @@
specifier_sets,
)

if typing.TYPE_CHECKING:
TYPE_CHECKING = False
if TYPE_CHECKING:
from packaging.version import Version

pytestmark = pytest.mark.property
Expand Down
17 changes: 10 additions & 7 deletions tests/test_dependency_groups.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import annotations

import re
import sys
import unittest.mock
from typing import Any

import pytest

Expand All @@ -18,12 +16,17 @@
from packaging.errors import ExceptionGroup
from packaging.requirements import Requirement

if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias
TYPE_CHECKING = False
if TYPE_CHECKING:
import sys
from typing import Any

GroupsTable: TypeAlias = "dict[str, list[str | dict[str, str]]]"
if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias

GroupsTable: TypeAlias = "dict[str, list[str | dict[str, str]]]"


def _group_contains(
Expand Down
7 changes: 4 additions & 3 deletions tests/test_manylinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import types
import typing

if typing.TYPE_CHECKING:
from collections.abc import Generator

import pretend
import pytest

Expand All @@ -30,6 +27,10 @@
_parse_glibc_version,
)

TYPE_CHECKING = False
if TYPE_CHECKING:
from collections.abc import Generator


@pytest.fixture(autouse=True)
def clear_lru_cache() -> Generator[None, None, None]:
Expand Down
Loading
Loading