@@ -33,7 +33,12 @@ class AstValidator(validate.Validator):
3333 """
3434
3535 def __init__ (
36- self , * , ast_node : ast .AST , filename : os .PathLike , obj_name : str
36+ self ,
37+ * ,
38+ ast_node : ast .AST ,
39+ filename : os .PathLike ,
40+ obj_name : str ,
41+ ancestry : list [ast .AST ],
3742 ) -> None :
3843 self .node : ast .AST = ast_node
3944 self .raw_doc : str = ast .get_docstring (self .node , clean = False ) or ""
@@ -46,6 +51,8 @@ def __init__(
4651 self .is_class : bool = isinstance (ast_node , ast .ClassDef )
4752 self .is_module : bool = isinstance (ast_node , ast .Module )
4853
54+ self .ancestry : list [ast .AST ] = ancestry
55+
4956 @staticmethod
5057 def _load_obj (name ):
5158 raise NotImplementedError ("AstValidator does not support this method." )
@@ -91,7 +98,7 @@ def source_file_def_line(self) -> int:
9198
9299 @property
93100 def signature_parameters (self ) -> Tuple [str ]:
94- def extract_signature (node ):
101+ def extract_signature (node , parent ):
95102 args_node = node .args
96103 params = []
97104 for arg_type in ["posonlyargs" , "args" , "vararg" , "kwonlyargs" , "kwarg" ]:
@@ -104,17 +111,21 @@ def extract_signature(node):
104111 else :
105112 params .extend ([arg .arg for arg in entries ])
106113 params = tuple (params )
107- if params and params [0 ] in {"self" , "cls" }:
114+ if (
115+ params
116+ and params [0 ] in {"self" , "cls" }
117+ and isinstance (parent , ast .ClassDef )
118+ ):
108119 return params [1 :]
109120 return params
110121
111122 params = tuple ()
112123 if self .is_function_or_method :
113- params = extract_signature (self .node )
124+ params = extract_signature (self .node , self . ancestry [ - 1 ] )
114125 elif self .is_class :
115126 for child in self .node .body :
116127 if isinstance (child , ast .FunctionDef ) and child .name == "__init__" :
117- params = extract_signature (child )
128+ params = extract_signature (child , self . node )
118129 return params
119130
120131 @property
@@ -144,9 +155,23 @@ def __init__(
144155 self .config : dict = config
145156 self .filepath : str = filepath
146157 self .module_name : str = Path (self .filepath ).stem
147- self .stack : list [str ] = []
158+ self .stack : list [ast . AST ] = []
148159 self .findings : list = []
149160
161+ @property
162+ def node_name (self ) -> str :
163+ """
164+ Get the full name of the current node in the stack.
165+
166+ Returns
167+ -------
168+ str
169+ The full name of the current node in the stack.
170+ """
171+ return "." .join (
172+ [getattr (node , "name" , self .module_name ) for node in self .stack ]
173+ )
174+
150175 def _ignore_issue (self , node : ast .AST , check : str ) -> bool :
151176 """
152177 Check whether the issue should be ignored.
@@ -185,9 +210,13 @@ def _get_numpydoc_issues(self, node: ast.AST) -> None:
185210 node : ast.AST
186211 The node under inspection.
187212 """
188- name = "." . join ( self .stack )
213+ name = self .node_name
189214 report = validate .validate (
190- name , AstValidator , ast_node = node , filename = self .filepath
215+ name ,
216+ AstValidator ,
217+ ast_node = node ,
218+ filename = self .filepath ,
219+ ancestry = self .stack [:- 1 ],
191220 )
192221 self .findings .extend (
193222 [
@@ -209,13 +238,11 @@ def visit(self, node: ast.AST) -> None:
209238 if isinstance (
210239 node , (ast .Module , ast .ClassDef , ast .FunctionDef , ast .AsyncFunctionDef )
211240 ):
212- self .stack .append (
213- self .module_name if isinstance (node , ast .Module ) else node .name
214- )
241+ self .stack .append (node )
215242
216243 if not (
217244 self .config ["exclude" ]
218- and re .search (self .config ["exclude" ], "." . join ( self .stack ) )
245+ and re .search (self .config ["exclude" ], self .node_name )
219246 ):
220247 self ._get_numpydoc_issues (node )
221248
0 commit comments