Skip to content

Commit

Permalink
Merge pull request #75 from pypa/debt/deprecate-version
Browse files Browse the repository at this point in the history
Deprecate version classes
  • Loading branch information
jaraco committed Dec 12, 2021
2 parents 45a280c + d8c1623 commit 92082ee
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
8 changes: 5 additions & 3 deletions distutils/cygwinccompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from subprocess import Popen, PIPE, check_output
import re

import distutils.version
from distutils.unixccompiler import UnixCCompiler
from distutils.file_util import write_file
from distutils.errors import (DistutilsExecError, CCompilerError,
Expand Down Expand Up @@ -405,9 +406,10 @@ def _find_exe_version(cmd):
result = RE_VERSION.search(out_string)
if result is None:
return None
# LooseVersion works with strings
# so we need to decode our bytes
return LooseVersion(result.group(1).decode())
# LooseVersion works with strings; decode
ver_str = result.group(1).decode()
with distutils.version.suppress_known_deprecation():
return LooseVersion(ver_str)

def get_versions():
""" Try to find out the versions of gcc, ld and dllwrap.
Expand Down
8 changes: 8 additions & 0 deletions distutils/tests/test_version.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
"""Tests for distutils.version."""
import unittest
import distutils
from distutils.version import LooseVersion
from distutils.version import StrictVersion
from test.support import run_unittest

class VersionTestCase(unittest.TestCase):

def setUp(self):
self.ctx = distutils.version.suppress_known_deprecation()
self.ctx.__enter__()

def tearDown(self):
self.ctx.__exit__(None, None, None)

def test_prerelease(self):
version = StrictVersion('1.2.3a1')
self.assertEqual(version.version, (1, 2, 3))
Expand Down
23 changes: 22 additions & 1 deletion distutils/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
"""

import re
import warnings
import contextlib


@contextlib.contextmanager
def suppress_known_deprecation():
with warnings.catch_warnings(record=True) as ctx:
warnings.filterwarnings(
action='default',
category=DeprecationWarning,
message="distutils Version classes are deprecated.",
)
yield ctx


class Version:
"""Abstract base class for version numbering classes. Just provides
Expand All @@ -36,6 +50,12 @@ class Version:
"""

def __init__ (self, vstring=None):
warnings.warn(
"distutils Version classes are deprecated. "
"Use packaging.version instead.",
DeprecationWarning,
stacklevel=2,
)
if vstring:
self.parse(vstring)

Expand Down Expand Up @@ -165,7 +185,8 @@ def __str__ (self):

def _cmp (self, other):
if isinstance(other, str):
other = StrictVersion(other)
with suppress_known_deprecation():
other = StrictVersion(other)
elif not isinstance(other, StrictVersion):
return NotImplemented

Expand Down
7 changes: 5 additions & 2 deletions distutils/versionpredicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ def splitUp(pred):
if not res:
raise ValueError("bad package restriction syntax: %r" % pred)
comp, verStr = res.groups()
return (comp, distutils.version.StrictVersion(verStr))
with distutils.version.suppress_known_deprecation():
other = distutils.version.StrictVersion(verStr)
return (comp, other)

compmap = {"<": operator.lt, "<=": operator.le, "==": operator.eq,
">": operator.gt, ">=": operator.ge, "!=": operator.ne}
Expand Down Expand Up @@ -162,5 +164,6 @@ def split_provision(value):
raise ValueError("illegal provides specification: %r" % value)
ver = m.group(2) or None
if ver:
ver = distutils.version.StrictVersion(ver)
with distutils.version.suppress_known_deprecation():
ver = distutils.version.StrictVersion(ver)
return m.group(1), ver

0 comments on commit 92082ee

Please sign in to comment.