Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Make unit converters also handle instances of subclasses.#13536

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
dstansby merged 1 commit intomatplotlib:masterfromanntzer:unit-subclasses
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes fromall 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
5 changes: 5 additions & 0 deletionsdoc/users/next_whats_new/2019-02-27-AL.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
Unit converters now handle instances of subclasses
``````````````````````````````````````````````````

Unit converters now also handle instances of subclasses of the class they have
been registered for.
16 changes: 12 additions & 4 deletionslib/matplotlib/tests/test_units.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
from datetime import datetime
import platform
from unittest.mock import MagicMock

import matplotlib.pyplot as plt
from matplotlib.testing.decorators import image_comparison
from matplotlib.testing.decorators importcheck_figures_equal,image_comparison
import matplotlib.units as munits
import numpy as np
import platform
import pytest


Expand DownExpand Up@@ -119,7 +120,6 @@ def test_empty_set_limits_with_units(quantity_converter):
@image_comparison(['jpl_bar_units.png'],
savefig_kwarg={'dpi': 120}, style='mpl20')
def test_jpl_bar_units():
from datetime import datetime
import matplotlib.testing.jpl_units as units
units.register()

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

Expand DownExpand Up@@ -164,3 +163,12 @@ def test_scatter_element0_masked():
fig, ax = plt.subplots()
ax.scatter(times, y)
fig.canvas.draw()


@check_figures_equal(extensions=["png"])
def test_subclass(fig_test, fig_ref):
class subdate(datetime):
pass

fig_test.subplots().plot(subdate(2000, 1, 1), 0, "o")
fig_ref.subplots().plot(datetime(2000, 1, 1), 0, "o")
24 changes: 13 additions & 11 deletionslib/matplotlib/units.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -205,18 +205,20 @@ def get_converter(self, x):
# If there are no elements in x, infer the units from its dtype
if not x.size:
return self.get_converter(np.array([0], dtype=x.dtype))
try: # Look up in the cache.
return self[type(x)]
except KeyError:
try: # If cache lookup fails, look up based on first element...
first = cbook.safe_first_element(x)
except (TypeError, StopIteration):
for cls in type(x).__mro__: # Look up in the cache.
try:
return self[cls]
except KeyError:
pass
else:
# ... and avoid infinite recursion for pathological iterables
# where indexing returns instances of the same iterable class.
if type(first) is not type(x):
return self.get_converter(first)
try: # If cache lookup fails, look up based on first element...
first = cbook.safe_first_element(x)
except (TypeError, StopIteration):
pass
else:
# ... and avoid infinite recursion for pathological iterables for
# which indexing returns instances of the same iterable class.
if type(first) is not type(x):
return self.get_converter(first)
return None


Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp