Skip to content
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

parse_{sdist,wheel}_filename: don't raise InvalidVersion #721

Merged
merged 1 commit into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Changelog
* Introduce ``metadata.Metadata`` (along with ``metadata.ExceptionGroup`` and ``metadata.InvalidMetadata``; :issue:`570`)
* Introduce the ``validate`` keyword parameter to ``utils.validate_name()`` (:issue:`570`)
* Introduce ``utils.is_normalized_name()`` (:issue:`570`)
* Make ``utils.parse_sdist_filename()` and ``utils.parse_wheel_filename()`
raise ``InvalidSdistFilename`` and ``InvalidWheelFilename``, respectively,
when the version component of the name is invalid

23.1 - 2023-04-12
~~~~~~~~~~~~~~~~~
Expand Down
18 changes: 16 additions & 2 deletions src/packaging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ def parse_wheel_filename(
if "__" in name_part or re.match(r"^[\w\d._]*$", name_part, re.UNICODE) is None:
raise InvalidWheelFilename(f"Invalid project name: {filename}")
name = canonicalize_name(name_part)
version = Version(parts[1])

try:
version = Version(parts[1])
except InvalidVersion as e:
raise InvalidWheelFilename(
f"Invalid wheel filename (invalid version): {filename}"
) from e

if dashes == 5:
build_part = parts[2]
build_match = _build_tag_regex.match(build_part)
Expand Down Expand Up @@ -154,5 +161,12 @@ def parse_sdist_filename(filename: str) -> Tuple[NormalizedName, Version]:
raise InvalidSdistFilename(f"Invalid sdist filename: {filename}")

name = canonicalize_name(name_part)
version = Version(version_part)

try:
version = Version(version_part)
except InvalidVersion as e:
raise InvalidSdistFilename(
f"Invalid sdist filename (invalid version): {filename}"
) from e

return (name, version)
10 changes: 9 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def test_parse_wheel_filename(filename, name, version, build, tags):
("foo-1.0-py3-none-any.wheel"), # Incorrect file extension (`.wheel`)
("foo__bar-1.0-py3-none-any.whl"), # Invalid name (`__`)
("foo#bar-1.0-py3-none-any.whl"), # Invalid name (`#`)
("foobar-1.x-py3-none-any.whl"), # Invalid version (`1.x`)
# Build number doesn't start with a digit (`abc`)
("foo-1.0-abc-py3-none-any.whl"),
("foo-1.0-200-py3-none-any-junk.whl"), # Too many dashes (`-junk`)
Expand All @@ -154,7 +155,14 @@ def test_parse_sdist_filename(filename, name, version):
assert parse_sdist_filename(filename) == (name, version)


@pytest.mark.parametrize(("filename"), [("foo-1.0.xz"), ("foo1.0.tar.gz")])
@pytest.mark.parametrize(
("filename"),
[
("foo-1.0.xz"), # Incorrect extension
("foo1.0.tar.gz"), # Missing separator
("foo-1.x.tar.gz"), # Invalid version
],
)
def test_parse_sdist_invalid_filename(filename):
with pytest.raises(InvalidSdistFilename):
parse_sdist_filename(filename)
Loading