Skip to content

Commit

Permalink
Fix canonicalize_version to accept Version instances (#328)
Browse files Browse the repository at this point in the history
* Fix `canonicalize_version` to accept Version instances

* Update utils.py

* Update test_utils.py
  • Loading branch information
ofek committed Aug 3, 2020
1 parent 1a7f729 commit 34ec49f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packaging/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ def canonicalize_name(name):
return cast("NormalizedName", value)


def canonicalize_version(_version):
# type: (str) -> Union[Version, str]
def canonicalize_version(version):
# type: (Union[Version, str]) -> Union[Version, str]
"""
This is very similar to Version.__str__, but has one subtle difference
with the way it handles the release segment.
"""

try:
version = Version(_version)
except InvalidVersion:
# Legacy versions cannot be normalized
return _version
if not isinstance(version, Version):
try:
version = Version(version)
except InvalidVersion:
# Legacy versions cannot be normalized
return version

parts = []

Expand Down
2 changes: 2 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from packaging.utils import canonicalize_name, canonicalize_version
from packaging.version import Version


@pytest.mark.parametrize(
Expand All @@ -30,6 +31,7 @@ def test_canonicalize_name(name, expected):
@pytest.mark.parametrize(
("version", "expected"),
[
(Version("1.4.0"), "1.4"),
("1.4.0", "1.4"),
("1.40.0", "1.40"),
("1.4.0.0.00.000.0000", "1.4"),
Expand Down

0 comments on commit 34ec49f

Please sign in to comment.