1
1
import ast
2
- from typing import Callable , Dict , Generator , List , Optional , Tuple , Type
2
+ from typing import (
3
+ Callable ,
4
+ Dict ,
5
+ Generator ,
6
+ List ,
7
+ Optional ,
8
+ Tuple ,
9
+ Type ,
10
+ Union ,
11
+ )
3
12
4
13
from pydoclint .utils import walk
5
14
from pydoclint .utils .annotation import unparseAnnotation
@@ -98,6 +107,14 @@ def getRaisedExceptions(node: FuncOrAsyncFuncDef) -> List[str]:
98
107
return sorted (set (_getRaisedExceptions (node )))
99
108
100
109
110
+ def _getFullAttribute (node : Union [ast .Attribute , ast .Name ]) -> str :
111
+ """Get the full name of a symbol like a.b.c.foo"""
112
+ if isinstance (node , ast .Name ):
113
+ return node .id
114
+
115
+ return _getFullAttribute (node .value ) + '.' + node .attr
116
+
117
+
101
118
def _getRaisedExceptions (
102
119
node : FuncOrAsyncFuncDef ,
103
120
) -> Generator [str , None , None ]:
@@ -132,7 +149,17 @@ def _getRaisedExceptions(
132
149
):
133
150
for subnode , _ in walk .walk_dfs (child ):
134
151
if isinstance (subnode , ast .Name ):
135
- yield subnode .id
152
+ if isinstance (child .exc , ast .Attribute ):
153
+ # case: looks like m.n.exception
154
+ yield _getFullAttribute (child .exc )
155
+ elif isinstance (child .exc , ast .Call ) and isinstance (
156
+ child .exc .func , ast .Attribute
157
+ ):
158
+ # case: looks like m.n.exception()
159
+ yield _getFullAttribute (child .exc .func )
160
+ else :
161
+ yield subnode .id
162
+
136
163
break
137
164
else :
138
165
# if "raise" statement was alone, it must be inside an "except"
@@ -149,9 +176,12 @@ def _extractExceptionsFromExcept(
149
176
yield node .type .id
150
177
151
178
if isinstance (node .type , ast .Tuple ):
152
- for child , _ in walk .walk (node .type ):
153
- if isinstance (child , ast .Name ):
154
- yield child .id
179
+ for elt in node .type .elts :
180
+ if isinstance (elt , ast .Attribute ):
181
+ # case: looks like m.n.exception
182
+ yield _getFullAttribute (elt )
183
+ elif isinstance (elt , ast .Name ):
184
+ yield elt .id
155
185
156
186
157
187
def _hasExpectedStatements (
0 commit comments