@@ -605,7 +605,9 @@ def _infer(
605
605
DEPRECATED_ARGUMENT_DEFAULT = "DEPRECATED_ARGUMENT_DEFAULT"
606
606
607
607
608
- class Arguments (_base_nodes .AssignTypeNode ):
608
+ class Arguments (
609
+ _base_nodes .AssignTypeNode
610
+ ): # pylint: disable=too-many-instance-attributes
609
611
"""Class representing an :class:`ast.arguments` node.
610
612
611
613
An :class:`Arguments` node represents that arguments in a
@@ -704,7 +706,20 @@ class Arguments(_base_nodes.AssignTypeNode):
704
706
kwargannotation : NodeNG | None
705
707
"""The type annotation for the variable length keyword arguments."""
706
708
707
- def __init__ (self , vararg : str | None , kwarg : str | None , parent : NodeNG ) -> None :
709
+ vararg_node : AssignName | None
710
+ """The node for variable length arguments"""
711
+
712
+ kwarg_node : AssignName | None
713
+ """The node for variable keyword arguments"""
714
+
715
+ def __init__ (
716
+ self ,
717
+ vararg : str | None ,
718
+ kwarg : str | None ,
719
+ parent : NodeNG ,
720
+ vararg_node : AssignName | None = None ,
721
+ kwarg_node : AssignName | None = None ,
722
+ ) -> None :
708
723
"""Almost all attributes can be None for living objects where introspection failed."""
709
724
super ().__init__ (
710
725
parent = parent ,
@@ -720,6 +735,9 @@ def __init__(self, vararg: str | None, kwarg: str | None, parent: NodeNG) -> Non
720
735
self .kwarg = kwarg
721
736
"""The name of the variable length keyword arguments."""
722
737
738
+ self .vararg_node = vararg_node
739
+ self .kwarg_node = kwarg_node
740
+
723
741
# pylint: disable=too-many-arguments
724
742
def postinit (
725
743
self ,
@@ -780,8 +798,21 @@ def fromlineno(self) -> int:
780
798
781
799
@cached_property
782
800
def arguments (self ):
783
- """Get all the arguments for this node, including positional only and positional and keyword"""
784
- return list (itertools .chain ((self .posonlyargs or ()), self .args or ()))
801
+ """Get all the arguments for this node. This includes:
802
+ * Positional only arguments
803
+ * Positional arguments
804
+ * Keyword arguments
805
+ * Variable arguments (.e.g *args)
806
+ * Variable keyword arguments (e.g **kwargs)
807
+ """
808
+ retval = list (itertools .chain ((self .posonlyargs or ()), (self .args or ())))
809
+ if self .vararg_node :
810
+ retval .append (self .vararg_node )
811
+ retval += self .kwonlyargs or ()
812
+ if self .kwarg_node :
813
+ retval .append (self .kwarg_node )
814
+
815
+ return retval
785
816
786
817
def format_args (self , * , skippable_names : set [str ] | None = None ) -> str :
787
818
"""Get the arguments formatted as string.
@@ -911,15 +942,20 @@ def default_value(self, argname):
911
942
:raises NoDefault: If there is no default value defined for the
912
943
given argument.
913
944
"""
914
- args = self .arguments
945
+ args = [
946
+ arg for arg in self .arguments if arg .name not in [self .vararg , self .kwarg ]
947
+ ]
948
+
949
+ index = _find_arg (argname , self .kwonlyargs )[0 ]
950
+ if index is not None and self .kw_defaults [index ] is not None :
951
+ return self .kw_defaults [index ]
952
+
915
953
index = _find_arg (argname , args )[0 ]
916
954
if index is not None :
917
- idx = index - (len (args ) - len (self .defaults ))
955
+ idx = index - (len (args ) - len (self .defaults ) - len ( self . kw_defaults ) )
918
956
if idx >= 0 :
919
957
return self .defaults [idx ]
920
- index = _find_arg (argname , self .kwonlyargs )[0 ]
921
- if index is not None and self .kw_defaults [index ] is not None :
922
- return self .kw_defaults [index ]
958
+
923
959
raise NoDefault (func = self .parent , name = argname )
924
960
925
961
def is_argument (self , name ) -> bool :
@@ -934,11 +970,7 @@ def is_argument(self, name) -> bool:
934
970
return True
935
971
if name == self .kwarg :
936
972
return True
937
- return (
938
- self .find_argname (name )[1 ] is not None
939
- or self .kwonlyargs
940
- and _find_arg (name , self .kwonlyargs )[1 ] is not None
941
- )
973
+ return self .find_argname (name )[1 ] is not None
942
974
943
975
def find_argname (self , argname , rec = DEPRECATED_ARGUMENT_DEFAULT ):
944
976
"""Get the index and :class:`AssignName` node for given name.
@@ -956,7 +988,9 @@ def find_argname(self, argname, rec=DEPRECATED_ARGUMENT_DEFAULT):
956
988
stacklevel = 2 ,
957
989
)
958
990
if self .arguments :
959
- return _find_arg (argname , self .arguments )
991
+ index , argument = _find_arg (argname , self .arguments )
992
+ if argument :
993
+ return index , argument
960
994
return None , None
961
995
962
996
def get_children (self ):
0 commit comments