Commit d99cb602 by Sébastien Eustace

Keep original version's precision when expanding constraints

parent 4113773a
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
### Changed ### Changed
- Improved the `show` command to make it easier to check if packages are properly installed. - Improved the `show` command to make it easier to check if packages are properly installed.
- Expanded version constraints now keep the original version's precision.
## [0.9.1] - 2018-05-18 ## [0.9.1] - 2018-05-18
......
...@@ -15,14 +15,24 @@ class Version(VersionRange): ...@@ -15,14 +15,24 @@ class Version(VersionRange):
A parsed semantic version number. A parsed semantic version number.
""" """
def __init__(self, major, minor=None, patch=None, pre=None, build=None, text=None def __init__(self,
): # type: (int, int, int, Union[str, None], Union[str, None], Union[str, None]) major, # type: int
minor=None, # type: Union[int, None]
patch=None, # type: Union[int, None]
pre=None, # type: Union[str, None]
build=None, # type: Union[str, None]
text=None, # type: Union[str, None]
precision=None # type: Union[str, None]
): # type: () -> None
self._major = int(major) self._major = int(major)
self._precision = None
if precision is None:
self._precision = 1 self._precision = 1
if minor is None: if minor is None:
minor = 0 minor = 0
else: else:
if self._precision is not None:
self._precision += 1 self._precision += 1
self._minor = int(minor) self._minor = int(minor)
...@@ -30,12 +40,23 @@ class Version(VersionRange): ...@@ -30,12 +40,23 @@ class Version(VersionRange):
if patch is None: if patch is None:
patch = 0 patch = 0
else: else:
if self._precision is not None:
self._precision += 1 self._precision += 1
if precision is not None:
self._precision = precision
self._patch = int(patch) self._patch = int(patch)
if text is None: if text is None:
text = '{}.{}.{}'.format(major, minor, patch) parts = [str(major)]
if self._precision >= 2 or minor != 0:
parts.append(str(minor))
if self._precision >= 3 or patch != 0:
parts.append(str(patch))
text = '.'.join(parts)
if pre: if pre:
text += '-{}'.format(pre) text += '-{}'.format(pre)
...@@ -222,13 +243,13 @@ class Version(VersionRange): ...@@ -222,13 +243,13 @@ class Version(VersionRange):
return self return self
def _increment_major(self): # type: () -> Version def _increment_major(self): # type: () -> Version
return Version(self.major + 1, 0, 0) return Version(self.major + 1, 0, 0, precision=self._precision)
def _increment_minor(self): # type: () -> Version def _increment_minor(self): # type: () -> Version
return Version(self.major, self.minor + 1, 0) return Version(self.major, self.minor + 1, 0, precision=self._precision)
def _increment_patch(self): # type: () -> Version def _increment_patch(self): # type: () -> Version
return Version(self.major, self.minor, self.patch + 1) return Version(self.major, self.minor, self.patch + 1, precision=self._precision)
def _normalize_prerelease(self, pre): # type: (str) -> str def _normalize_prerelease(self, pre): # type: (str) -> str
if not pre: if not pre:
......
...@@ -14,8 +14,8 @@ def command(): ...@@ -14,8 +14,8 @@ def command():
('0.0.0', 'patch', '0.0.1'), ('0.0.0', 'patch', '0.0.1'),
('0.0.0', 'minor', '0.1.0'), ('0.0.0', 'minor', '0.1.0'),
('0.0.0', 'major', '1.0.0'), ('0.0.0', 'major', '1.0.0'),
('0.0', 'major', '1.0.0'), ('0.0', 'major', '1.0'),
('0.0', 'minor', '0.1.0'), ('0.0', 'minor', '0.1'),
('0.0', 'patch', '0.0.1'), ('0.0', 'patch', '0.0.1'),
('1.2.3', 'patch', '1.2.4'), ('1.2.3', 'patch', '1.2.4'),
('1.2.3', 'minor', '1.3.0'), ('1.2.3', 'minor', '1.3.0'),
......
...@@ -17,7 +17,7 @@ python-versions = "*" ...@@ -17,7 +17,7 @@ python-versions = "*"
platform = "*" platform = "*"
[package.requirements] [package.requirements]
python = ">=2.4,<2.5.0" python = ">=2.4,<2.5"
[[package]] [[package]]
name = "C" name = "C"
...@@ -32,7 +32,7 @@ platform = "*" ...@@ -32,7 +32,7 @@ platform = "*"
D = "^1.2" D = "^1.2"
[package.requirements] [package.requirements]
python = ">=2.7,<2.8.0 || >=3.4,<4.0.0" python = ">=2.7,<2.8 || >=3.4,<4.0"
[[package]] [[package]]
name = "D" name = "D"
...@@ -44,7 +44,7 @@ python-versions = "*" ...@@ -44,7 +44,7 @@ python-versions = "*"
platform = "*" platform = "*"
[package.requirements] [package.requirements]
python = ">=2.7,<2.8.0 || >=3.4,<4.0.0" python = ">=2.7,<2.8 || >=3.4,<4.0"
[metadata] [metadata]
python-versions = "~2.7 || ^3.4" python-versions = "~2.7 || ^3.4"
......
...@@ -114,7 +114,7 @@ License: MIT ...@@ -114,7 +114,7 @@ License: MIT
Keywords: packaging,dependency,poetry Keywords: packaging,dependency,poetry
Author: Sébastien Eustace Author: Sébastien Eustace
Author-email: sebastien@eustace.io Author-email: sebastien@eustace.io
Requires-Python: >=3.6,<4.0.0 Requires-Python: >=3.6,<4.0
Classifier: License :: OSI Approved :: MIT License Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.6
...@@ -123,8 +123,8 @@ Classifier: Topic :: Software Development :: Build Tools ...@@ -123,8 +123,8 @@ Classifier: Topic :: Software Development :: Build Tools
Classifier: Topic :: Software Development :: Libraries :: Python Modules Classifier: Topic :: Software Development :: Libraries :: Python Modules
Provides-Extra: time Provides-Extra: time
Requires-Dist: cachy[msgpack] (>=0.2.0,<0.3.0) Requires-Dist: cachy[msgpack] (>=0.2.0,<0.3.0)
Requires-Dist: cleo (>=0.6,<0.7.0) Requires-Dist: cleo (>=0.6,<0.7)
Requires-Dist: pendulum (>=1.4,<2.0.0); extra == "time" Requires-Dist: pendulum (>=1.4,<2.0); extra == "time"
Description-Content-Type: text/x-rst Description-Content-Type: text/x-rst
My Package My Package
......
...@@ -48,8 +48,8 @@ def test_convert_dependencies(): ...@@ -48,8 +48,8 @@ def test_convert_dependencies():
] ]
) )
main = [ main = [
'A>=1.0,<2.0.0', 'A>=1.0,<2.0',
'B>=1.0,<1.1.0', 'B>=1.0,<1.1',
'C==1.2.3', 'C==1.2.3',
] ]
extras = {} extras = {}
...@@ -70,7 +70,7 @@ def test_convert_dependencies(): ...@@ -70,7 +70,7 @@ def test_convert_dependencies():
] ]
) )
main = [ main = [
'B>=1.0,<1.1.0', 'B>=1.0,<1.1',
'C==1.2.3', 'C==1.2.3',
] ]
extras = { extras = {
...@@ -98,16 +98,16 @@ def test_convert_dependencies(): ...@@ -98,16 +98,16 @@ def test_convert_dependencies():
] ]
) )
main = [ main = [
'B>=1.0,<1.1.0', 'B>=1.0,<1.1',
] ]
extra_python = ( extra_python = (
':(python_version >= "2.7" and python_version < "2.8.0") ' ':(python_version >= "2.7" and python_version < "2.8") '
'or (python_version >= "3.6" and python_version < "4.0.0")' 'or (python_version >= "3.6" and python_version < "4.0")'
) )
extra_d_dependency = ( extra_d_dependency = (
'baz:(python_version >= "2.7" and python_version < "2.8.0") ' 'baz:(python_version >= "2.7" and python_version < "2.8") '
'or (python_version >= "3.4" and python_version < "4.0.0")' 'or (python_version >= "3.4" and python_version < "4.0")'
) )
extras = { extras = {
extra_python: ['C==1.2.3'], extra_python: ['C==1.2.3'],
...@@ -134,7 +134,7 @@ def test_make_setup(): ...@@ -134,7 +134,7 @@ def test_make_setup():
] ]
assert ns['install_requires'] == [ assert ns['install_requires'] == [
'cachy[msgpack]>=0.2.0,<0.3.0', 'cachy[msgpack]>=0.2.0,<0.3.0',
'cleo>=0.6,<0.7.0', 'cleo>=0.6,<0.7',
] ]
assert ns['entry_points'] == { assert ns['entry_points'] == {
'console_scripts': [ 'console_scripts': [
...@@ -144,7 +144,7 @@ def test_make_setup(): ...@@ -144,7 +144,7 @@ def test_make_setup():
} }
assert ns['extras_require'] == { assert ns['extras_require'] == {
'time': [ 'time': [
'pendulum>=1.4,<2.0.0' 'pendulum>=1.4,<2.0'
] ]
} }
......
...@@ -59,15 +59,15 @@ def test_to_pep_508(): ...@@ -59,15 +59,15 @@ def test_to_pep_508():
dependency = Dependency('Django', '^1.23') dependency = Dependency('Django', '^1.23')
result = dependency.to_pep_508() result = dependency.to_pep_508()
assert result == 'Django (>=1.23,<2.0.0)' assert result == 'Django (>=1.23,<2.0)'
dependency = Dependency('Django', '^1.23') dependency = Dependency('Django', '^1.23')
dependency.python_versions = '~2.7 || ^3.6' dependency.python_versions = '~2.7 || ^3.6'
result = dependency.to_pep_508() result = dependency.to_pep_508()
assert result == 'Django (>=1.23,<2.0.0); ' \ assert result == 'Django (>=1.23,<2.0); ' \
'(python_version >= "2.7" and python_version < "2.8.0") ' \ '(python_version >= "2.7" and python_version < "2.8") ' \
'or (python_version >= "3.6" and python_version < "4.0.0")' 'or (python_version >= "3.6" and python_version < "4.0")'
def test_to_pep_508_wilcard(): def test_to_pep_508_wilcard():
...@@ -82,21 +82,21 @@ def test_to_pep_508_in_extras(): ...@@ -82,21 +82,21 @@ def test_to_pep_508_in_extras():
dependency.in_extras.append('foo') dependency.in_extras.append('foo')
result = dependency.to_pep_508() result = dependency.to_pep_508()
assert result == 'Django (>=1.23,<2.0.0); extra == "foo"' assert result == 'Django (>=1.23,<2.0); extra == "foo"'
dependency.in_extras.append('bar') dependency.in_extras.append('bar')
result = dependency.to_pep_508() result = dependency.to_pep_508()
assert result == 'Django (>=1.23,<2.0.0); extra == "foo" or extra == "bar"' assert result == 'Django (>=1.23,<2.0); extra == "foo" or extra == "bar"'
dependency.python_versions = '~2.7 || ^3.6' dependency.python_versions = '~2.7 || ^3.6'
result = dependency.to_pep_508() result = dependency.to_pep_508()
assert result == ( assert result == (
'Django (>=1.23,<2.0.0); ' 'Django (>=1.23,<2.0); '
'(' '('
'(python_version >= "2.7" and python_version < "2.8.0") ' '(python_version >= "2.7" and python_version < "2.8") '
'or (python_version >= "3.6" and python_version < "4.0.0")' 'or (python_version >= "3.6" and python_version < "4.0")'
') ' ') '
'and (extra == "foo" or extra == "bar")' 'and (extra == "foo" or extra == "bar")'
) )
...@@ -120,3 +120,25 @@ def test_parse_constraint_multi(input): ...@@ -120,3 +120,25 @@ def test_parse_constraint_multi(input):
) )
def test_parse_constraints_negative_wildcard(input, constraint): def test_parse_constraints_negative_wildcard(input, constraint):
assert parse_constraint(input) == constraint assert parse_constraint(input) == constraint
@pytest.mark.parametrize(
'input, expected',
[
('1', '1'),
('1.2', '1.2'),
('1.2.3', '1.2.3'),
('!=1', '<1 || >1'),
('!=1.2', '<1.2 || >1.2'),
('!=1.2.3', '<1.2.3 || >1.2.3'),
('^1', '>=1,<2'),
('^1.0', '>=1.0,<2.0'),
('^1.0.0', '>=1.0.0,<2.0.0'),
('~1', '>=1,<2'),
('~1.0', '>=1.0,<1.1'),
('~1.0.0', '>=1.0.0,<1.1.0'),
]
)
def test_constraints_keep_version_precision(input, expected):
assert str(parse_constraint(input)) == expected
...@@ -27,7 +27,7 @@ def test_poetry(): ...@@ -27,7 +27,7 @@ def test_poetry():
assert package.keywords == ["packaging", "dependency", "poetry"] assert package.keywords == ["packaging", "dependency", "poetry"]
assert package.python_versions == '~2.7 || ^3.6' assert package.python_versions == '~2.7 || ^3.6'
assert str(package.python_constraint) == '>=2.7,<2.8.0 || >=3.6,<4.0.0' assert str(package.python_constraint) == '>=2.7,<2.8 || >=3.6,<4.0'
dependencies = {} dependencies = {}
for dep in package.requires: for dep in package.requires:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment