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

Skip to content

Commit b4db196

Browse files
committed
Don't silence TypeErrors in fmt_{x,y}data.
Still in the "fewer ways to shoot oneself in the foot" spirit.
1 parent 6c0aa84 commit b4db196

3 files changed

Lines changed: 44 additions & 37 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
``Axes.fmt_xdata`` and ``Axes.fmt_ydata`` no longer ignore TypeErrors raised by a user-provided formatter
2+
`````````````````````````````````````````````````````````````````````````````````````````````````````````
3+
4+
Previously, if the user provided a ``fmt_xdata`` or ``fmt_ydata`` function that
5+
raised a TypeError (or set them to a non-callable), the exception would be
6+
silently ignored and the default formatter be used instead. This is no longer
7+
the case; the exception is now propagated out.

lib/matplotlib/axes/_base.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,32 +3700,26 @@ def yaxis_date(self, tz=None):
37003700

37013701
def format_xdata(self, x):
37023702
"""
3703-
Return *x* string formatted. This function will use the attribute
3704-
self.fmt_xdata if it is callable, else will fall back on the xaxis
3705-
major formatter
3703+
Return *x* formatted as an x-value.
3704+
3705+
This function will use the `.fmt_xdata` attribute if it is not None,
3706+
else will fall back on the xaxis major formatter.
37063707
"""
3707-
try:
3708-
return self.fmt_xdata(x)
3709-
except TypeError:
3710-
func = self.xaxis.get_major_formatter().format_data_short
3711-
val = func(x)
3712-
return val
3708+
return (self.fmt_xdata if self.fmt_xdata is not None
3709+
else self.xaxis.get_major_formatter().format_data_short)(x)
37133710

37143711
def format_ydata(self, y):
37153712
"""
3716-
Return y string formatted. This function will use the
3717-
:attr:`fmt_ydata` attribute if it is callable, else will fall
3718-
back on the yaxis major formatter
3713+
Return *y* formatted as an y-value.
3714+
3715+
This function will use the `.fmt_ydata` attribute if it is not None,
3716+
else will fall back on the yaxis major formatter.
37193717
"""
3720-
try:
3721-
return self.fmt_ydata(y)
3722-
except TypeError:
3723-
func = self.yaxis.get_major_formatter().format_data_short
3724-
val = func(y)
3725-
return val
3718+
return (self.fmt_ydata if self.fmt_ydata is not None
3719+
else self.yaxis.get_major_formatter().format_data_short)(y)
37263720

37273721
def format_coord(self, x, y):
3728-
"""Return a format string formatting the *x*, *y* coord"""
3722+
"""Return a format string formatting the *x*, *y* coordinates."""
37293723
if x is None:
37303724
xs = '???'
37313725
else:

lib/matplotlib/tests/test_backend_bases.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
from matplotlib.backend_bases import (
24
FigureCanvasBase, LocationEvent, RendererBase)
35
import matplotlib.pyplot as plt
@@ -71,22 +73,26 @@ def test_non_gui_warning():
7173
in str(rec[0].message))
7274

7375

74-
def test_location_event_position():
75-
# LocationEvent should cast its x and y arguments
76-
# to int unless it is None
77-
fig = plt.figure()
76+
@pytest.mark.parametrize(
77+
"x, y", [(42, 24), (None, 42), (None, None), (200, 100.01), (205.75, 2.0)])
78+
def test_location_event_position(x, y):
79+
# LocationEvent should cast its x and y arguments to int unless it is None.
80+
fig, ax = plt.subplots()
7881
canvas = FigureCanvasBase(fig)
79-
test_positions = [(42, 24), (None, 42), (None, None),
80-
(200, 100.01), (205.75, 2.0)]
81-
for x, y in test_positions:
82-
event = LocationEvent("test_event", canvas, x, y)
83-
if x is None:
84-
assert event.x is None
85-
else:
86-
assert event.x == int(x)
87-
assert isinstance(event.x, int)
88-
if y is None:
89-
assert event.y is None
90-
else:
91-
assert event.y == int(y)
92-
assert isinstance(event.y, int)
82+
event = LocationEvent("test_event", canvas, x, y)
83+
if x is None:
84+
assert event.x is None
85+
else:
86+
assert event.x == int(x)
87+
assert isinstance(event.x, int)
88+
if y is None:
89+
assert event.y is None
90+
else:
91+
assert event.y == int(y)
92+
assert isinstance(event.y, int)
93+
if x is not None and y is not None:
94+
assert re.match(
95+
"x={} +y={}".format(ax.format_xdata(x), ax.format_ydata(y)),
96+
ax.format_coord(x, y))
97+
ax.fmt_xdata = ax.fmt_ydata = lambda x: "foo"
98+
assert re.match("x=foo +y=foo", ax.format_coord(x, y))

0 commit comments

Comments
 (0)