Skip to content

Commit 5bf7018

Browse files
authored
perf: use version.__replace__ in specifier comparison (#999)
1 parent 7cafa45 commit 5bf7018

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

src/packaging/specifiers.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,11 @@ def _coerce_version(version: UnparsedVersion) -> Version | None:
3333

3434

3535
def _public_version(version: Version) -> Version:
36-
"""Skip creation of a new Version instance if no local version to strip."""
37-
if version.local is None:
38-
return version
39-
return Version(version.public)
36+
return version.__replace__(local=None)
4037

4138

4239
def _base_version(version: Version) -> Version:
43-
"""Skip creation of a new Version instance if already a base version."""
44-
if (
45-
version.pre is None
46-
and version.post is None
47-
and version.dev is None
48-
and version.local is None
49-
):
50-
return version
51-
return Version(version.base_version)
40+
return version.__replace__(pre=None, post=None, dev=None, local=None)
5241

5342

5443
class InvalidSpecifier(ValueError):
@@ -437,7 +426,7 @@ def _compare_equal(self, prospective: Version, spec: str) -> bool:
437426
if spec.endswith(".*"):
438427
# In the case of prefix matching we want to ignore local segment.
439428
normalized_prospective = canonicalize_version(
440-
prospective.public, strip_trailing_zero=False
429+
_public_version(prospective), strip_trailing_zero=False
441430
)
442431
# Get the normalized version string ignoring the trailing .*
443432
normalized_spec = canonicalize_version(spec[:-2], strip_trailing_zero=False)
@@ -505,7 +494,7 @@ def _compare_less_than(self, prospective: Version, spec_str: str) -> bool:
505494
if (
506495
not spec.is_prerelease
507496
and prospective.is_prerelease
508-
and Version(prospective.base_version) == _base_version(spec)
497+
and _base_version(prospective) == _base_version(spec)
509498
):
510499
return False
511500

@@ -532,14 +521,14 @@ def _compare_greater_than(self, prospective: Version, spec_str: str) -> bool:
532521
if (
533522
not spec.is_postrelease
534523
and prospective.is_postrelease
535-
and Version(prospective.base_version) == _base_version(spec)
524+
and _base_version(prospective) == _base_version(spec)
536525
):
537526
return False
538527

539528
# Ensure that we do not allow a local version of the version mentioned
540529
# in the specifier, which is technically greater than, to match.
541-
if prospective.local is not None and Version(
542-
prospective.base_version
530+
if prospective.local is not None and _base_version(
531+
prospective
543532
) == _base_version(spec):
544533
return False
545534

0 commit comments

Comments
 (0)