Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: check locator and formatter args when passed #10772

Merged
merged 2 commits into from Mar 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/matplotlib/axis.py
Expand Up @@ -1568,6 +1568,9 @@ def set_major_formatter(self, formatter):

ACCEPTS: A :class:`~matplotlib.ticker.Formatter` instance
"""
if not hasattr(formatter, 'format_data'):
raise TypeError("formatter argument should be instance of "
Copy link
Member

@timhoffm timhoffm Mar 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one could shorten this to

raise TypeError("formatter must be a matplotlib.ticker.Formatter")

But that's a matter of taste. I'll leave this up to you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats what I had before but folks wanted "instance of" 😉

"matplotlib.ticker.Formatter")
self.isDefault_majfmt = False
self.major.formatter = formatter
formatter.set_axis(self)
Expand All @@ -1579,6 +1582,9 @@ def set_minor_formatter(self, formatter):

ACCEPTS: A :class:`~matplotlib.ticker.Formatter` instance
"""
if not hasattr(formatter, 'format_data'):
raise TypeError("formatter argument should be instance of "
"matplotlib.ticker.Formatter")
self.isDefault_minfmt = False
self.minor.formatter = formatter
formatter.set_axis(self)
Expand All @@ -1590,6 +1596,9 @@ def set_major_locator(self, locator):

ACCEPTS: a :class:`~matplotlib.ticker.Locator` instance
"""
if not hasattr(locator, 'tick_values'):
raise TypeError("formatter argument should be instance of "
"matplotlib.ticker.Locator")
self.isDefault_majloc = False
self.major.locator = locator
locator.set_axis(self)
Expand All @@ -1601,6 +1610,9 @@ def set_minor_locator(self, locator):

ACCEPTS: a :class:`~matplotlib.ticker.Locator` instance
"""
if not hasattr(locator, 'tick_values'):
raise TypeError("formatter argument should be instance of "
"matplotlib.ticker.Locator")
self.isDefault_minloc = False
self.minor.locator = locator
locator.set_axis(self)
Expand Down
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_ticker.py
Expand Up @@ -728,3 +728,27 @@ def test_latex(self, is_latex, usetex, expected):
fmt = mticker.PercentFormatter(symbol='\\{t}%', is_latex=is_latex)
with matplotlib.rc_context(rc={'text.usetex': usetex}):
assert fmt.format_pct(50, 100) == expected


def test_majformatter_type():
fig, ax = plt.subplots()
with pytest.raises(TypeError):
ax.xaxis.set_major_formatter(matplotlib.ticker.LogLocator())


def test_minformatter_type():
fig, ax = plt.subplots()
with pytest.raises(TypeError):
ax.xaxis.set_minor_formatter(matplotlib.ticker.LogLocator())


def test_majlocator_type():
fig, ax = plt.subplots()
with pytest.raises(TypeError):
ax.xaxis.set_major_locator(matplotlib.ticker.LogFormatter())


def test_minlocator_type():
fig, ax = plt.subplots()
with pytest.raises(TypeError):
ax.xaxis.set_minor_locator(matplotlib.ticker.LogFormatter())