Skip to content

Commit 516e60a

Browse files
authored
chore: add ruff TC (#980)
* chore: add ruff TC Signed-off-by: Henry Schreiner <[email protected]> * chore: coverage should skip type checking blocks Signed-off-by: Henry Schreiner <[email protected]> * Refactor import of Self for compatibility --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent 312041e commit 516e60a

File tree

6 files changed

+16
-16
lines changed

6 files changed

+16
-16
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ source_pkgs = ["packaging"]
6464
[tool.coverage.report]
6565
show_missing = true
6666
fail_under = 100
67-
exclude_also = ["@abc.abstractmethod", "@abc.abstractproperty"]
67+
exclude_also = ["@abc.abstractmethod", "@abc.abstractproperty", "if typing.TYPE_CHECKING:"]
6868

6969
[tool.pytest.ini_options]
7070
minversion = "6.2"
@@ -123,6 +123,7 @@ extend-select = [
123123
"SLOT", # flake8-slots
124124
"T10", # flake8-debugger
125125
"T20", # flake8-print
126+
"TC", # flake8-type-checking
126127
"TRY", # tryceratops
127128
"UP", # pyupgrade
128129
"W",

src/packaging/licenses/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,6 @@ def canonicalize_license_expression(
142142
normalized_expression = " ".join(normalized_tokens)
143143

144144
return cast(
145-
NormalizedLicenseExpression,
145+
"NormalizedLicenseExpression",
146146
normalized_expression.replace("( ", "(").replace(" )", ")"),
147147
)

src/packaging/markers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def _repair_python_full_version(
356356
Work around platform.python_version() returning something that is not PEP 440
357357
compliant for non-tagged Python builds.
358358
"""
359-
python_full_version = cast(str, env["python_full_version"])
359+
python_full_version = cast("str", env["python_full_version"])
360360
if python_full_version.endswith("+"):
361361
env["python_full_version"] = f"{python_full_version}local"
362362
return env

src/packaging/metadata.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
from . import licenses, requirements, specifiers, utils
2222
from . import version as version_module
23-
from .licenses import NormalizedLicenseExpression
23+
24+
if typing.TYPE_CHECKING:
25+
from .licenses import NormalizedLicenseExpression
2426

2527
T = typing.TypeVar("T")
2628

@@ -498,7 +500,7 @@ def parse_email(data: bytes | str) -> tuple[RawMetadata, dict[str, list[str]]]:
498500
# Check to see if we've already got a description, if so then both
499501
# it, and this body move to unparsable.
500502
if "description" in raw:
501-
description_header = cast(str, raw.pop("description"))
503+
description_header = cast("str", raw.pop("description"))
502504
unparsed.setdefault("description", []).extend(
503505
[description_header, payload]
504506
)
@@ -511,7 +513,7 @@ def parse_email(data: bytes | str) -> tuple[RawMetadata, dict[str, list[str]]]:
511513
# literal key names, but we're computing our key names on purpose, but the
512514
# way this function is implemented, our `TypedDict` can only have valid key
513515
# names.
514-
return cast(RawMetadata, raw), unparsed
516+
return cast("RawMetadata", raw), unparsed
515517

516518

517519
_NOT_FOUND = object()
@@ -574,7 +576,7 @@ def __get__(self, instance: Metadata, _owner: type[Metadata]) -> T:
574576
except KeyError:
575577
pass
576578

577-
return cast(T, value)
579+
return cast("T", value)
578580

579581
def _invalid_metadata(
580582
self, msg: str, cause: Exception | None = None
@@ -589,7 +591,7 @@ def _process_metadata_version(self, value: str) -> _MetadataVersion:
589591
# Implicitly makes Metadata-Version required.
590592
if value not in _VALID_METADATA_VERSIONS:
591593
raise self._invalid_metadata(f"{value!r} is not a valid metadata version")
592-
return cast(_MetadataVersion, value)
594+
return cast("_MetadataVersion", value)
593595

594596
def _process_name(self, value: str) -> str:
595597
if not value:

src/packaging/pylock.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import dataclasses
44
import logging
55
import re
6-
import sys
76
from collections.abc import Mapping, Sequence
87
from dataclasses import dataclass
98
from datetime import datetime
10-
from pathlib import Path
119
from typing import (
1210
TYPE_CHECKING,
1311
Any,
@@ -22,10 +20,9 @@
2220
from .version import Version
2321

2422
if TYPE_CHECKING: # pragma: no cover
25-
if sys.version_info >= (3, 11):
26-
from typing import Self
27-
else:
28-
from typing_extensions import Self
23+
from pathlib import Path
24+
25+
from typing_extensions import Self
2926

3027
_logger = logging.getLogger(__name__)
3128

src/packaging/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def canonicalize_name(name: str, *, validate: bool = False) -> NormalizedName:
4848
raise InvalidName(f"name is invalid: {name!r}")
4949
# This is taken from PEP 503.
5050
value = _canonicalize_regex.sub("-", name).lower()
51-
return cast(NormalizedName, value)
51+
return cast("NormalizedName", value)
5252

5353

5454
def is_normalized_name(name: str) -> bool:
@@ -127,7 +127,7 @@ def parse_wheel_filename(
127127
raise InvalidWheelFilename(
128128
f"Invalid build number: {build_part} in {filename!r}"
129129
)
130-
build = cast(BuildTag, (int(build_match.group(1)), build_match.group(2)))
130+
build = cast("BuildTag", (int(build_match.group(1)), build_match.group(2)))
131131
else:
132132
build = ()
133133
tags = parse_tag(parts[-1])

0 commit comments

Comments
 (0)