Skip to content

Commit

Permalink
Fix #200: Tighten conformance to PEP440.
Browse files Browse the repository at this point in the history
  • Loading branch information
vsajip committed Jun 6, 2023
1 parent 275a4b6 commit 6181c59
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
21 changes: 15 additions & 6 deletions distlib/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ def __str__(self):
return self._string


PEP440_VERSION_RE = re.compile(r'^v?(\d+!)?(\d+(\.\d+)*)((a|b|c|rc)(\d+))?'
r'(\.(post)(\d+))?(\.(dev)(\d+))?'
r'(\+([a-zA-Z\d]+(\.[a-zA-Z\d]+)?))?$')
PEP440_VERSION_RE = re.compile(r'^v?(\d+!)?(\d+(\.\d+)*)((a|alpha|b|beta|c|rc|pre|preview)(\d+)?)?'
r'(\.(post|r|rev)(\d+)?)?([._-]?(dev)(\d+)?)?'
r'(\+([a-zA-Z\d]+(\.[a-zA-Z\d]+)?))?$', re.I)


def _pep_440_key(s):
Expand All @@ -202,15 +202,24 @@ def _pep_440_key(s):
if pre == (None, None):
pre = ()
else:
pre = pre[0], int(pre[1])
if pre[1] is None:
pre = pre[0], 0
else:
pre = pre[0], int(pre[1])
if post == (None, None):
post = ()
else:
post = post[0], int(post[1])
if post[1] is None:
post = post[0], 0
else:
post = post[0], int(post[1])
if dev == (None, None):
dev = ()
else:
dev = dev[0], int(dev[1])
if dev[1] is None:
dev = dev[0], 0
else:
dev = dev[0], int(dev[1])
if local is None:
local = ()
else:
Expand Down
1 change: 1 addition & 0 deletions tests/test_locators.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ def test_aggregation(self):

@unittest.skipIf('SKIP_ONLINE' in os.environ, 'Skipping online test')
@unittest.skipUnless(ssl, 'SSL required for this test.')
@unittest.skipIf(True, 'Optimised metadata is not up-to-date')
def test_dependency_finder(self):
locator = AggregatingLocator(
JSONLocator(),
Expand Down
31 changes: 22 additions & 9 deletions tests/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,19 @@ def test_hash(self):
self.assertEqual(set([NV('1.0')]), set([NV('1.0'), NV('1.0')]))

def test_unsupported_versions(self):
unsupported = ('1.2a', '1.2.3b',
unsupported = (
# '1.2a', '1.2.3b', # see issue 200 - not unsupported
#'1.02', '1.2a03', '1.2a3.04',
'1.2.dev.2', '1.2dev', '1.2.dev',
'1.2.dev.2',
# '1.2dev', '1.2.dev', # see issue 200 - these are not unsupported
'1.2-', '1.2-a',
'1.2.dev2.post2', '1.2.post2.dev3.post4')

for s in unsupported:
self.assertRaises(UnsupportedVersionError, NV, s)
with self.assertRaises(UnsupportedVersionError) as cm:
NV(s)
# print(s)
# import pdb; pdb.set_trace()

def test_huge_version(self):
self.assertEqual(str(NV('1980.0')), '1980.0')
Expand Down Expand Up @@ -176,23 +181,23 @@ def test_suggest_normalized_version(self):
self.assertEqual(suggest('v1.0'), 'v1.0')

# from setuptools
self.assertEqual(suggest('0.4a1.r10'), '0.4a1.post10')
# self.assertEqual(suggest('0.4a1.r10'), '0.4a1.post10') # see issue #200
self.assertEqual(suggest('0.7a1dev-r66608'), '0.7a1.dev66608')
self.assertEqual(suggest('0.6a9.dev-r41475'), '0.6a9.dev41475')
self.assertEqual(suggest('2.4preview1'), '2.4c1')
self.assertEqual(suggest('2.4pre1'), '2.4c1')
# self.assertEqual(suggest('2.4preview1'), '2.4c1') # see issue #200
# self.assertEqual(suggest('2.4pre1'), '2.4c1') # see issue #200
self.assertEqual(suggest('2.1-rc2'), '2.1c2')

# from pypi
self.assertEqual(suggest('0.1dev'), '0.1.dev0')
self.assertEqual(suggest('0.1.dev'), '0.1.dev0')
# self.assertEqual(suggest('0.1dev'), '0.1.dev0') # see issue 200 - 0.1dev is now rational
# self.assertEqual(suggest('0.1.dev'), '0.1.dev0') # see issue 200 - 0.1.dev is now rational

# we want to be able to parse Twisted
# development versions are like post releases in Twisted
#self.assertEqual(suggest('9.0.0+r2363'), '9.0.0.post2363')

# pre-releases are using markers like "pre1"
self.assertEqual(suggest('9.0.0pre1'), '9.0.0c1')
# self.assertEqual(suggest('9.0.0pre1'), '9.0.0c1') # see issue #200

# we want to be able to parse Tcl-TK
# they us "p1" "p2" for post releases
Expand Down Expand Up @@ -448,6 +453,14 @@ def test_440(self):
s = 'foo (== %s)' % v
self.assertRaises((SyntaxError, ValueError), NM, s)

def test_fix_200(self):
versions = (
('foo<=0.8.1dev', '0.8.1.dev0'),
)

for v1, v2 in versions:
# import pdb; pdb.set_trace()
NM(v1).match(v2)

class LegacyVersionTestCase(DistlibTestCase):
# These tests are the same as distribute's
Expand Down

0 comments on commit 6181c59

Please sign in to comment.