Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8e57e5d

Browse filesBrowse files
authored
Merge pull request #13536 from anntzer/unit-subclasses
Make unit converters also handle instances of subclasses.
2 parents 08fd9f2 + 9086388 commit 8e57e5d
Copy full SHA for 8e57e5d

File tree

Expand file treeCollapse file tree

3 files changed

+30
-15
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+30
-15
lines changed
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Unit converters now handle instances of subclasses
2+
``````````````````````````````````````````````````
3+
4+
Unit converters now also handle instances of subclasses of the class they have
5+
been registered for.

‎lib/matplotlib/tests/test_units.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_units.py
+12-4Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
from datetime import datetime
2+
import platform
13
from unittest.mock import MagicMock
24

35
import matplotlib.pyplot as plt
4-
from matplotlib.testing.decorators import image_comparison
6+
from matplotlib.testing.decorators import check_figures_equal, image_comparison
57
import matplotlib.units as munits
68
import numpy as np
7-
import platform
89
import pytest
910

1011

@@ -119,7 +120,6 @@ def test_empty_set_limits_with_units(quantity_converter):
119120
@image_comparison(['jpl_bar_units.png'],
120121
savefig_kwarg={'dpi': 120}, style='mpl20')
121122
def test_jpl_bar_units():
122-
from datetime import datetime
123123
import matplotlib.testing.jpl_units as units
124124
units.register()
125125

@@ -136,7 +136,6 @@ def test_jpl_bar_units():
136136
@image_comparison(['jpl_barh_units.png'],
137137
savefig_kwarg={'dpi': 120}, style='mpl20')
138138
def test_jpl_barh_units():
139-
from datetime import datetime
140139
import matplotlib.testing.jpl_units as units
141140
units.register()
142141

@@ -164,3 +163,12 @@ def test_scatter_element0_masked():
164163
fig, ax = plt.subplots()
165164
ax.scatter(times, y)
166165
fig.canvas.draw()
166+
167+
168+
@check_figures_equal(extensions=["png"])
169+
def test_subclass(fig_test, fig_ref):
170+
class subdate(datetime):
171+
pass
172+
173+
fig_test.subplots().plot(subdate(2000, 1, 1), 0, "o")
174+
fig_ref.subplots().plot(datetime(2000, 1, 1), 0, "o")

‎lib/matplotlib/units.py

Copy file name to clipboardExpand all lines: lib/matplotlib/units.py
+13-11Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,20 @@ def get_converter(self, x):
205205
# If there are no elements in x, infer the units from its dtype
206206
if not x.size:
207207
return self.get_converter(np.array([0], dtype=x.dtype))
208-
try: # Look up in the cache.
209-
return self[type(x)]
210-
except KeyError:
211-
try: # If cache lookup fails, look up based on first element...
212-
first = cbook.safe_first_element(x)
213-
except (TypeError, StopIteration):
208+
for cls in type(x).__mro__: # Look up in the cache.
209+
try:
210+
return self[cls]
211+
except KeyError:
214212
pass
215-
else:
216-
# ... and avoid infinite recursion for pathological iterables
217-
# where indexing returns instances of the same iterable class.
218-
if type(first) is not type(x):
219-
return self.get_converter(first)
213+
try: # If cache lookup fails, look up based on first element...
214+
first = cbook.safe_first_element(x)
215+
except (TypeError, StopIteration):
216+
pass
217+
else:
218+
# ... and avoid infinite recursion for pathological iterables for
219+
# which indexing returns instances of the same iterable class.
220+
if type(first) is not type(x):
221+
return self.get_converter(first)
220222
return None
221223

222224

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.