eric7/Documentation/Source/eric7.Plugins.CheckerPlugins.CodeStyleChecker.Annotations.AnnotationsFunctionVisitor.html

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

eric ide

mercurial