-
-
Notifications
You must be signed in to change notification settings - Fork 672
fix(pypi): handle unnormalized package names when extracting sdist version #3635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rickeylev
merged 5 commits into
bazel-contrib:main
from
aignas:aignas.fix.version_from_filename
Feb 25, 2026
+112
−23
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| """Parse the version of the thing just from the filename. This is useful for selecting files based on the requested version.""" | ||
|
|
||
| _SDIST_EXTS = [ | ||
| ".tar", # handles any compression | ||
| ".zip", | ||
| ] | ||
|
|
||
| def version_from_filename(filename, _fail = None): | ||
| """Parse the version of the filename. | ||
|
|
||
| Args: | ||
| filename: {type}`str` the filename. | ||
| _fail: The fail function. | ||
|
|
||
| Returns: | ||
| A string version or None if we could not parse the version. | ||
| """ | ||
| # See https://packaging.python.org/en/latest/specifications/binary-distribution-format/#binary-distribution-format | ||
|
|
||
| if filename.endswith(".whl"): | ||
| # The format is {name}-{version}-{whl_specifiers}.whl | ||
| _, _, version = filename.partition("-") | ||
| version, _, _ = version.partition("-") | ||
| return version | ||
|
|
||
| # NOTE @aignas 2025-03-29: most of the files are wheels, so this is not the common path | ||
|
|
||
| # {name}-{version}.{ext} | ||
| head = "" | ||
| for ext in _SDIST_EXTS: | ||
| head, _, _ = filename.rpartition(ext) # build or name | ||
| if head: | ||
| break | ||
|
|
||
| if not head: | ||
| if _fail: | ||
| _fail("Unsupported sdist extension: {filename}".format(filename = filename)) | ||
| return None | ||
|
|
||
| # Based on PEP440 the version number cannot include dashes | ||
| _, _, version = head.rpartition("-") | ||
| return version | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| load(":version_from_filename_tests.bzl", "version_from_filename_test_suite") | ||
|
|
||
| version_from_filename_test_suite(name = "version_from_filename_tests") |
56 changes: 56 additions & 0 deletions
56
tests/pypi/version_from_filename/version_from_filename_tests.bzl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| "" | ||
|
|
||
| load("@rules_testing//lib:test_suite.bzl", "test_suite") | ||
| load("//python/private/pypi:version_from_filename.bzl", "version_from_filename") # buildifier: disable=bzl-visibility | ||
|
|
||
| _tests = [] | ||
|
|
||
| def _test_wheel_version_extraction(env): | ||
| # Case 1: wheel | ||
| env.expect.that_str(version_from_filename("foo-1.2.3-py3-none-any.whl")).equals("1.2.3") | ||
|
|
||
| _tests.append(_test_wheel_version_extraction) | ||
|
|
||
| def _test_sdist_version_extraction(env): | ||
| # Case 1: Standard sdist | ||
| env.expect.that_str(version_from_filename("foo-1.2.3.tar.gz")).equals("1.2.3") | ||
|
|
||
| # Case 2: PEP 625 - Project name has underscores (normalized from dashes) | ||
| # If the package is 'my-pkg', the sdist might be 'my_pkg-1.0.0.tar.gz' | ||
| env.expect.that_str(version_from_filename("my_pkg-1.0.0.tar.gz")).equals("1.0.0") | ||
|
|
||
| # Case 3: Project name has multiple underscores | ||
| env.expect.that_str(version_from_filename("very_long_project_name-0.5.0.zip")).equals("0.5.0") | ||
|
|
||
| # Case 4: Legacy sdist with hyphens in name | ||
| # Note: Modern tools normalize this, but we should support the hyphen split | ||
| env.expect.that_str(version_from_filename("complex-name-1.2.3.tar.gz")).equals("1.2.3") | ||
|
|
||
| # Case 5: Version contains an underscore (e.g. local versions) | ||
| env.expect.that_str(version_from_filename("pkg-1.2.3_post1.tar.gz")).equals("1.2.3_post1") | ||
|
|
||
| # Case 6: custom compression | ||
| env.expect.that_str(version_from_filename("pkg-1.2.3_post1.tar.xz")).equals("1.2.3_post1") | ||
|
|
||
| _tests.append(_test_sdist_version_extraction) | ||
|
|
||
| def _test_sdist_version_extraction_fail(env): | ||
| failures = [] | ||
|
|
||
| # Case 1: 7z | ||
| env.expect.that_str(version_from_filename("foo-1.2.3.7z")).equals(None) | ||
| env.expect.that_str(version_from_filename("foo-1.2.3.7z", _fail = failures.append)).equals(None) | ||
| env.expect.that_collection(failures).contains_exactly(["Unsupported sdist extension: foo-1.2.3.7z"]) | ||
|
|
||
| # Case 2: egg | ||
| failures.clear() | ||
| env.expect.that_str(version_from_filename("foo-1.2.3-py3.egg", _fail = failures.append)).equals(None) | ||
| env.expect.that_collection(failures).contains_exactly(["Unsupported sdist extension: foo-1.2.3-py3.egg"]) | ||
|
|
||
| _tests.append(_test_sdist_version_extraction_fail) | ||
|
|
||
| def version_from_filename_test_suite(name): | ||
| test_suite( | ||
| name = name, | ||
| basic_tests = _tests, | ||
| ) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.