Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca07df4

Browse files
authored
Merge pull request #11395 from anntzer/deprecations
Various fixes to deprecated and warn_deprecated.
2 parents 3a5d8c2 + 07ede14 commit ca07df4

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

doc/api/next_api_changes/2018-02-15-AL-deprecations.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ The following rcParams are deprecated:
4242
- ``pgf.debug`` (the pgf backend relies on logging),
4343

4444
The following keyword arguments are deprecated:
45-
- passing ``verts`` to ``scatter`` (use ``marker`` instead),
45+
- passing ``verts`` to ``Axes.scatter`` (use ``marker`` instead),
46+
- passing ``obj_type`` to ``cbook.deprecated``,

lib/matplotlib/cbook/deprecation.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class MatplotlibDeprecationWarning(UserWarning):
1414
1515
https://docs.python.org/dev/whatsnew/2.7.html#the-future-for-python-2-x
1616
"""
17-
pass
1817

1918

2019
mplDeprecation = MatplotlibDeprecationWarning
@@ -28,29 +27,34 @@ def _generate_deprecation_message(
2827
removal = {"2.2": "in 3.1", "3.0": "in 3.2"}.get(
2928
since, "two minor releases later")
3029
elif removal:
30+
if pending:
31+
raise ValueError(
32+
"A pending deprecation cannot have a scheduled removal")
3133
removal = "in {}".format(removal)
3234

3335
if not message:
3436
message = (
35-
"The {name} {obj_type}"
37+
"The %(name)s %(obj_type)s"
3638
+ (" will be deprecated in a future version"
3739
if pending else
38-
(" was deprecated in Matplotlib {since}"
39-
+ (" and will be removed {removal}"
40+
(" was deprecated in Matplotlib %(since)s"
41+
+ (" and will be removed %(removal)s"
4042
if removal else
4143
"")))
4244
+ "."
43-
+ (" Use {alternative} instead." if alternative else ""))
45+
+ (" Use %(alternative)s instead." if alternative else ""))
4446

45-
return message.format(func=name, name=name, obj_type=obj_type, since=since,
46-
removal=removal, alternative=alternative)
47+
return (
48+
message % dict(func=name, name=name, obj_type=obj_type, since=since,
49+
removal=removal, alternative=alternative)
50+
+ addendum)
4751

4852

4953
def warn_deprecated(
5054
since, message='', name='', alternative='', pending=False,
5155
obj_type='attribute', addendum='', *, removal=''):
5256
"""
53-
Used to display deprecation warning in a standard way.
57+
Used to display deprecation in a standard way.
5458
5559
Parameters
5660
----------
@@ -69,18 +73,19 @@ def warn_deprecated(
6973
The name of the deprecated object.
7074
7175
alternative : str, optional
72-
An alternative function that the user may use in place of the
73-
deprecated function. The deprecation warning will tell the user
74-
about this alternative if provided.
76+
An alternative API that the user may use in place of the deprecated
77+
API. The deprecation warning will tell the user about this alternative
78+
if provided.
7579
7680
pending : bool, optional
7781
If True, uses a PendingDeprecationWarning instead of a
78-
DeprecationWarning.
82+
DeprecationWarning. Cannot be used together with *removal*.
7983
8084
removal : str, optional
8185
The expected removal version. With the default (an empty string), a
8286
removal version is automatically computed from *since*. Set to other
83-
Falsy values to not schedule a removal date.
87+
Falsy values to not schedule a removal date. Cannot be used together
88+
with *pending*.
8489
8590
obj_type : str, optional
8691
The object type being deprecated.
@@ -101,7 +106,9 @@ def warn_deprecated(
101106
message = _generate_deprecation_message(
102107
since, message, name, alternative, pending, obj_type, removal=removal)
103108
message = '\n' + message
104-
warnings.warn(message, mplDeprecation, stacklevel=2)
109+
category = (PendingDeprecationWarning if pending
110+
else MatplotlibDeprecationWarning)
111+
warnings.warn(message, category, stacklevel=2)
105112

106113

107114
def deprecated(since, message='', name='', alternative='', pending=False,
@@ -120,8 +127,7 @@ def deprecated(since, message='', name='', alternative='', pending=False,
120127
specifier `%(name)s` may be used for the name of the object,
121128
and `%(alternative)s` may be used in the deprecation message
122129
to insert the name of an alternative to the deprecated
123-
object. `%(obj_type)s` may be used to insert a friendly name
124-
for the type of object being deprecated.
130+
object.
125131
126132
name : str, optional
127133
The name of the deprecated object; if not provided the name
@@ -135,18 +141,19 @@ def new_function():
135141
oldFunction = new_function
136142
137143
alternative : str, optional
138-
An alternative object that the user may use in place of the
139-
deprecated object. The deprecation warning will tell the user
140-
about this alternative if provided.
144+
An alternative API that the user may use in place of the deprecated
145+
API. The deprecation warning will tell the user about this alternative
146+
if provided.
141147
142148
pending : bool, optional
143149
If True, uses a PendingDeprecationWarning instead of a
144-
DeprecationWarning.
150+
DeprecationWarning. Cannot be used together with *removal*.
145151
146152
removal : str, optional
147153
The expected removal version. With the default (an empty string), a
148154
removal version is automatically computed from *since*. Set to other
149-
Falsy values to not schedule a removal date.
155+
Falsy values to not schedule a removal date. Cannot be used together
156+
with *pending*.
150157
151158
addendum : str, optional
152159
Additional text appended directly to the final message.
@@ -159,9 +166,14 @@ def new_function():
159166
@deprecated('1.4.0')
160167
def the_function_to_deprecate():
161168
pass
162-
163169
"""
164170

171+
if obj_type is not None:
172+
warn_deprecated(
173+
"3.0", "Passing 'obj_type' to the 'deprecated' decorator has no "
174+
"effect, and is deprecated since Matplotlib %(since)s; support "
175+
"for it will be removed %(removal)s.")
176+
165177
def deprecate(obj, message=message, name=name, alternative=alternative,
166178
pending=pending, addendum=addendum):
167179

@@ -174,12 +186,7 @@ def deprecate(obj, message=message, name=name, alternative=alternative,
174186
func = obj.__init__
175187

176188
def finalize(wrapper, new_doc):
177-
try:
178-
obj.__doc__ = new_doc
179-
except (AttributeError, TypeError):
180-
# cls.__doc__ is not writeable on Py2.
181-
# TypeError occurs on PyPy
182-
pass
189+
obj.__doc__ = new_doc
183190
obj.__init__ = wrapper
184191
return obj
185192
else:
@@ -204,9 +211,11 @@ def finalize(wrapper, new_doc):
204211
message = _generate_deprecation_message(
205212
since, message, name, alternative, pending, obj_type, addendum,
206213
removal=removal)
214+
category = (PendingDeprecationWarning if pending
215+
else MatplotlibDeprecationWarning)
207216

208217
def wrapper(*args, **kwargs):
209-
warnings.warn(message, mplDeprecation, stacklevel=2)
218+
warnings.warn(message, category, stacklevel=2)
210219
return func(*args, **kwargs)
211220

212221
old_doc = textwrap.dedent(old_doc or '').strip('\n')

0 commit comments

Comments
 (0)