8000 @anntzer suggestions - gettattr, cbook.warn, switch in __call__ · matplotlib/matplotlib@394d73a · GitHub
[go: up one dir, main page]

Skip to content

Commit 394d73a

Browse files
committed
@anntzer suggestions - gettattr, cbook.warn, switch in __call__
flake errors
1 parent 0ed733c commit 394d73a

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

lib/matplotlib/tests/test_ticker.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@ def test_basic(self):
10421042

10431043

10441044
class TestFuncFormatter:
1045+
<<<<<<< HEAD
10451046
tests = [("function, 1 input",
10461047
(lambda x: f"{x}!", [2], "2!")),
10471048
("function 2 inputs",
@@ -1051,7 +1052,6 @@ class TestFuncFormatter:
10511052
("format 2 inputs",
10521053
("{}+{}!".format, [2, 3], "2+3!"))]
10531054
ids, data = zip(*tests)
1054-
10551055
@pytest.mark.parametrize("func, args, expected", data, ids=ids)
10561056
def test_arguments(self, func, args, expected):
10571057
assert expected == mticker.FuncFormatter(func)(*args)
@@ -1067,6 +1067,31 @@ def test_other_builtins(self):
10671067
with pytest.raises(UserWarning, match=r'max is not'):
10681068
mticker.FuncFormatter(max)
10691069

1070+
=======
1071+
def test_input_x_pos(self):
1072+
def func(x, pos):
1073+
return f"{x}+{pos}"
1074+
funcform = mticker.FuncFormatter(func)
1075+
assert "1+2" == funcform(1, 2)
1076+
1077+
def text_input_x(self):
1078+
def func(x):
1079+
f"hello {x}"
1080+
funcform = mticker.FuncFormatter(func)
1081+
assert "hello 0" == funcform(0)
1082+
1083+
def test_error(self):
1084+
with pytest.raises(TypeError, match=r"FuncFormatter*"):
1085+
def func(x, y, z):
1086+
" ".join([x, y, z])
1087+
funcform = mticker.FuncFormatter(func)
1088+
funcform(1, 2, 3)
1089+
1090+
def test_built_in(self):
1091+
funcform = mticker.FuncFormatter("{} hi!".format)
1092+
assert "42 hi!" == funcform(42)
1093+
>>>>>>> 103314bfb... flake errors
1094+
10701095

10711096
class TestStrMethodFormatter:
10721097
test_data = [

lib/matplotlib/ticker.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@
171171
import math
172172
import string
173173
import types
174-
import warnings
175174
from numbers import Integral
176175

177176
import numpy as np
@@ -386,25 +385,23 @@ class FuncFormatter(Formatter):
386385
the corresponding tick label.
387386
"""
388387
def __init__(self, func):
388+
self.func = func
389+
389390
if not isinstance(func, types.BuiltinFunctionType):
390-
nargs = len(inspect.signature(func).parameters)
391-
elif (func.__name__ == 'format'):
391 8000 +
self.nargs = len(inspect.signature(func).parameters)
392+
elif (isinstance(getattr(func, "__self__"), str) and
393+
(getattr(func, "__name__", "") == "format")):
392394
#check if there's a format spec
393-
nargs = len([(_, _, fs, _) for (_, _, fs, _)
394-
in string.Formatter().parse(func.__self__)
395-
if fs is not None])
395+
self.nargs = len([(_, _, fs, _) for (_, _, fs, _)
396+
in string.Formatter().parse(func.__self__)
397+
if fs is not None])
396398
else:
397399
#finding argcount for other builtins is a mess
398-
nargs = 2
399-
raise warnings.warn(f"{func.__name__} is not supported "
400-
"and may not work as expected")
401-
402-
if nargs == 1:
403-
self.func = lambda x, pos: func(x)
404-
elif nargs == 2:
405-
self.func = func
406-
else:
407-
raise TypeError(f"{func.__name__} takes {nargs} arguments. "
400+
self.nargs = 2
401+
cbook._warn_external(f"{func.__name__} is not supported "
402+
"and may not work as expected")
403+
if self.nargs not in [1, 2]:
404+
raise TypeError(f"{func.__name__} takes {self.nargs} arguments. "
408405
"FuncFormatter functions take at most 2: "
409406
"x (required), pos (optional).")
410407

@@ -414,6 +411,8 @@ def __call__(self, x, pos=None):
414411
415412
*x* and *pos* are passed through as-is.
416413
"""
414+
if self.nargs == 1:
415+
return self.func(x)
417416
return self.func(x, pos)
418417

419418

0 commit comments

Comments
 (0)
0