33import ast
44import re
55import warnings
6- from typing import Any , List , Union , Tuple , Type , Callable , overload
6+ from typing import Any , List , Union , Tuple , Type , Callable , Optional , overload
77
88from executing import Source
99
2727
2828def varname (
2929 frame : int = 1 ,
30- ignore : IgnoreType = None ,
30+ ignore : IgnoreType | None = None ,
3131 multi_vars : bool = False ,
3232 raise_exc : bool = True ,
3333 strict : bool = True ,
34- ) -> Union [str , Tuple [Union [str , Tuple ], ...]]:
34+ ) -> Optional [ Union [str , Tuple [Union [str , Tuple ], ...] ]]:
3535 """Get the name of the variable(s) that assigned by function call or
3636 class instantiation.
3737
@@ -148,10 +148,10 @@ class instantiation.
148148 )
149149 )
150150
151- return names [0 ]
151+ return names [0 ] # type: ignore
152152
153153
154- def will (frame : int = 1 , raise_exc : bool = True ) -> str :
154+ def will (frame : int = 1 , raise_exc : bool = True ) -> Optional [ str ] :
155155 """Detect the attribute name right immediately after a function call.
156156
157157 Examples:
@@ -204,7 +204,7 @@ def will(frame: int = 1, raise_exc: bool = True) -> str:
204204 return None
205205
206206 # try to get node inst.attr from inst.attr()
207- node = node .parent
207+ node = node .parent # type: ignore
208208
209209 # see test_will_fail
210210 if not isinstance (node , ast .Attribute ):
@@ -309,12 +309,12 @@ def nameof(
309309 # We don't have to check keyword arguments here, as the instruction
310310 # will then be CALL_FUNCTION_KW.
311311 if not more_vars :
312- return bytecode_nameof (frameobj .f_code , frameobj .f_lasti )
312+ return bytecode_nameof (frameobj .f_code , frameobj .f_lasti ) # type: ignore
313313
314314 # We are anyway raising exceptions, no worries about additional burden
315315 # of frame retrieval again
316- source = frameobj .f_code .co_filename
317- if source == "<stdin>" :
316+ source = frameobj .f_code .co_filename # type: ignore
317+ if source == "<stdin>" : # pragma: no cover
318318 raise VarnameRetrievingError (
319319 "Are you trying to call nameof in REPL/python shell? "
320320 "In such a case, nameof can only be called with single "
@@ -345,10 +345,10 @@ def nameof(
345345def argname (
346346 arg : str ,
347347 * ,
348- func : Callable = None ,
349- dispatch : Type = None ,
348+ func : Optional [ Callable ] = None ,
349+ dispatch : Optional [ Type ] = None ,
350350 frame : int = 1 ,
351- ignore : IgnoreType = None ,
351+ ignore : Optional [ IgnoreType ] = None ,
352352 vars_only : bool = True ,
353353) -> ArgSourceType : # pragma: no cover
354354 ...
@@ -360,10 +360,10 @@ def argname(
360360 more_arg : str ,
361361 / , # introduced in python 3.8
362362 * more_args : str ,
363- func : Callable = None ,
364- dispatch : Type = None ,
363+ func : Optional [ Callable ] = None ,
364+ dispatch : Optional [ Type ] = None ,
365365 frame : int = 1 ,
366- ignore : IgnoreType = None ,
366+ ignore : Optional [ IgnoreType ] = None ,
367367 vars_only : bool = True ,
368368) -> Tuple [ArgSourceType , ...]: # pragma: no cover
369369 ...
@@ -372,10 +372,10 @@ def argname(
372372def argname (
373373 arg : str ,
374374 * more_args : str ,
375- func : Callable = None ,
376- dispatch : Type = None ,
375+ func : Optional [ Callable ] = None ,
376+ dispatch : Optional [ Type ] = None ,
377377 frame : int = 1 ,
378- ignore : IgnoreType = None ,
378+ ignore : Optional [ IgnoreType ] = None ,
379379 vars_only : bool = True ,
380380) -> Union [ArgSourceType , Tuple [ArgSourceType , ...]]:
381381 """Get the names/sources of arguments passed to a function.
@@ -456,10 +456,10 @@ def argname(
456456 func_node = reconstruct_func_node (func_node )
457457
458458 if not func :
459- func = get_function_called_argname (func_frame , func_node )
459+ func = get_function_called_argname (func_frame , func_node ) # type: ignore
460460
461461 if dispatch :
462- func = func .dispatch (dispatch )
462+ func = func .dispatch (dispatch ) # type: ignore
463463
464464 # don't pass the target arguments so that we can cache the sources in
465465 # the same call. For example:
@@ -468,9 +468,9 @@ def argname(
468468 # >>> b_name = argname(b)
469469 try :
470470 argument_sources = get_argument_sources (
471- Source .for_frame (func_frame ),
471+ Source .for_frame (func_frame ), # type: ignore
472472 func_node ,
473- func ,
473+ func , # type: ignore
474474 vars_only = vars_only ,
475475 )
476476 except Exception as err :
@@ -483,13 +483,13 @@ def argname(
483483 for farg in (arg , * more_args ):
484484
485485 farg_name = farg
486- farg_subscript = None # type: str | int
486+ farg_subscript = None # type: Optional[ str]
487487 match = re .match (r"^([\w_]+)\[(.+)\]$" , farg )
488488 if match :
489489 farg_name = match .group (1 )
490490 farg_subscript = match .group (2 )
491- if farg_subscript .isdigit ():
492- farg_subscript = int (farg_subscript )
491+ if farg_subscript .isdigit (): # type: ignore
492+ farg_subscript = int (farg_subscript ) # type: ignore
493493 else :
494494 match = re .match (r"^\*([\w_]+)$" , farg )
495495 if match :
0 commit comments