@@ -202,7 +202,7 @@ def will(frame: int = 1, raise_exc: bool = True) -> str:
202
202
203
203
204
204
def nameof (
205
- var , # pylint: disable=unused-argument
205
+ var , # pylint: disable=unused-argument
206
206
* more_vars ,
207
207
# *, keyword only argument, supported with python3.8+
208
208
frame : int = 1 ,
@@ -294,10 +294,10 @@ def nameof(
294
294
out = argname2 (
295
295
"var" , "*more_vars" , func = nameof , frame = frame , vars_only = vars_only
296
296
)
297
- return out if more_vars else out [0 ] # type: ignore
297
+ return out if more_vars else out [0 ] # type: ignore
298
298
299
299
300
- def argname ( # pylint: disable=unused-argument,too-many-branches
300
+ def argname ( # pylint: disable=unused-argument,too-many-branches
301
301
arg : Any ,
302
302
* more_args : Any ,
303
303
# *, keyword-only argument, only available with python3.8+
@@ -393,7 +393,7 @@ def argname( # pylint: disable=unused-argument,too-many-branches
393
393
pos_only = pos_only ,
394
394
)
395
395
396
- ret = [] # type: List[ArgSourceType]
396
+ ret = [] # type: List[ArgSourceType]
397
397
for argnode in argname_node .args :
398
398
if not isinstance (argnode , (ast .Name , ast .Subscript , ast .Starred )):
399
399
raise ValueError (
@@ -412,7 +412,7 @@ def argname( # pylint: disable=unused-argument,too-many-branches
412
412
raise ValueError (
413
413
f"No such variable positional argument { posvar !r} "
414
414
)
415
- ret .extend (argument_sources [argnode .value .id ]) # type: ignore
415
+ ret .extend (argument_sources [argnode .value .id ]) # type: ignore
416
416
417
417
elif isinstance (argnode , ast .Name ):
418
418
if argnode .id not in argument_sources :
@@ -441,7 +441,7 @@ def argname( # pylint: disable=unused-argument,too-many-branches
441
441
f"{ name !r} is not a keyword argument "
442
442
"(**kwargs, for example)."
443
443
)
444
- ret .append (argument_sources [name ][subscript ]) # type: ignore
444
+ ret .append (argument_sources [name ][subscript ]) # type: ignore
445
445
446
446
if vars_only :
447
447
for source in ret :
@@ -451,7 +451,7 @@ def argname( # pylint: disable=unused-argument,too-many-branches
451
451
"or an attribute."
452
452
)
453
453
454
- return ret [0 ] if not more_args else tuple (ret ) # type: ignore
454
+ return ret [0 ] if not more_args else tuple (ret ) # type: ignore
455
455
456
456
457
457
def argname2 (
@@ -461,6 +461,7 @@ def argname2(
461
461
func : Callable = None ,
462
462
dispatch : Type = None ,
463
463
frame : int = 1 ,
464
+ ignore : IgnoreType = None ,
464
465
vars_only : bool = True ,
465
466
) -> ArgSourceType :
466
467
"""Get the names/sources of arguments passed to a function.
@@ -505,14 +506,19 @@ def argname2(
505
506
specified, expect `func` to be the generic function if provided.
506
507
frame: The frame where target function is called from this call.
507
508
Calls from python standard libraries are ignored.
509
+ ignore: The intermediate calls to be ignored. See `varname.ignore`
508
510
vars_only: Require the arguments to be variables only.
509
511
If False, `asttokens` is required to retrieve the source.
510
512
511
513
Returns:
512
514
Scalar string if
513
515
514
516
"""
515
- ignore_list = IgnoreList .create (ignore_lambda = False , ignore_varname = False )
517
+ ignore_list = IgnoreList .create (
518
+ ignore ,
519
+ ignore_lambda = False ,
520
+ ignore_varname = False ,
521
+ )
516
522
# where func(...) is called, skip the argname2() call
517
523
func_frame = ignore_list .get_frame (frame + 1 )
518
524
func_node = get_node_by_frame (func_frame )
@@ -545,17 +551,17 @@ def argname2(
545
551
vars_only = vars_only ,
546
552
pos_only = False ,
547
553
)
548
- except TypeError as terr :
554
+ except Exception as err :
549
555
raise VarnameRetrievingError (
550
556
"Have you specified the right `frame`?"
551
- ) from terr
557
+ ) from err
552
558
553
- out = [] # type: List[ArgSourceType]
559
+ out = [] # type: List[ArgSourceType]
554
560
farg_star = False
555
561
for farg in (arg , * more_args ):
556
562
557
563
farg_name = farg
558
- farg_subscript = None # type: str | int
564
+ farg_subscript = None # type: str | int
559
565
match = re .match (r"^([\w_]+)\[(.+)\]$" , farg )
560
566
if match :
561
567
farg_name = match .group (1 )
@@ -582,7 +588,7 @@ def argname2(
582
588
)
583
589
584
590
if farg_subscript is not None :
585
- out .append (source [farg_subscript ]) # type: ignore
591
+ out .append (source [farg_subscript ]) # type: ignore
586
592
elif farg_star :
587
593
out .extend (source )
588
594
else :
@@ -591,5 +597,5 @@ def argname2(
591
597
return (
592
598
out [0 ]
593
599
if not more_args and not farg_star
594
- else tuple (out ) # type: ignore
600
+ else tuple (out ) # type: ignore
595
601
)
0 commit comments