Skip to content

Conversation

@notatallshaw
Copy link
Member

Inspired by @henryiii comment #999 (comment)

_get_spec_version is an internal one element cache in Specifier to save having to re-parse the version that Specifier is based on, we can use it in two extra places:

  • _canonical_spec when version is not a wild card, however a wild card will always fail to be turned into a Version object by canonicalize_version, so we can short-circuit and immediately return the wild card string.
  • prereleases property, again when version is not a wild card, however wild cards cannot imply prereleases, e.g. 1.0a1.* is not a valid version, so we can skip checking if wild cards imply prereleases and immediately return False.

Also I have added a new TestSpecifierInternal that validates that the underlying cache element _spec_version is initially set and doesn't change, as it is not trivial to deduce that from all the places it is used. Let me know if this is over the top, I can remove it.

@notatallshaw notatallshaw force-pushed the use-`_get_spec_version`-in-more-places-for-`Specifier` branch from cc84fdf to 6f71dc9 Compare November 30, 2025 21:50
@notatallshaw notatallshaw force-pushed the use-`_get_spec_version`-in-more-places-for-`Specifier` branch from 6f71dc9 to 48deb70 Compare November 30, 2025 21:52
@notatallshaw notatallshaw merged commit a70ead8 into pypa:main Dec 1, 2025
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants