Allow Version('') and map it to the empty tuple (#25953)

This commit is contained in:
Tamara Dahlgren 2021-10-08 01:36:54 -07:00 committed by GitHub
parent 169d0a5649
commit 7f2611a960
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View file

@ -614,3 +614,17 @@ def test_empty_version_range_raises():
assert VersionRange('2', '1.0')
with pytest.raises(ValueError):
assert ver('2:1.0')
def test_version_empty_slice():
"""Check an empty slice to confirm get "empty" version instead of
an IndexError (#25953).
"""
assert Version('1.')[1:] == Version('')
def test_version_wrong_idx_type():
"""Ensure exception raised if attempt to use non-integer index."""
v = Version('1.1')
with pytest.raises(TypeError):
v['0:']

View file

@ -177,7 +177,7 @@ def __init__(self, string):
string = string.strip()
self.string = string
if not VALID_VERSION.match(string):
if string and not VALID_VERSION.match(string):
raise ValueError("Bad characters in version string: %s" % string)
# An object that can lookup git commits to compare them to versions
@ -347,9 +347,12 @@ def __getitem__(self, idx):
string_arg.append(str(token))
string_arg.append(str(sep))
string_arg.pop() # We don't need the last separator
string_arg = ''.join(string_arg)
return cls(string_arg)
if string_arg:
string_arg.pop() # We don't need the last separator
string_arg = ''.join(string_arg)
return cls(string_arg)
else:
return Version('')
message = '{cls.__name__} indices must be integers'
raise TypeError(message.format(cls=cls))