Skip to content

Proof of concept for deprecation of global non-callables. #10735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
from . import cbook
from matplotlib.cbook import (
mplDeprecation, dedent, get_label, sanitize_sequence)
from matplotlib.cbook.deprecation import _deprecated_global
from matplotlib.rcsetup import defaultParams, validate_backend, cycler

import numpy
Expand Down Expand Up @@ -391,6 +392,11 @@ def ge(self, level):
return self.vald[self.level] >= self.vald[level]


with warnings.catch_warnings():
warnings.simplefilter("ignore")
_deprecated_global("verbose", Verbose(), "2.2")


def _wrap(fmt, func, level='DEBUG', always=True):
"""
return a callable function that wraps func and reports its
Expand Down
37 changes: 37 additions & 0 deletions lib/matplotlib/cbook/deprecation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import functools
import sys
import textwrap
from types import ModuleType
import warnings


Expand Down Expand Up @@ -221,3 +223,38 @@ def wrapper(*args, **kwargs):
return finalize(wrapper, new_doc)

return deprecate


class _DeprecatorModuleType(ModuleType):
def __getattr__(self, name):
try:
val, message = self._deprecated_dict[name]
warnings.warn(message, MatplotlibDeprecationWarning, stacklevel=2)
return val
except KeyError:
raise AttributeError(
"Module {!r} has no attibute {!r}"
.format(self.__name__, name)) from None


def _deprecated_global(
name, obj, since, *,
message="", alternative="", pending=False, obj_type="object",
addendum="", removal=""):
frame = sys._getframe(1)
if frame.f_locals is not frame.f_globals:
raise RuntimeError(
"Matplotlib internal error: cannot globally deprecate object from "
"non-global frame")
mod = sys.modules[frame.f_globals["__name__"]]
if type(mod) == ModuleType:
mod.__class__ = _DeprecatorModuleType
elif mod.__class__ != _DeprecatorModuleType:
warnings.warn("Matplotlib internal error: cannot deprecate global of "
"patched module. Assigning attribute normally.")
mod.__dict__[name] = obj
return
message = _generate_deprecation_message(
since=since, message=message, name=name, alternative=alternative,
pending=pending, obj_type=obj_type, removal=removal)
mod.__dict__.setdefault("_deprecated_dict", {})[name] = (obj, message)