171171import math
172172import string
173173import types
174- import warnings
175174from numbers import Integral
176175
177176import 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+ 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,7 @@ def __call__(self, x, pos=None):
414411
415412 *x* and *pos* are passed through as-is.
416413 """
414+ if self .nargs == 1 : return self .func (x )
417415 return self .func (x , pos )
418416
419417
0 commit comments