8000 Merge pull request #14879 from anntzer/scalevalidation · matplotlib/matplotlib@d511ab8 · GitHub
[go: up one dir, main page]

Skip to content

Commit d511ab8

Browse files
authored
Merge pull request #14879 from anntzer/scalevalidation
Error out when unsupported kwargs are passed to Scale.
2 parents b2783ed + c9a2ba6 commit d511ab8

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
API changes
2+
```````````
3+
4+
Passing unsupported keyword arguments to `.ScaleBase` and its subclasses
5+
`.LinearScale`, and `.SymLogScale` is deprecated and will raise a `TypeError` in 3.3.
6+
7+
If extra kwargs are pased to `.LogScale`, `TypeError` will now be
8+
raised instead of `ValueError`.

lib/matplotlib/scale.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
NullLocator, LogLocator, AutoLocator, AutoMinorLocator,
2020
SymmetricalLogLocator, LogitLocator)
2121
from matplotlib.transforms import Transform, IdentityTransform
22+
from matplotlib.cbook import warn_deprecated
2223

2324

2425
class ScaleBase:
@@ -52,6 +53,14 @@ def __init__(self, axis, **kwargs):
5253
be used: a single scale object should be usable by multiple
5354
`~matplotlib.axis.Axis`\es at the same time.
5455
"""
56+
if kwargs:
57+
warn_deprecated(
58+
'3.2.0',
59+
message=(
60+
f"ScaleBase got an unexpected keyword "
61+
f"argument {next(iter(kwargs))!r}. "
62+
'In the future this will raise TypeError')
63+
)
5564

5665
def get_transform(self):
5766
"""
@@ -143,8 +152,7 @@ def forward(values: array-like) -> array-like
143152
self._forward = forward
144153
self._inverse = inverse
145154
else:
146-
raise ValueError('arguments to FuncTransform must '
147-
'be functions')
155+
raise ValueError('arguments to FuncTransform must be functions')
148156

149157
def transform_non_affine(self, values):
150158
return self._forward(values)
@@ -382,16 +390,14 @@ def __init__(self, axis, **kwargs):
382390
nonpos = kwargs.pop('nonposy', 'clip')
383391
cbook._check_in_list(['mask', 'clip'], nonposy=nonpos)
384392

385-
if len(kwargs):
386-
raise ValueError(("provided too many kwargs, can only pass "
387-
"{'basex', 'subsx', nonposx'} or "
388-
"{'basey', 'subsy', nonposy'}. You passed ") +
389-
"{!r}".format(kwargs))
393+
if kwargs:
394+
raise TypeError(f"LogScale got an unexpected keyword "
395+
f"argument {next(iter(kwargs))!r}")
390396

391397
if base <= 0 or base == 1:
392398
raise ValueError('The log base cannot be <= 0 or == 1')
393399

394-
self._transform = self.LogTransform(base, nonpos)
400+
self._transform = LogTransform(base, nonpos)
395401
self.subs = subs
396402

397403
@property
@@ -566,6 +572,16 @@ def __init__(self, axis, **kwargs):
566572
linthresh = kwargs.pop('linthreshy', 2.0)
567573
subs = kwargs.pop('subsy', None)
568574
linscale = kwargs.pop('linscaley', 1.0)
575+
if kwargs:
576+
warn_deprecated(
577+
'3.2.0',
578+
message=(
579+
f"SymmetricalLogScale got an unexpected keyword "
580+
f"argument {next(iter(kwargs))!r}. "
581+
'In the future this will raise TypeError')
582+
)
583+
# raise TypeError(f"SymmetricalLogScale got an unexpected keyword "
584+
# f"argument {next(iter(kwargs))!r}")
569585

570586
if base <= 1.0:
571587
raise ValueError("'basex/basey' must be larger than 1")
@@ -574,10 +590,7 @@ def __init__(self, axis, **kwargs):
574590
if linscale <= 0.0:
575591
raise ValueError("'linscalex/linthreshy' must be positive")
576592

577-
self._transform = self.SymmetricalLogTransform(base,
578-
linthresh,
579-
linscale)
580-
593+
self._transform = SymmetricalLogTransform(base, linthresh, linscale)
581594
self.base = base
582595
self.linthresh = linthresh
583596
self.linscale = linscale

lib/matplotlib/tests/test_scale.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,20 @@ def test_logscale_mask():
106106
ax.set(yscale="log")
107107

108108

109-
def test_extra_kwargs_raise():
109+
def test_extra_kwargs_raise_or_warn():
110110
fig, ax = plt.subplots()
111-
with pytest.raises(ValueError):
111+
112+
# with pytest.raises(TypeError):
113+
with pytest.warns(MatplotlibDeprecationWarning):
114+
ax.set_yscale('linear', nonpos='mask')
115+
116+
with pytest.raises(TypeError):
112117
ax.set_yscale('log', nonpos='mask')
113118

119+
# with pytest.raises(TypeError):
120+
with pytest.warns(MatplotlibDeprecationWarning):
121+
ax.set_yscale('symlog', nonpos='mask')
122+
114123

115124
def test_logscale_invert_transform():
116125
fig, ax = plt.subplots()

0 commit comments

Comments
 (0)
0