Allow Version('') and map it to the empty tuple (#25953)
This commit is contained in:
		| @@ -614,3 +614,17 @@ def test_empty_version_range_raises(): | |||||||
|         assert VersionRange('2', '1.0') |         assert VersionRange('2', '1.0') | ||||||
|     with pytest.raises(ValueError): |     with pytest.raises(ValueError): | ||||||
|         assert ver('2:1.0') |         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:'] | ||||||
|   | |||||||
| @@ -177,7 +177,7 @@ def __init__(self, string): | |||||||
|         string = string.strip() |         string = string.strip() | ||||||
|         self.string = string |         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) |             raise ValueError("Bad characters in version string: %s" % string) | ||||||
| 
 | 
 | ||||||
|         # An object that can lookup git commits to compare them to versions |         # 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(token)) | ||||||
|                 string_arg.append(str(sep)) |                 string_arg.append(str(sep)) | ||||||
| 
 | 
 | ||||||
|  |             if string_arg: | ||||||
|                 string_arg.pop()  # We don't need the last separator |                 string_arg.pop()  # We don't need the last separator | ||||||
|                 string_arg = ''.join(string_arg) |                 string_arg = ''.join(string_arg) | ||||||
|                 return cls(string_arg) |                 return cls(string_arg) | ||||||
|  |             else: | ||||||
|  |                 return Version('') | ||||||
| 
 | 
 | ||||||
|         message = '{cls.__name__} indices must be integers' |         message = '{cls.__name__} indices must be integers' | ||||||
|         raise TypeError(message.format(cls=cls)) |         raise TypeError(message.format(cls=cls)) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Tamara Dahlgren
					Tamara Dahlgren