Skip to content

Commit

Permalink
Remove SetuptoolsVersion and SetuptoolsLegacyVersion. Ref #296.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Mar 17, 2018
1 parent d97e55a commit eeeb9b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 109 deletions.
10 changes: 10 additions & 0 deletions CHANGES.rst
@@ -1,3 +1,13 @@
v39.0.0
-------

* #296: Removed long-deprecated support for iteration on
Version objects as returned by ``pkg_resources.parse_version``.
Removed the ``SetuptoolsVersion`` and
``SetuptoolsLegacyVersion`` names as well. They should not
have been used, but if they were, replace with
``Version`` and ``LegacyVersion`` from ``packaging.version``.

v38.7.0
-------

Expand Down
111 changes: 2 additions & 109 deletions pkg_resources/__init__.py
Expand Up @@ -114,118 +114,11 @@ class PEP440Warning(RuntimeWarning):
"""


class _SetuptoolsVersionMixin(object):
def __hash__(self):
return super(_SetuptoolsVersionMixin, self).__hash__()

def __lt__(self, other):
if isinstance(other, tuple):
return tuple(self) < other
else:
return super(_SetuptoolsVersionMixin, self).__lt__(other)

def __le__(self, other):
if isinstance(other, tuple):
return tuple(self) <= other
else:
return super(_SetuptoolsVersionMixin, self).__le__(other)

def __eq__(self, other):
if isinstance(other, tuple):
return tuple(self) == other
else:
return super(_SetuptoolsVersionMixin, self).__eq__(other)

def __ge__(self, other):
if isinstance(other, tuple):
return tuple(self) >= other
else:
return super(_SetuptoolsVersionMixin, self).__ge__(other)

def __gt__(self, other):
if isinstance(other, tuple):
return tuple(self) > other
else:
return super(_SetuptoolsVersionMixin, self).__gt__(other)

def __ne__(self, other):
if isinstance(other, tuple):
return tuple(self) != other
else:
return super(_SetuptoolsVersionMixin, self).__ne__(other)

def __getitem__(self, key):
return tuple(self)[key]

def __iter__(self):
component_re = re.compile(r'(\d+ | [a-z]+ | \.| -)', re.VERBOSE)
replace = {
'pre': 'c',
'preview': 'c',
'-': 'final-',
'rc': 'c',
'dev': '@',
}.get

def _parse_version_parts(s):
for part in component_re.split(s):
part = replace(part, part)
if not part or part == '.':
continue
if part[:1] in '0123456789':
# pad for numeric comparison
yield part.zfill(8)
else:
yield '*' + part

# ensure that alpha/beta/candidate are before final
yield '*final'

def old_parse_version(s):
parts = []
for part in _parse_version_parts(s.lower()):
if part.startswith('*'):
# remove '-' before a prerelease tag
if part < '*final':
while parts and parts[-1] == '*final-':
parts.pop()
# remove trailing zeros from each series of numeric parts
while parts and parts[-1] == '00000000':
parts.pop()
parts.append(part)
return tuple(parts)

# Warn for use of this function
warnings.warn(
"You have iterated over the result of "
"pkg_resources.parse_version. This is a legacy behavior which is "
"inconsistent with the new version class introduced in setuptools "
"8.0. In most cases, conversion to a tuple is unnecessary. For "
"comparison of versions, sort the Version instances directly. If "
"you have another use case requiring the tuple, please file a "
"bug with the setuptools project describing that need.",
RuntimeWarning,
stacklevel=1,
)

for part in old_parse_version(str(self)):
yield part


class SetuptoolsVersion(_SetuptoolsVersionMixin, packaging.version.Version):
pass


class SetuptoolsLegacyVersion(_SetuptoolsVersionMixin,
packaging.version.LegacyVersion):
pass


def parse_version(v):
try:
return SetuptoolsVersion(v)
return packaging.version.Version(v)
except packaging.version.InvalidVersion:
return SetuptoolsLegacyVersion(v)
return packaging.version.LegacyVersion(v)


_state_vars = {}
Expand Down

2 comments on commit eeeb9b2

@ida
Copy link

@ida ida commented on eeeb9b2 Apr 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaraco This change caused plone/plone.recipe.zope2instance#37, any hint on how comparing versions in another way than comparing integers of the tuple, is appreciated.

@pganssle
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ida I think you're supposed to actually call parse_version on them and compare the results.

Please sign in to comment.