|
1 <!DOCTYPE html> |
|
2 <html><head> |
|
3 <title>eric7.Plugins.CheckerPlugins.CodeStyleChecker.Annotations.AnnotationsFunctionVisitor</title> |
|
4 <meta charset="UTF-8"> |
|
5 <link rel="stylesheet" href="styles.css"> |
|
6 </head> |
|
7 <body> |
|
8 <a NAME="top" ID="top"></a> |
|
9 <h1>eric7.Plugins.CheckerPlugins.CodeStyleChecker.Annotations.AnnotationsFunctionVisitor</h1> |
|
10 |
|
11 <p> |
|
12 Module implementing a node visitor for function type annotations. |
|
13 </p> |
|
14 <h3>Global Attributes</h3> |
|
15 |
|
16 <table> |
|
17 <tr><td>AST_ARG_TYPES</td></tr> |
|
18 </table> |
|
19 <h3>Classes</h3> |
|
20 |
|
21 <table> |
|
22 |
|
23 <tr> |
|
24 <td><a href="#Argument">Argument</a></td> |
|
25 <td>Class representing a function argument.</td> |
|
26 </tr> |
|
27 <tr> |
|
28 <td><a href="#Function">Function</a></td> |
|
29 <td>Class representing a function.</td> |
|
30 </tr> |
|
31 <tr> |
|
32 <td><a href="#FunctionVisitor">FunctionVisitor</a></td> |
|
33 <td>Class implementing a node visitor to check function annotations.</td> |
|
34 </tr> |
|
35 <tr> |
|
36 <td><a href="#ReturnVisitor">ReturnVisitor</a></td> |
|
37 <td>Class implementing a node visitor to check the return statements of a function node.</td> |
|
38 </tr> |
|
39 </table> |
|
40 <h3>Functions</h3> |
|
41 |
|
42 <table> |
|
43 <tr><td>None</td></tr> |
|
44 </table> |
|
45 <hr /> |
|
46 <hr /> |
|
47 <a NAME="Argument" ID="Argument"></a> |
|
48 <h2>Argument</h2> |
|
49 |
|
50 <p> |
|
51 Class representing a function argument. |
|
52 </p> |
|
53 <h3>Derived from</h3> |
|
54 None |
|
55 <h3>Class Attributes</h3> |
|
56 |
|
57 <table> |
|
58 <tr><td>None</td></tr> |
|
59 </table> |
|
60 <h3>Class Methods</h3> |
|
61 |
|
62 <table> |
|
63 |
|
64 <tr> |
|
65 <td><a href="#Argument.fromNode">fromNode</a></td> |
|
66 <td>Class method to create an Argument object based on the given node.</td> |
|
67 </tr> |
|
68 </table> |
|
69 <h3>Methods</h3> |
|
70 |
|
71 <table> |
|
72 |
|
73 <tr> |
|
74 <td><a href="#Argument.__init__">Argument</a></td> |
|
75 <td>Constructor</td> |
|
76 </tr> |
|
77 </table> |
|
78 <h3>Static Methods</h3> |
|
79 |
|
80 <table> |
|
81 <tr><td>None</td></tr> |
|
82 </table> |
|
83 |
|
84 <a NAME="Argument.fromNode" ID="Argument.fromNode"></a> |
|
85 <h4>Argument.fromNode (class method)</h4> |
|
86 <b>fromNode</b>(<i>node, annotationTypeName</i>) |
|
87 |
|
88 <p> |
|
89 Class method to create an Argument object based on the given node. |
|
90 </p> |
|
91 <dl> |
|
92 |
|
93 <dt><i>node</i> (ast.arguments)</dt> |
|
94 <dd> |
|
95 reference to the node to be converted |
|
96 </dd> |
|
97 <dt><i>annotationTypeName</i> (str)</dt> |
|
98 <dd> |
|
99 name of the annotation type |
|
100 </dd> |
|
101 </dl> |
|
102 <dl> |
|
103 <dt>Return:</dt> |
|
104 <dd> |
|
105 Argument object |
|
106 </dd> |
|
107 </dl> |
|
108 <dl> |
|
109 <dt>Return Type:</dt> |
|
110 <dd> |
|
111 Argument |
|
112 </dd> |
|
113 </dl> |
|
114 <a NAME="Argument.__init__" ID="Argument.__init__"></a> |
|
115 <h4>Argument (Constructor)</h4> |
|
116 <b>Argument</b>(<i>argname, lineno, col_offset, annotationType, hasTypeAnnotation=False, has3107Annotation=False, hasTypeComment=False</i>) |
|
117 |
|
118 <p> |
|
119 Constructor |
|
120 </p> |
|
121 <dl> |
|
122 |
|
123 <dt><i>argname</i> (str)</dt> |
|
124 <dd> |
|
125 name of the argument |
|
126 </dd> |
|
127 <dt><i>lineno</i> (int)</dt> |
|
128 <dd> |
|
129 line number |
|
130 </dd> |
|
131 <dt><i>col_offset</i> (int)</dt> |
|
132 <dd> |
|
133 column number |
|
134 </dd> |
|
135 <dt><i>annotationType</i> (AnnotationType)</dt> |
|
136 <dd> |
|
137 type of annotation |
|
138 </dd> |
|
139 <dt><i>hasTypeAnnotation</i> (bool (optional))</dt> |
|
140 <dd> |
|
141 flag indicating the presence of a type |
|
142 annotation (defaults to False) |
|
143 </dd> |
|
144 <dt><i>has3107Annotation</i> (bool (optional))</dt> |
|
145 <dd> |
|
146 flag indicating the presence of a PEP 3107 |
|
147 annotation (defaults to False) |
|
148 </dd> |
|
149 <dt><i>hasTypeComment</i> (bool (optional))</dt> |
|
150 <dd> |
|
151 flag indicating the presence of a type comment |
|
152 (defaults to False) |
|
153 </dd> |
|
154 </dl> |
|
155 <div align="right"><a href="#top">Up</a></div> |
|
156 <hr /> |
|
157 <hr /> |
|
158 <a NAME="Function" ID="Function"></a> |
|
159 <h2>Function</h2> |
|
160 |
|
161 <p> |
|
162 Class representing a function. |
|
163 </p> |
|
164 <h3>Derived from</h3> |
|
165 None |
|
166 <h3>Class Attributes</h3> |
|
167 |
|
168 <table> |
|
169 <tr><td>None</td></tr> |
|
170 </table> |
|
171 <h3>Class Methods</h3> |
|
172 |
|
173 <table> |
|
174 |
|
175 <tr> |
|
176 <td><a href="#Function.fromNode">fromNode</a></td> |
|
177 <td>Class method to create a Function object from ast.FunctionDef or ast.AsyncFunctionDef nodes.</td> |
|
178 </tr> |
|
179 </table> |
|
180 <h3>Methods</h3> |
|
181 |
|
182 <table> |
|
183 |
|
184 <tr> |
|
185 <td><a href="#Function.__init__">Function</a></td> |
|
186 <td>Constructor</td> |
|
187 </tr> |
|
188 <tr> |
|
189 <td><a href="#Function.__decoratorChecker">__decoratorChecker</a></td> |
|
190 <td>Private method to check the provided decorator for a match against the provided set of check names.</td> |
|
191 </tr> |
|
192 <tr> |
|
193 <td><a href="#Function.getAnnotatedArguments">getAnnotatedArguments</a></td> |
|
194 <td>Public method to get list of arguments with type annotations.</td> |
|
195 </tr> |
|
196 <tr> |
|
197 <td><a href="#Function.getMissedAnnotations">getMissedAnnotations</a></td> |
|
198 <td>Public method to provide a list of arguments with missing type annotations.</td> |
|
199 </tr> |
|
200 <tr> |
|
201 <td><a href="#Function.hasDecorator">hasDecorator</a></td> |
|
202 <td>Public method to check whether the function node is decorated by any of the provided decorators.</td> |
|
203 </tr> |
|
204 <tr> |
|
205 <td><a href="#Function.isDynamicallyTyped">isDynamicallyTyped</a></td> |
|
206 <td>Public method to check, if a function definition is dynamically typed (i.e.</td> |
|
207 </tr> |
|
208 <tr> |
|
209 <td><a href="#Function.isFullyAnnotated">isFullyAnnotated</a></td> |
|
210 <td>Public method to check, if the function definition is fully type annotated.</td> |
|
211 </tr> |
|
212 </table> |
|
213 <h3>Static Methods</h3> |
|
214 |
|
215 <table> |
|
216 |
|
217 <tr> |
|
218 <td><a href="#Function._maybeInjectClassArgument">_maybeInjectClassArgument</a></td> |
|
219 <td>Static method to inject `self` or `cls` args into a type comment to align with PEP 3107-style annotations.</td> |
|
220 </tr> |
|
221 <tr> |
|
222 <td><a href="#Function._singleLineColonSeeker">_singleLineColonSeeker</a></td> |
|
223 <td>Static method to find the line & column indices of a single line function definition.</td> |
|
224 </tr> |
|
225 <tr> |
|
226 <td><a href="#Function.colonSeeker">colonSeeker</a></td> |
|
227 <td>Static method to find the line & column indices of the function definition's closing colon.</td> |
|
228 </tr> |
|
229 <tr> |
|
230 <td><a href="#Function.getClassDecoratorType">getClassDecoratorType</a></td> |
|
231 <td>Static method to get the class method's decorator type from its function node.</td> |
|
232 </tr> |
|
233 <tr> |
|
234 <td><a href="#Function.getFunctionType">getFunctionType</a></td> |
|
235 <td>Static method to determine the function's FunctionType from its name.</td> |
|
236 </tr> |
|
237 <tr> |
|
238 <td><a href="#Function.tryTypeComment">tryTypeComment</a></td> |
|
239 <td>Static method to infer type hints from a function-level type comment.</td> |
|
240 </tr> |
|
241 </table> |
|
242 |
|
243 <a NAME="Function.fromNode" ID="Function.fromNode"></a> |
|
244 <h4>Function.fromNode (class method)</h4> |
|
245 <b>fromNode</b>(<i>node, lines, **kwargs</i>) |
|
246 |
|
247 <p> |
|
248 Class method to create a Function object from ast.FunctionDef or |
|
249 ast.AsyncFunctionDef nodes. |
|
250 </p> |
|
251 <p> |
|
252 Accept the source code, as a list of strings, in order to get the |
|
253 column where the function definition ends. |
|
254 </p> |
|
255 <p> |
|
256 With exceptions, input kwargs are passed straight through to Function's |
|
257 __init__. The following kwargs will be overridden: |
|
258 * function_type |
|
259 * class_decorator_type |
|
260 * args |
|
261 </p> |
|
262 <dl> |
|
263 |
|
264 <dt><i>node</i> (ast.AsyncFunctionDef or ast.FunctionDef)</dt> |
|
265 <dd> |
|
266 reference to the function definition node |
|
267 </dd> |
|
268 <dt><i>lines</i> (list of str)</dt> |
|
269 <dd> |
|
270 list of source code lines |
|
271 </dd> |
|
272 <dt><i>**kwargs=</i> (dict)</dt> |
|
273 <dd> |
|
274 keyword arguments |
|
275 </dd> |
|
276 </dl> |
|
277 <dl> |
|
278 <dt>Return:</dt> |
|
279 <dd> |
|
280 created Function object |
|
281 </dd> |
|
282 </dl> |
|
283 <dl> |
|
284 <dt>Return Type:</dt> |
|
285 <dd> |
|
286 Function |
|
287 </dd> |
|
288 </dl> |
|
289 <a NAME="Function.__init__" ID="Function.__init__"></a> |
|
290 <h4>Function (Constructor)</h4> |
|
291 <b>Function</b>(<i>name, lineno, col_offset, functionType=FunctionType.PUBLIC, isClassMethod=False, classDecoratorType=None, isReturnAnnotated=False, hasTypeComment=False, hasOnlyNoneReturns=True, isNested=False, decoratorList=None, args=None</i>) |
|
292 |
|
293 <p> |
|
294 Constructor |
|
295 </p> |
|
296 <dl> |
|
297 |
|
298 <dt><i>name</i> (str)</dt> |
|
299 <dd> |
|
300 name of the function |
|
301 </dd> |
|
302 <dt><i>lineno</i> (int)</dt> |
|
303 <dd> |
|
304 line number |
|
305 </dd> |
|
306 <dt><i>col_offset</i> (int)</dt> |
|
307 <dd> |
|
308 column number |
|
309 </dd> |
|
310 <dt><i>functionType</i> (FunctionType (optional))</dt> |
|
311 <dd> |
|
312 type of the function (defaults to |
|
313 FunctionType.PUBLIC) |
|
314 </dd> |
|
315 <dt><i>isClassMethod</i> (bool (optional))</dt> |
|
316 <dd> |
|
317 flag indicating a class method (defaults to False) |
|
318 </dd> |
|
319 <dt><i>classDecoratorType</i> (ClassDecoratorType or None (optional))</dt> |
|
320 <dd> |
|
321 type of a function decorator |
|
322 (defaults to None) |
|
323 </dd> |
|
324 <dt><i>isReturnAnnotated</i> (bool (optional))</dt> |
|
325 <dd> |
|
326 flag indicating the presence of a return |
|
327 type annotation (defaults to False) |
|
328 </dd> |
|
329 <dt><i>hasTypeComment</i> (bool (optional))</dt> |
|
330 <dd> |
|
331 flag indicating the presence of a type comment |
|
332 (defaults to False) |
|
333 </dd> |
|
334 <dt><i>hasOnlyNoneReturns</i> (bool (optional))</dt> |
|
335 <dd> |
|
336 flag indicating only None return values |
|
337 (defaults to True) |
|
338 </dd> |
|
339 <dt><i>isNested</i> (bool (optional))</dt> |
|
340 <dd> |
|
341 flag indicating a nested function (defaults to False) |
|
342 </dd> |
|
343 <dt><i>decoratorList</i> (list of ast.Attribute, ast.Call or ast.Name (optional))</dt> |
|
344 <dd> |
|
345 list of decorator nodes (defaults to None) |
|
346 </dd> |
|
347 <dt><i>args</i> (list of Argument (optional))</dt> |
|
348 <dd> |
|
349 list of arguments (defaults to None) |
|
350 </dd> |
|
351 </dl> |
|
352 <a NAME="Function.__decoratorChecker" ID="Function.__decoratorChecker"></a> |
|
353 <h4>Function.__decoratorChecker</h4> |
|
354 <b>__decoratorChecker</b>(<i>decorator, checkDecorators</i>) |
|
355 |
|
356 <p> |
|
357 Private method to check the provided decorator for a match against the |
|
358 provided set of check names. |
|
359 </p> |
|
360 <p> |
|
361 Decorators are assumed to be of the following form: |
|
362 * `a.name` or `a.name()` |
|
363 * `name` or `name()` |
|
364 </p> |
|
365 <p> |
|
366 Note: Deeper imports (e.g. `a.b.name`) are not explicitly supported. |
|
367 </p> |
|
368 <dl> |
|
369 |
|
370 <dt><i>decorator</i> (ast.Attribute, ast.Call or ast.Name)</dt> |
|
371 <dd> |
|
372 decorator node to check |
|
373 </dd> |
|
374 <dt><i>checkDecorators</i> (set of str)</dt> |
|
375 <dd> |
|
376 set of decorators to check against |
|
377 </dd> |
|
378 </dl> |
|
379 <dl> |
|
380 <dt>Return:</dt> |
|
381 <dd> |
|
382 flag indicating the presence of any decorators |
|
383 </dd> |
|
384 </dl> |
|
385 <dl> |
|
386 <dt>Return Type:</dt> |
|
387 <dd> |
|
388 bool |
|
389 </dd> |
|
390 </dl> |
|
391 <a NAME="Function.getAnnotatedArguments" ID="Function.getAnnotatedArguments"></a> |
|
392 <h4>Function.getAnnotatedArguments</h4> |
|
393 <b>getAnnotatedArguments</b>(<i></i>) |
|
394 |
|
395 <p> |
|
396 Public method to get list of arguments with type annotations. |
|
397 </p> |
|
398 <dl> |
|
399 <dt>Return:</dt> |
|
400 <dd> |
|
401 list of arguments with type annotations. |
|
402 </dd> |
|
403 </dl> |
|
404 <dl> |
|
405 <dt>Return Type:</dt> |
|
406 <dd> |
|
407 list of Argument |
|
408 </dd> |
|
409 </dl> |
|
410 <a NAME="Function.getMissedAnnotations" ID="Function.getMissedAnnotations"></a> |
|
411 <h4>Function.getMissedAnnotations</h4> |
|
412 <b>getMissedAnnotations</b>(<i></i>) |
|
413 |
|
414 <p> |
|
415 Public method to provide a list of arguments with missing type |
|
416 annotations. |
|
417 </p> |
|
418 <dl> |
|
419 <dt>Return:</dt> |
|
420 <dd> |
|
421 list of arguments with missing type annotations |
|
422 </dd> |
|
423 </dl> |
|
424 <dl> |
|
425 <dt>Return Type:</dt> |
|
426 <dd> |
|
427 list of Argument |
|
428 </dd> |
|
429 </dl> |
|
430 <a NAME="Function.hasDecorator" ID="Function.hasDecorator"></a> |
|
431 <h4>Function.hasDecorator</h4> |
|
432 <b>hasDecorator</b>(<i>checkDecorators</i>) |
|
433 |
|
434 <p> |
|
435 Public method to check whether the function node is decorated by any of |
|
436 the provided decorators. |
|
437 </p> |
|
438 <p> |
|
439 Decorator matching is done against the provided `checkDecorators` set. |
|
440 Decorators are assumed to be either a module attribute (e.g. |
|
441 `@typing.overload`) or name (e.g. `@overload`). For the case of a |
|
442 module attribute, only the attribute is checked against |
|
443 `overload_decorators`. |
|
444 </p> |
|
445 <p> |
|
446 Note: Deeper decorator imports (e.g. `a.b.overload`) are not explicitly |
|
447 supported. |
|
448 </p> |
|
449 <dl> |
|
450 |
|
451 <dt><i>checkDecorators</i> (set of str)</dt> |
|
452 <dd> |
|
453 set of decorators to check against |
|
454 </dd> |
|
455 </dl> |
|
456 <dl> |
|
457 <dt>Return:</dt> |
|
458 <dd> |
|
459 flag indicating the presence of any decorators |
|
460 </dd> |
|
461 </dl> |
|
462 <dl> |
|
463 <dt>Return Type:</dt> |
|
464 <dd> |
|
465 bool |
|
466 </dd> |
|
467 </dl> |
|
468 <a NAME="Function.isDynamicallyTyped" ID="Function.isDynamicallyTyped"></a> |
|
469 <h4>Function.isDynamicallyTyped</h4> |
|
470 <b>isDynamicallyTyped</b>(<i></i>) |
|
471 |
|
472 <p> |
|
473 Public method to check, if a function definition is dynamically typed |
|
474 (i.e. completely lacking hints). |
|
475 </p> |
|
476 <dl> |
|
477 <dt>Return:</dt> |
|
478 <dd> |
|
479 flag indicating a dynamically typed function definition |
|
480 </dd> |
|
481 </dl> |
|
482 <dl> |
|
483 <dt>Return Type:</dt> |
|
484 <dd> |
|
485 bool |
|
486 </dd> |
|
487 </dl> |
|
488 <a NAME="Function.isFullyAnnotated" ID="Function.isFullyAnnotated"></a> |
|
489 <h4>Function.isFullyAnnotated</h4> |
|
490 <b>isFullyAnnotated</b>(<i></i>) |
|
491 |
|
492 <p> |
|
493 Public method to check, if the function definition is fully type |
|
494 annotated. |
|
495 </p> |
|
496 <p> |
|
497 Note: self.args will always include an Argument object for return. |
|
498 </p> |
|
499 <dl> |
|
500 <dt>Return:</dt> |
|
501 <dd> |
|
502 flag indicating a fully annotated function definition |
|
503 </dd> |
|
504 </dl> |
|
505 <dl> |
|
506 <dt>Return Type:</dt> |
|
507 <dd> |
|
508 bool |
|
509 </dd> |
|
510 </dl> |
|
511 <a NAME="Function._maybeInjectClassArgument" ID="Function._maybeInjectClassArgument"></a> |
|
512 <h4>Function._maybeInjectClassArgument (static)</h4> |
|
513 <b>_maybeInjectClassArgument</b>(<i>funcObj</i>) |
|
514 |
|
515 <p> |
|
516 Static method to inject `self` or `cls` args into a type comment to |
|
517 align with PEP 3107-style annotations. |
|
518 </p> |
|
519 <p> |
|
520 Because PEP 484 does not describe a method to provide partial function- |
|
521 level type comments, there is a potential for ambiguity in the context |
|
522 of both class methods and classmethods when aligning type comments to |
|
523 method arguments. |
|
524 </p> |
|
525 <p> |
|
526 These two class methods, for example, should lint equivalently: |
|
527 </p> |
|
528 <p> |
|
529 def bar(self, a): |
|
530 # type: (int) -> int |
|
531 ... |
|
532 </p> |
|
533 <p> |
|
534 def bar(self, a: int) -> int |
|
535 ... |
|
536 </p> |
|
537 <p> |
|
538 When this example type comment is parsed by `ast` and then matched with |
|
539 the method's arguments, it associates the `int` hint to `self` rather |
|
540 than `a`, so a dummy hint needs to be provided in situations where |
|
541 `self` or `class` are not hinted in the type comment in order to |
|
542 achieve equivalent linting results to PEP-3107 style annotations. |
|
543 </p> |
|
544 <p> |
|
545 A dummy `ast.Ellipses` constant is injected if the following criteria |
|
546 are met: |
|
547 1. The function node is either a class method or classmethod |
|
548 2. The number of hinted args is at least 1 less than the number |
|
549 of function args |
|
550 </p> |
|
551 <dl> |
|
552 |
|
553 <dt><i>hintTree</i> (ast.FunctionType)</dt> |
|
554 <dd> |
|
555 parsed type hint node |
|
556 </dd> |
|
557 <dt><i>funcObj</i> (Function)</dt> |
|
558 <dd> |
|
559 reference to the Function object |
|
560 </dd> |
|
561 </dl> |
|
562 <dl> |
|
563 <dt>Return:</dt> |
|
564 <dd> |
|
565 reference to the hint node |
|
566 </dd> |
|
567 </dl> |
|
568 <dl> |
|
569 <dt>Return Type:</dt> |
|
570 <dd> |
|
571 ast.FunctionType |
|
572 </dd> |
|
573 </dl> |
|
574 <a NAME="Function._singleLineColonSeeker" ID="Function._singleLineColonSeeker"></a> |
|
575 <h4>Function._singleLineColonSeeker (static)</h4> |
|
576 <b>_singleLineColonSeeker</b>(<i>line</i>) |
|
577 |
|
578 <p> |
|
579 Static method to find the line & column indices of a single line |
|
580 function definition. |
|
581 </p> |
|
582 <dl> |
|
583 |
|
584 <dt><i>node</i> (ast.AsyncFunctionDef or ast.FunctionDef)</dt> |
|
585 <dd> |
|
586 reference to the function definition node |
|
587 </dd> |
|
588 <dt><i>line</i> (str)</dt> |
|
589 <dd> |
|
590 source code line |
|
591 </dd> |
|
592 </dl> |
|
593 <dl> |
|
594 <dt>Return:</dt> |
|
595 <dd> |
|
596 line and column offset of the colon |
|
597 </dd> |
|
598 </dl> |
|
599 <dl> |
|
600 <dt>Return Type:</dt> |
|
601 <dd> |
|
602 tuple of (int, int) |
|
603 </dd> |
|
604 </dl> |
|
605 <a NAME="Function.colonSeeker" ID="Function.colonSeeker"></a> |
|
606 <h4>Function.colonSeeker (static)</h4> |
|
607 <b>colonSeeker</b>(<i>lines</i>) |
|
608 |
|
609 <p> |
|
610 Static method to find the line & column indices of the function |
|
611 definition's closing colon. |
|
612 </p> |
|
613 <dl> |
|
614 |
|
615 <dt><i>node</i> (ast.AsyncFunctionDef or ast.FunctionDef)</dt> |
|
616 <dd> |
|
617 reference to the function definition node |
|
618 </dd> |
|
619 <dt><i>lines</i> (list of str)</dt> |
|
620 <dd> |
|
621 list of source code lines |
|
622 </dd> |
|
623 </dl> |
|
624 <dl> |
|
625 <dt>Return:</dt> |
|
626 <dd> |
|
627 line and column offset of the colon |
|
628 </dd> |
|
629 </dl> |
|
630 <dl> |
|
631 <dt>Return Type:</dt> |
|
632 <dd> |
|
633 tuple of (int, int) |
|
634 </dd> |
|
635 </dl> |
|
636 <a NAME="Function.getClassDecoratorType" ID="Function.getClassDecoratorType"></a> |
|
637 <h4>Function.getClassDecoratorType (static)</h4> |
|
638 <b>getClassDecoratorType</b>(<i></i>) |
|
639 |
|
640 <p> |
|
641 Static method to get the class method's decorator type from its |
|
642 function node. |
|
643 </p> |
|
644 <p> |
|
645 Only @classmethod and @staticmethod decorators are identified; all |
|
646 other decorators are ignored |
|
647 </p> |
|
648 <p> |
|
649 If @classmethod or @staticmethod decorators are not present, this |
|
650 function will return None. |
|
651 </p> |
|
652 <dl> |
|
653 |
|
654 <dt><i>functionNode</i> (ast.AsyncFunctionDef or ast.FunctionDef)</dt> |
|
655 <dd> |
|
656 reference to the function definition node |
|
657 </dd> |
|
658 </dl> |
|
659 <dl> |
|
660 <dt>Return:</dt> |
|
661 <dd> |
|
662 class decorator type |
|
663 </dd> |
|
664 </dl> |
|
665 <dl> |
|
666 <dt>Return Type:</dt> |
|
667 <dd> |
|
668 ClassDecoratorType or None |
|
669 </dd> |
|
670 </dl> |
|
671 <a NAME="Function.getFunctionType" ID="Function.getFunctionType"></a> |
|
672 <h4>Function.getFunctionType (static)</h4> |
|
673 <b>getFunctionType</b>(<i></i>) |
|
674 |
|
675 <p> |
|
676 Static method to determine the function's FunctionType from its name. |
|
677 </p> |
|
678 <p> |
|
679 MethodType is determined by the following priority: |
|
680 1. Special: function name prefixed & suffixed by "__" |
|
681 2. Private: function name prefixed by "__" |
|
682 3. Protected: function name prefixed by "_" |
|
683 4. Public: everything else |
|
684 </p> |
|
685 <dl> |
|
686 |
|
687 <dt><i>functionName</i> (str)</dt> |
|
688 <dd> |
|
689 function name to be checked |
|
690 </dd> |
|
691 </dl> |
|
692 <dl> |
|
693 <dt>Return:</dt> |
|
694 <dd> |
|
695 type of function |
|
696 </dd> |
|
697 </dl> |
|
698 <dl> |
|
699 <dt>Return Type:</dt> |
|
700 <dd> |
|
701 FunctionType |
|
702 </dd> |
|
703 </dl> |
|
704 <a NAME="Function.tryTypeComment" ID="Function.tryTypeComment"></a> |
|
705 <h4>Function.tryTypeComment (static)</h4> |
|
706 <b>tryTypeComment</b>(<i>node</i>) |
|
707 |
|
708 <p> |
|
709 Static method to infer type hints from a function-level type comment. |
|
710 </p> |
|
711 <p> |
|
712 If a function is type commented it is assumed to have a return |
|
713 annotation, otherwise Python will fail to parse the hint. |
|
714 </p> |
|
715 <dl> |
|
716 |
|
717 <dt><i>funcObj</i> (Function)</dt> |
|
718 <dd> |
|
719 reference to the Function object |
|
720 </dd> |
|
721 <dt><i>node</i> (ast.AsyncFunctionDef or ast.FunctionDef)</dt> |
|
722 <dd> |
|
723 reference to the function definition node |
|
724 </dd> |
|
725 </dl> |
|
726 <dl> |
|
727 <dt>Return:</dt> |
|
728 <dd> |
|
729 reference to the modified Function object |
|
730 </dd> |
|
731 </dl> |
|
732 <dl> |
|
733 <dt>Return Type:</dt> |
|
734 <dd> |
|
735 Function |
|
736 </dd> |
|
737 </dl> |
|
738 <div align="right"><a href="#top">Up</a></div> |
|
739 <hr /> |
|
740 <hr /> |
|
741 <a NAME="FunctionVisitor" ID="FunctionVisitor"></a> |
|
742 <h2>FunctionVisitor</h2> |
|
743 |
|
744 <p> |
|
745 Class implementing a node visitor to check function annotations. |
|
746 </p> |
|
747 <h3>Derived from</h3> |
|
748 ast.NodeVisitor |
|
749 <h3>Class Attributes</h3> |
|
750 |
|
751 <table> |
|
752 <tr><td>AstFuncTypes</td></tr><tr><td>visit_AsyncFunctionDef</td></tr><tr><td>visit_ClassDef</td></tr><tr><td>visit_FunctionDef</td></tr> |
|
753 </table> |
|
754 <h3>Class Methods</h3> |
|
755 |
|
756 <table> |
|
757 <tr><td>None</td></tr> |
|
758 </table> |
|
759 <h3>Methods</h3> |
|
760 |
|
761 <table> |
|
762 |
|
763 <tr> |
|
764 <td><a href="#FunctionVisitor.__init__">FunctionVisitor</a></td> |
|
765 <td>Constructor</td> |
|
766 </tr> |
|
767 <tr> |
|
768 <td><a href="#FunctionVisitor.switchContext">switchContext</a></td> |
|
769 <td>Public method implementing a context switcher as a generic function visitor in order to track function context.</td> |
|
770 </tr> |
|
771 </table> |
|
772 <h3>Static Methods</h3> |
|
773 |
|
774 <table> |
|
775 <tr><td>None</td></tr> |
|
776 </table> |
|
777 |
|
778 <a NAME="FunctionVisitor.__init__" ID="FunctionVisitor.__init__"></a> |
|
779 <h4>FunctionVisitor (Constructor)</h4> |
|
780 <b>FunctionVisitor</b>(<i>lines</i>) |
|
781 |
|
782 <p> |
|
783 Constructor |
|
784 </p> |
|
785 <dl> |
|
786 |
|
787 <dt><i>lines</i> (list of str)</dt> |
|
788 <dd> |
|
789 source code lines of the function |
|
790 </dd> |
|
791 </dl> |
|
792 <a NAME="FunctionVisitor.switchContext" ID="FunctionVisitor.switchContext"></a> |
|
793 <h4>FunctionVisitor.switchContext</h4> |
|
794 <b>switchContext</b>(<i>node</i>) |
|
795 |
|
796 <p> |
|
797 Public method implementing a context switcher as a generic function |
|
798 visitor in order to track function context. |
|
799 </p> |
|
800 <p> |
|
801 Without keeping track of context, it's challenging to reliably |
|
802 differentiate class methods from "regular" functions, especially in the |
|
803 case of nested classes. |
|
804 </p> |
|
805 <dl> |
|
806 |
|
807 <dt><i>node</i> (ast.AsyncFunctionDef or ast.FunctionDef)</dt> |
|
808 <dd> |
|
809 reference to the function definition node to be analyzed |
|
810 </dd> |
|
811 </dl> |
|
812 <div align="right"><a href="#top">Up</a></div> |
|
813 <hr /> |
|
814 <hr /> |
|
815 <a NAME="ReturnVisitor" ID="ReturnVisitor"></a> |
|
816 <h2>ReturnVisitor</h2> |
|
817 |
|
818 <p> |
|
819 Class implementing a node visitor to check the return statements of a |
|
820 function node. |
|
821 </p> |
|
822 <p> |
|
823 If the function node being visited has an explicit return statement of |
|
824 anything other than `None`, the `instance.hasOnlyNoneReturns` flag will |
|
825 be set to `False`. |
|
826 </p> |
|
827 <p> |
|
828 If the function node being visited has no return statement, or contains |
|
829 only return statement(s) that explicitly return `None`, the |
|
830 `instance.hasOnlyNoneReturns` flag will be set to `True`. |
|
831 </p> |
|
832 <p> |
|
833 Due to the generic visiting being done, we need to keep track of the |
|
834 context in which a non-`None` return node is found. These functions are |
|
835 added to a set that is checked to see whether nor not the parent node is |
|
836 present. |
|
837 </p> |
|
838 <h3>Derived from</h3> |
|
839 ast.NodeVisitor |
|
840 <h3>Class Attributes</h3> |
|
841 |
|
842 <table> |
|
843 <tr><td>visit_AsyncFunctionDef</td></tr><tr><td>visit_FunctionDef</td></tr> |
|
844 </table> |
|
845 <h3>Class Methods</h3> |
|
846 |
|
847 <table> |
|
848 <tr><td>None</td></tr> |
|
849 </table> |
|
850 <h3>Methods</h3> |
|
851 |
|
852 <table> |
|
853 |
|
854 <tr> |
|
855 <td><a href="#ReturnVisitor.__init__">ReturnVisitor</a></td> |
|
856 <td>Constructor</td> |
|
857 </tr> |
|
858 <tr> |
|
859 <td><a href="#ReturnVisitor.hasOnlyNoneReturns">hasOnlyNoneReturns</a></td> |
|
860 <td>Public method indicating, that the parent node isn't in the visited nodes that don't return `None`.</td> |
|
861 </tr> |
|
862 <tr> |
|
863 <td><a href="#ReturnVisitor.switchContext">switchContext</a></td> |
|
864 <td>Public method implementing a context switcher as a generic function visitor in order to track function context.</td> |
|
865 </tr> |
|
866 <tr> |
|
867 <td><a href="#ReturnVisitor.visit_Return">visit_Return</a></td> |
|
868 <td>Public method to check each Return node to see if it returns anything other than `None`.</td> |
|
869 </tr> |
|
870 </table> |
|
871 <h3>Static Methods</h3> |
|
872 |
|
873 <table> |
|
874 <tr><td>None</td></tr> |
|
875 </table> |
|
876 |
|
877 <a NAME="ReturnVisitor.__init__" ID="ReturnVisitor.__init__"></a> |
|
878 <h4>ReturnVisitor (Constructor)</h4> |
|
879 <b>ReturnVisitor</b>(<i>parentNode</i>) |
|
880 |
|
881 <p> |
|
882 Constructor |
|
883 </p> |
|
884 <dl> |
|
885 |
|
886 <dt><i>parentNode</i> (ast.AsyncFunctionDef or ast.FunctionDef)</dt> |
|
887 <dd> |
|
888 reference to the function definition node to be |
|
889 analyzed |
|
890 </dd> |
|
891 </dl> |
|
892 <a NAME="ReturnVisitor.hasOnlyNoneReturns" ID="ReturnVisitor.hasOnlyNoneReturns"></a> |
|
893 <h4>ReturnVisitor.hasOnlyNoneReturns</h4> |
|
894 <b>hasOnlyNoneReturns</b>(<i></i>) |
|
895 |
|
896 <p> |
|
897 Public method indicating, that the parent node isn't in the visited |
|
898 nodes that don't return `None`. |
|
899 </p> |
|
900 <dl> |
|
901 <dt>Return:</dt> |
|
902 <dd> |
|
903 flag indicating, that the parent node isn't in the visited |
|
904 nodes that don't return `None` |
|
905 </dd> |
|
906 </dl> |
|
907 <dl> |
|
908 <dt>Return Type:</dt> |
|
909 <dd> |
|
910 bool |
|
911 </dd> |
|
912 </dl> |
|
913 <a NAME="ReturnVisitor.switchContext" ID="ReturnVisitor.switchContext"></a> |
|
914 <h4>ReturnVisitor.switchContext</h4> |
|
915 <b>switchContext</b>(<i>node</i>) |
|
916 |
|
917 <p> |
|
918 Public method implementing a context switcher as a generic function |
|
919 visitor in order to track function context. |
|
920 </p> |
|
921 <p> |
|
922 Without keeping track of context, it's challenging to reliably |
|
923 differentiate class methods from "regular" functions, especially in the |
|
924 case of nested classes. |
|
925 </p> |
|
926 <dl> |
|
927 |
|
928 <dt><i>node</i> (ast.AsyncFunctionDef or ast.FunctionDef)</dt> |
|
929 <dd> |
|
930 reference to the function definition node to be analyzed |
|
931 </dd> |
|
932 </dl> |
|
933 <a NAME="ReturnVisitor.visit_Return" ID="ReturnVisitor.visit_Return"></a> |
|
934 <h4>ReturnVisitor.visit_Return</h4> |
|
935 <b>visit_Return</b>(<i>node</i>) |
|
936 |
|
937 <p> |
|
938 Public method to check each Return node to see if it returns anything |
|
939 other than `None`. |
|
940 </p> |
|
941 <p> |
|
942 If the node being visited returns anything other than `None`, its |
|
943 parent context is added to the set of non-returning child nodes of |
|
944 the parent node. |
|
945 </p> |
|
946 <dl> |
|
947 |
|
948 <dt><i>node</i> (ast.Return)</dt> |
|
949 <dd> |
|
950 reference to the AST Return node |
|
951 </dd> |
|
952 </dl> |
|
953 <div align="right"><a href="#top">Up</a></div> |
|
954 <hr /> |
|
955 </body></html> |