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

Skip to content

Commit e756add

Browse files
committed
Make unit converters also handle instances of subclasses.
1 parent ece3282 commit e756add

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def get_converter(self, x):
149149
if not x.size:
150150
return self.get_converter(np.array([0], dtype=x.dtype))
151151
try:
152-
return self[type(x)]
153-
except KeyError:
152+
return next(self[cls] for cls in type(x).__mro__ if cls in self)
153+
except StopIteration:
154154
try:
155155
first = cbook.safe_first_element(x)
156156
except (TypeError, StopIteration):

0 commit comments

Comments
 (0)