Skip to content

Do not check value of parameters from rcParam (and check rcParam befo… #26167

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2757,6 +2757,7 @@ def bar_label(self, container, labels=None, *, fmt="%g", label_type="edge",
if key in kwargs:
raise ValueError(
f"Passing {key!r} to bar_label() is not supported.")
_api.check_in_list(['edge', 'center'], label_type=label_type)

a, b = self.yaxis.get_view_interval()
y_inverted = a > b
Expand All @@ -2768,8 +2769,6 @@ def bar_label(self, container, labels=None, *, fmt="%g", label_type="edge",
def sign(x):
return 1 if x >= 0 else -1

_api.check_in_list(['edge', 'center'], label_type=label_type)

bars = container.patches
errorbar = container.errorbar
datavalues = container.datavalues
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,8 @@ def set_style(self, style):
"""
if style is None:
style = mpl.rcParams['font.style']
_api.check_in_list(['normal', 'italic', 'oblique'], style=style)
else:
_api.check_in_list(['normal', 'italic', 'oblique'], style=style)
Comment on lines 741 to +744
Copy link
Member

@timhoffm timhoffm Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this kind of change. The code get's longer, harder to follow and more prone to errors.

Double checking the rcParam value here does not hurt. The only disadvantage are 300ns performance loss (which I claim to be negigible in any real-world application). But since the rcParam validation is an independent concept, we don't have a guarantee that they are synchronous. Such differences could be an oversight. But I can also imagine cases where rcParams are used in multiple places and have a slightly broader acceptance range than what a specific function supports.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that those 300 ns are multiplied with a huge number in many situations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is huge? If huge * 300ns > 2% of the total plot time, we may start to consider, otherwise, the benefit is negligible.

self._slant = style

def set_variant(self, variant):
Expand All @@ -753,7 +754,8 @@ def set_variant(self, variant):
"""
if variant is None:
variant = mpl.rcParams['font.variant']
_api.check_in_list(['normal', 'small-caps'], variant=variant)
else:
_api.check_in_list(['normal', 'small-caps'], variant=variant)
self._variant = variant

def set_weight(self, weight):
Expand Down
6 changes: 4 additions & 2 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ def __init__(self, ax,
cm.ScalarMappable.__init__(self, norm, cmap)
if origin is None:
origin = mpl.rcParams['image.origin']
_api.check_in_list(["upper", "lower"], origin=origin)
else:
_api.check_in_list(["upper", "lower"], origin=origin)
self.origin = origin
self.set_filternorm(filternorm)
self.set_filterrad(filterrad)
Expand Down Expand Up @@ -792,7 +793,8 @@ def set_interpolation_stage(self, s):
"""
if s is None:
s = "data" # placeholder for maybe having rcParam
_api.check_in_list(['data', 'rgba'], s=s)
else:
_api.check_in_list(['data', 'rgba'], s=s)
self._interpolation_stage = s
self.stale = True

Expand Down
6 changes: 2 additions & 4 deletions lib/matplotlib/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,6 @@ def __init__(self, xdata, ydata, *,
if solid_joinstyle is None:
solid_joinstyle = mpl.rcParams['lines.solid_joinstyle']

if drawstyle is None:
drawstyle = 'default'

self._dashcapstyle = None
self._dashjoinstyle = None
self._solidjoinstyle = None
Expand Down Expand Up @@ -1090,7 +1087,8 @@ def set_drawstyle(self, drawstyle):
"""
if drawstyle is None:
drawstyle = 'default'
_api.check_in_list(self.drawStyles, drawstyle=drawstyle)
else:
_api.check_in_list(self.drawStyles, drawstyle=drawstyle)
if self._drawstyle != drawstyle:
self.stale = True
# invalidate to trigger a recache of the path
Expand Down
3 changes: 2 additions & 1 deletion lib/matplotlib/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ def _set_fillstyle(self, fillstyle):
"""
if fillstyle is None:
fillstyle = mpl.rcParams['markers.fillstyle']
_api.check_in_list(self.fillstyles, fillstyle=fillstyle)
else:
_api.check_in_list(self.fillstyles, fillstyle=fillstyle)
self._fillstyle = fillstyle
self._recache()

Expand Down
10 changes: 6 additions & 4 deletions lib/matplotlib/mlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,10 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,

if mode is None or mode == 'default':
mode = 'psd'
_api.check_in_list(
['default', 'psd', 'complex', 'magnitude', 'angle', 'phase'],
mode=mode)
else:
_api.check_in_list(
['default', 'psd', 'complex', 'magnitude', 'angle', 'phase'],
mode=mode)

if not same_data and mode != 'psd':
raise ValueError("x and y must be equal if mode is not 'psd'")
Expand All @@ -262,7 +263,8 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
sides = 'twosided'
else:
sides = 'onesided'
_api.check_in_list(['default', 'onesided', 'twosided'], sides=sides)
else:
_api.check_in_list(['default', 'onesided', 'twosided'], sides=sides)

# zero pad x and y up to NFFT if they are shorter than NFFT
if len(x) < NFFT:
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/rcsetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,8 @@ def _convert_validator_spec(key, conv):

## font props
"font.family": validate_stringlist, # used by text object
"font.style": validate_string,
"font.variant": validate_string,
"font.style": ['normal', 'italic', 'oblique'],
"font.variant": ['normal', 'small-caps'],
"font.stretch": validate_fontstretch,
"font.weight": validate_fontweight,
"font.size": validate_float, # Base font size in points
Expand Down