Skip to content

Commit

Permalink
Merge pull request #1545 from robinjhuang/custom-deprecation-warnings
Browse files Browse the repository at this point in the history
Custom deprecation warning classes.
  • Loading branch information
pganssle committed Oct 29, 2018
2 parents 29f9cb0 + 3a9dc2b commit 566f3aa
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 13 deletions.
1 change: 1 addition & 0 deletions changelog.d/1545.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed the warning class of all deprecation warnings; deprecation warning classes are no longer derived from ``DeprecationWarning`` and are thus visible by default.
13 changes: 12 additions & 1 deletion pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ def get_supported_platform():
'register_finder', 'register_namespace_handler', 'register_loader_type',
'fixup_namespace_packages', 'get_importer',

# Warnings
'PkgResourcesDeprecationWarning',

# Deprecated/backward compatibility only
'run_main', 'AvailableDistributions',
]
Expand Down Expand Up @@ -2335,7 +2338,7 @@ def load(self, require=True, *args, **kwargs):
warnings.warn(
"Parameters to load are deprecated. Call .resolve and "
".require separately.",
DeprecationWarning,
PkgResourcesDeprecationWarning,
stacklevel=2,
)
if require:
Expand Down Expand Up @@ -3158,3 +3161,11 @@ def _initialize_master_working_set():
# match order
list(map(working_set.add_entry, sys.path))
globals().update(locals())

class PkgResourcesDeprecationWarning(Warning):
"""
Base class for warning about deprecations in ``pkg_resources``
This class is not derived from ``DeprecationWarning``, and as such is
visible by default.
"""
11 changes: 10 additions & 1 deletion pkg_resources/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pkg_resources import (
parse_requirements, VersionConflict, parse_version,
Distribution, EntryPoint, Requirement, safe_version, safe_name,
WorkingSet)
WorkingSet, PkgResourcesDeprecationWarning)


# from Python 3.6 docs.
Expand Down Expand Up @@ -492,6 +492,15 @@ def testParseMap(self):
with pytest.raises(ValueError):
EntryPoint.parse_map(self.submap_str)

def testDeprecationWarnings(self):
ep = EntryPoint(
"foo", "pkg_resources.tests.test_resources", ["TestEntryPoints"],
["x"]
)
with pytest.warns(pkg_resources.PkgResourcesDeprecationWarning):
ep.load(require=False)



class TestRequirements:
def testBasics(self):
Expand Down
4 changes: 4 additions & 0 deletions setuptools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from distutils.util import convert_path
from fnmatch import fnmatchcase

from ._deprecation_warning import SetuptoolsDeprecationWarning

from setuptools.extern.six import PY3
from setuptools.extern.six.moves import filter, map

Expand All @@ -22,6 +24,7 @@

__all__ = [
'setup', 'Distribution', 'Feature', 'Command', 'Extension', 'Require',
'SetuptoolsDeprecationWarning',
'find_packages'
]

Expand Down Expand Up @@ -188,4 +191,5 @@ def findall(dir=os.curdir):
return list(files)


# Apply monkey patches
monkey.patch_all()
7 changes: 7 additions & 0 deletions setuptools/_deprecation_warning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class SetuptoolsDeprecationWarning(Warning):
"""
Base class for warning deprecations in ``setuptools``
This class is not derived from ``DeprecationWarning``, and as such is
visible by default.
"""
15 changes: 11 additions & 4 deletions setuptools/command/easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@
import shlex
import io


from sysconfig import get_config_vars, get_path

from setuptools import SetuptoolsDeprecationWarning

from setuptools.extern import six
from setuptools.extern.six.moves import configparser, map

Expand Down Expand Up @@ -2077,15 +2080,15 @@ class ScriptWriter:
@classmethod
def get_script_args(cls, dist, executable=None, wininst=False):
# for backward compatibility
warnings.warn("Use get_args", DeprecationWarning)
warnings.warn("Use get_args", EasyInstallDeprecationWarning)
writer = (WindowsScriptWriter if wininst else ScriptWriter).best()
header = cls.get_script_header("", executable, wininst)
return writer.get_args(dist, header)

@classmethod
def get_script_header(cls, script_text, executable=None, wininst=False):
# for backward compatibility
warnings.warn("Use get_header", DeprecationWarning, stacklevel=2)
warnings.warn("Use get_header", EasyInstallDeprecationWarning, stacklevel=2)
if wininst:
executable = "python.exe"
return cls.get_header(script_text, executable)
Expand Down Expand Up @@ -2120,7 +2123,7 @@ def _ensure_safe_name(name):
@classmethod
def get_writer(cls, force_windows):
# for backward compatibility
warnings.warn("Use best", DeprecationWarning)
warnings.warn("Use best", EasyInstallDeprecationWarning)
return WindowsScriptWriter.best() if force_windows else cls.best()

@classmethod
Expand Down Expand Up @@ -2152,7 +2155,7 @@ class WindowsScriptWriter(ScriptWriter):
@classmethod
def get_writer(cls):
# for backward compatibility
warnings.warn("Use best", DeprecationWarning)
warnings.warn("Use best", EasyInstallDeprecationWarning)
return cls.best()

@classmethod
Expand Down Expand Up @@ -2333,3 +2336,7 @@ def gen_usage(script_name):
yield
finally:
distutils.core.gen_usage = saved

class EasyInstallDeprecationWarning(SetuptoolsDeprecationWarning):
"""Class for warning about deprecations in EasyInstall in SetupTools. Not ignored by default, unlike DeprecationWarning."""

8 changes: 6 additions & 2 deletions setuptools/command/egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from setuptools.glob import glob

from setuptools.extern import packaging

from setuptools import SetuptoolsDeprecationWarning

def translate_pattern(glob):
"""
Expand Down Expand Up @@ -696,11 +696,15 @@ def get_pkg_info_revision():
Get a -r### off of PKG-INFO Version in case this is an sdist of
a subversion revision.
"""
warnings.warn("get_pkg_info_revision is deprecated.", DeprecationWarning)
warnings.warn("get_pkg_info_revision is deprecated.", EggInfoDeprecationWarning)
if os.path.exists('PKG-INFO'):
with io.open('PKG-INFO') as f:
for line in f:
match = re.match(r"Version:.*-r(\d+)\s*$", line)
if match:
return int(match.group(1))
return 0


class EggInfoDeprecationWarning(SetuptoolsDeprecationWarning):
"""Class for warning about deprecations in eggInfo in setupTools. Not ignored by default, unlike DeprecationWarning."""
10 changes: 8 additions & 2 deletions setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from setuptools.extern import packaging
from setuptools.extern.six.moves import map, filter, filterfalse

from . import SetuptoolsDeprecationWarning

from setuptools.depends import Require
from setuptools import windows_support
from setuptools.monkey import get_unpatched
Expand All @@ -33,7 +35,7 @@


def _get_unpatched(cls):
warnings.warn("Do not call this function", DeprecationWarning)
warnings.warn("Do not call this function", DistDeprecationWarning)
return get_unpatched(cls)


Expand Down Expand Up @@ -980,7 +982,7 @@ def warn_deprecated():
"Features are deprecated and will be removed in a future "
"version. See https://github.com/pypa/setuptools/issues/65."
)
warnings.warn(msg, DeprecationWarning, stacklevel=3)
warnings.warn(msg, DistDeprecationWarning, stacklevel=3)

def __init__(
self, description, standard=False, available=True,
Expand Down Expand Up @@ -1069,3 +1071,7 @@ def validate(self, dist):
" doesn't contain any packages or modules under %s"
% (self.description, item, item)
)


class DistDeprecationWarning(SetuptoolsDeprecationWarning):
"""Class for warning about deprecations in dist in setuptools. Not ignored by default, unlike DeprecationWarning."""
5 changes: 4 additions & 1 deletion setuptools/tests/test_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import unicode_literals

import io

from setuptools.dist import DistDeprecationWarning, _get_unpatched
from setuptools import Distribution
from setuptools.extern.six.moves.urllib.request import pathname2url
from setuptools.extern.six.moves.urllib_parse import urljoin
Expand Down Expand Up @@ -56,6 +56,9 @@ def sdist_with_index(distname, version):
assert [dist.key for dist in resolved_dists if dist] == reqs


def test_dist__get_unpatched_deprecated():
pytest.warns(DistDeprecationWarning, _get_unpatched, [""])

def __maintainer_test_cases():
attrs = {"name": "package",
"version": "1.0",
Expand Down
18 changes: 17 additions & 1 deletion setuptools/tests/test_easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import io
import zipfile
import mock

from setuptools.command.easy_install import EasyInstallDeprecationWarning, ScriptWriter, WindowsScriptWriter
import time
from setuptools.extern import six
from setuptools.extern.six.moves import urllib
Expand Down Expand Up @@ -288,6 +288,22 @@ def test_script_install(self, sdist_script, tmpdir, monkeypatch):
assert (target / 'mypkg_script').exists()


def test_dist_get_script_args_deprecated(self):
with pytest.warns(EasyInstallDeprecationWarning):
ScriptWriter.get_script_args(None, None)

def test_dist_get_script_header_deprecated(self):
with pytest.warns(EasyInstallDeprecationWarning):
ScriptWriter.get_script_header("")

def test_dist_get_writer_deprecated(self):
with pytest.warns(EasyInstallDeprecationWarning):
ScriptWriter.get_writer(None)

def test_dist_WindowsScriptWriter_get_writer_deprecated(self):
with pytest.warns(EasyInstallDeprecationWarning):
WindowsScriptWriter.get_writer()

@pytest.mark.filterwarnings('ignore:Unbuilt egg')
class TestPTHFileWriter:
def test_add_from_cwd_site_sets_dirty(self):
Expand Down
5 changes: 4 additions & 1 deletion setuptools/tests/test_egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import stat
import time

from setuptools.command.egg_info import egg_info, manifest_maker
from setuptools.command.egg_info import egg_info, manifest_maker, EggInfoDeprecationWarning, get_pkg_info_revision
from setuptools.dist import Distribution
from setuptools.extern.six.moves import map

Expand Down Expand Up @@ -603,3 +603,6 @@ def test_egg_info_tag_only_once(self, tmpdir_cwd, env):
with open(os.path.join(egg_info_dir, 'PKG-INFO')) as pkginfo_file:
pkg_info_lines = pkginfo_file.read().split('\n')
assert 'Version: 0.0.0.dev0' in pkg_info_lines

def test_get_pkg_info_revision_deprecated(self):
pytest.warns(EggInfoDeprecationWarning, get_pkg_info_revision)

0 comments on commit 566f3aa

Please sign in to comment.