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

Skip to content

Commit 6095dc2

Browse files
committed
Make unit converters also handle instances of subclasses.
1 parent 2208665 commit 6095dc2

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed
Lines 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

Lines 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

@@ -121,7 +122,6 @@ def test_empty_set_limits_with_units(quantity_converter):
121122
@image_comparison(baseline_images=['jpl_bar_units'], extensions=['png'],
122123
savefig_kwarg={'dpi': 120}, style='mpl20')
123124
def test_jpl_bar_units():
124-
from datetime import datetime
125125
import matplotlib.testing.jpl_units as units
126126
units.register()
127127

@@ -138,7 +138,6 @@ def test_jpl_bar_units():
138138
@image_comparison(baseline_images=['jpl_barh_units'], extensions=['png'],
139139
savefig_kwarg={'dpi': 120}, style='mpl20')
140140
def test_jpl_barh_units():
141-
from datetime import datetime
142141
import matplotlib.testing.jpl_units as units
143142
units.register()
144143

@@ -155,3 +154,12 @@ def test_jpl_barh_units():
155154
def test_empty_arrays():
156155
# Check that plotting an empty array with a dtype works
157156
plt.scatter(np.array([], dtype='datetime64[ns]'), np.array([]))
157+
158+
159+
@check_figures_equal(extensions=["png"])
160+
def test_subclass(fig_test, fig_ref):
161+
class subdate(datetime):
162+
pass
163+
164+
fig_test.subplots().plot(subdate(2000, 1, 1), 0, "o")
165+
fig_ref.subplots().plot(datetime(2000, 1, 1), 0, "o")

lib/matplotlib/units.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,20 @@ def get_converter(self, x):
148148
# If there are no elements in x, infer the units from its dtype
149149
if not x.size:
150150
return self.get_converter(np.array([0], dtype=x.dtype))
151-
try: # Look up in the cache.
152-
return self[type(x)]
153-
except KeyError:
154-
try: # If cache lookup fails, look up based on first element...
155-
first = cbook.safe_first_element(x)
156-
except (TypeError, StopIteration):
151+
for cls in type(x).__mro__: # Look up in the cache.
152+
try:
153+
return self[cls]
154+
except KeyError:
157155
pass
158-
else:
159-
# ... and avoid infinite recursion for pathological iterables
160-
# where indexing returns instances of the same iterable class.
161-
if type(first) is not type(x):
162-
return self.get_converter(first)
156+
try: # If cache lookup fails, look up based on first element...
157+
first = cbook.safe_first_element(x)
158+
except (TypeError, StopIteration):
159+
pass
160+
else:
161+
# ... and avoid infinite recursion for pathological iterables for
162+
# which indexing returns instances of the same iterable class.
163+
if type(first) is not type(x):
164+
return self.get_converter(first)
163165
return None
164166

165167

0 commit comments

Comments
 (0)