Skip to content

Commit 7cafa45

Browse files
authored
perf: add __slots__ to Specifiers (#1002) (#1004)
1 parent f88ea3c commit 7cafa45

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

src/packaging/specifiers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ class InvalidSpecifier(ValueError):
6565

6666
class BaseSpecifier(metaclass=abc.ABCMeta):
6767
__slots__ = ()
68+
__match_args__ = ("_str",)
69+
70+
@property
71+
def _str(self) -> str:
72+
"""Internal property for match_args"""
73+
return str(self)
6874

6975
@abc.abstractmethod
7076
def __str__(self) -> str:

src/packaging/version.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ class Version(_BaseVersion):
268268
"""
269269

270270
__slots__ = ("_dev", "_epoch", "_key_cache", "_local", "_post", "_pre", "_release")
271+
__match_args__ = ("_str",)
272+
273+
_regex = re.compile(r"\s*" + VERSION_PATTERN + r"\s*", re.VERBOSE | re.IGNORECASE)
271274

272275
_epoch: int
273276
_release: tuple[int, ...]
@@ -278,8 +281,6 @@ class Version(_BaseVersion):
278281

279282
_key_cache: CmpKey | None
280283

281-
_regex = re.compile(r"\s*" + VERSION_PATTERN + r"\s*", re.VERBOSE | re.IGNORECASE)
282-
283284
def __init__(self, version: str) -> None:
284285
"""Initialize a Version object.
285286
@@ -386,6 +387,11 @@ def __str__(self) -> str:
386387

387388
return "".join(parts)
388389

390+
@property
391+
def _str(self) -> str:
392+
"""Internal property for match_args"""
393+
return str(self)
394+
389395
@property
390396
def epoch(self) -> int:
391397
"""The epoch of the version.

tests/test_specifiers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class TestSpecifier:
4646
def test_specifiers_valid(self, specifier: str) -> None:
4747
Specifier(specifier)
4848

49+
def test_match_args(self) -> None:
50+
assert Specifier.__match_args__ == ("_str",)
51+
assert Specifier(">=1.0")._str == ">=1.0"
52+
4953
@pytest.mark.parametrize(
5054
"specifier",
5155
[
@@ -829,6 +833,10 @@ def test_create_from_specifiers(self) -> None:
829833
spec = SpecifierSet(iter(specs))
830834
assert set(spec) == set(specs)
831835

836+
def test_match_args(self) -> None:
837+
assert SpecifierSet.__match_args__ == ("_str",)
838+
assert SpecifierSet(">=1.0,<2")._str == str(SpecifierSet(">=1.0,<2"))
839+
832840
@pytest.mark.parametrize(
833841
(
834842
"initial_prereleases",

tests/test_version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ class TestVersion:
108108
def test_valid_versions(self, version: str) -> None:
109109
Version(version)
110110

111+
def test_match_args(self) -> None:
112+
assert Version.__match_args__ == ("_str",)
113+
assert Version("1.2")._str == "1.2"
114+
111115
@pytest.mark.parametrize(
112116
"version",
113117
[

0 commit comments

Comments
 (0)