171
171
import math
172
172
import string
173
173
import types
174
- import warnings
175
174
from numbers import Integral
176
175
177
176
import numpy as np
@@ -386,25 +385,23 @@ class FuncFormatter(Formatter):
386
385
the corresponding tick label.
387
386
"""
388
387
def __init__ (self , func ):
388
+ self .func = func
389
+
389
390
if not isinstance (func , types .BuiltinFunctionType ):
390
- nargs = len (inspect .signature (func ).parameters )
391
- elif (func .__name__ == 'format' ):
391
+ self .nargs = len (inspect .signature (func ).parameters )
392
+ elif (isinstance (getattr (func , "__self__" ), str ) and
393
+ (getattr (func , "__name__" , "" ) == "format" )):
392
394
#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 ])
396
398
else :
397
399
#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. "
408
405
"FuncFormatter functions take at most 2: "
409
406
"x (required), pos (optional)." )
410
407
@@ -414,6 +411,7 @@ def __call__(self, x, pos=None):
414
411
415
412
*x* and *pos* are passed through as-is.
416
413
"""
414
+ if self .nargs == 1 : return self .func (x )
417
415
return self .func (x , pos )
418
416
419
417