115 Argument |
115 Argument |
116 </dd> |
116 </dd> |
117 </dl> |
117 </dl> |
118 <a NAME="Argument.__init__" ID="Argument.__init__"></a> |
118 <a NAME="Argument.__init__" ID="Argument.__init__"></a> |
119 <h4>Argument (Constructor)</h4> |
119 <h4>Argument (Constructor)</h4> |
120 <b>Argument</b>(<i>argname, lineno, col_offset, annotationType, hasTypeAnnotation=False, has3107Annotation=False, hasTypeComment=False, isDynamicallyTyped=False, </i>) |
120 <b>Argument</b>(<i>argname, lineno, col_offset, annotationType, hasTypeAnnotation=False, hasTypeComment=False, isDynamicallyTyped=False, </i>) |
121 |
121 |
122 <p> |
122 <p> |
123 Constructor |
123 Constructor |
124 </p> |
124 </p> |
125 <dl> |
125 <dl> |
141 type of annotation |
141 type of annotation |
142 </dd> |
142 </dd> |
143 <dt><i>hasTypeAnnotation</i> (bool (optional))</dt> |
143 <dt><i>hasTypeAnnotation</i> (bool (optional))</dt> |
144 <dd> |
144 <dd> |
145 flag indicating the presence of a type |
145 flag indicating the presence of a type |
146 annotation (defaults to False) |
|
147 </dd> |
|
148 <dt><i>has3107Annotation</i> (bool (optional))</dt> |
|
149 <dd> |
|
150 flag indicating the presence of a PEP 3107 |
|
151 annotation (defaults to False) |
146 annotation (defaults to False) |
152 </dd> |
147 </dd> |
153 <dt><i>hasTypeComment</i> (bool (optional))</dt> |
148 <dt><i>hasTypeComment</i> (bool (optional))</dt> |
154 <dd> |
149 <dd> |
155 flag indicating the presence of a type comment |
150 flag indicating the presence of a type comment |
171 <p> |
166 <p> |
172 Support is provided for the following patterns: |
167 Support is provided for the following patterns: |
173 * 'from typing import Any; foo: Any' |
168 * 'from typing import Any; foo: Any' |
174 * 'import typing; foo: typing.Any' |
169 * 'import typing; foo: typing.Any' |
175 * 'import typing as <alias>; foo: <alias>.Any' |
170 * 'import typing as <alias>; foo: <alias>.Any' |
176 </p> |
|
177 <p> |
|
178 Type comments are also supported. Inline type comments are assumed to be |
|
179 passed here as 'str', and function-level type comments are assumed to be |
|
180 passed as 'ast.expr'. |
|
181 </p> |
171 </p> |
182 <dl> |
172 <dl> |
183 |
173 |
184 <dt><i>argExpr</i> (ast.expr or str)</dt> |
174 <dt><i>argExpr</i> (ast.expr or str)</dt> |
185 <dd> |
175 <dd> |
259 <h3>Static Methods</h3> |
249 <h3>Static Methods</h3> |
260 |
250 |
261 <table> |
251 <table> |
262 |
252 |
263 <tr> |
253 <tr> |
264 <td><a href="#Function._maybeInjectClassArgument">_maybeInjectClassArgument</a></td> |
|
265 <td>Static method to inject `self` or `cls` args into a type comment to align with PEP 3107-style annotations.</td> |
|
266 </tr> |
|
267 <tr> |
|
268 <td><a href="#Function._singleLineColonSeeker">_singleLineColonSeeker</a></td> |
254 <td><a href="#Function._singleLineColonSeeker">_singleLineColonSeeker</a></td> |
269 <td>Static method to find the line & column indices of a single line function definition.</td> |
255 <td>Static method to find the line & column indices of a single line function definition.</td> |
270 </tr> |
256 </tr> |
271 <tr> |
257 <tr> |
272 <td><a href="#Function.colonSeeker">colonSeeker</a></td> |
258 <td><a href="#Function.colonSeeker">colonSeeker</a></td> |
277 <td>Static method to get the class method's decorator type from its function node.</td> |
263 <td>Static method to get the class method's decorator type from its function node.</td> |
278 </tr> |
264 </tr> |
279 <tr> |
265 <tr> |
280 <td><a href="#Function.getFunctionType">getFunctionType</a></td> |
266 <td><a href="#Function.getFunctionType">getFunctionType</a></td> |
281 <td>Static method to determine the function's FunctionType from its name.</td> |
267 <td>Static method to determine the function's FunctionType from its name.</td> |
282 </tr> |
|
283 <tr> |
|
284 <td><a href="#Function.tryTypeComment">tryTypeComment</a></td> |
|
285 <td>Static method to infer type hints from a function-level type comment.</td> |
|
286 </tr> |
268 </tr> |
287 </table> |
269 </table> |
288 |
270 |
289 <a NAME="Function.fromNode" ID="Function.fromNode"></a> |
271 <a NAME="Function.fromNode" ID="Function.fromNode"></a> |
290 <h4>Function.fromNode (class method)</h4> |
272 <h4>Function.fromNode (class method)</h4> |
552 <dt>Return Type:</dt> |
534 <dt>Return Type:</dt> |
553 <dd> |
535 <dd> |
554 bool |
536 bool |
555 </dd> |
537 </dd> |
556 </dl> |
538 </dl> |
557 <a NAME="Function._maybeInjectClassArgument" ID="Function._maybeInjectClassArgument"></a> |
|
558 <h4>Function._maybeInjectClassArgument (static)</h4> |
|
559 <b>_maybeInjectClassArgument</b>(<i>funcObj</i>) |
|
560 |
|
561 <p> |
|
562 Static method to inject `self` or `cls` args into a type comment to |
|
563 align with PEP 3107-style annotations. |
|
564 </p> |
|
565 <p> |
|
566 Because PEP 484 does not describe a method to provide partial function- |
|
567 level type comments, there is a potential for ambiguity in the context |
|
568 of both class methods and classmethods when aligning type comments to |
|
569 method arguments. |
|
570 </p> |
|
571 <p> |
|
572 These two class methods, for example, should lint equivalently: |
|
573 </p> |
|
574 <p> |
|
575 def bar(self, a): |
|
576 # type: (int) -> int |
|
577 ... |
|
578 </p> |
|
579 <p> |
|
580 def bar(self, a: int) -> int |
|
581 ... |
|
582 </p> |
|
583 <p> |
|
584 When this example type comment is parsed by `ast` and then matched with |
|
585 the method's arguments, it associates the `int` hint to `self` rather |
|
586 than `a`, so a dummy hint needs to be provided in situations where |
|
587 `self` or `class` are not hinted in the type comment in order to |
|
588 achieve equivalent linting results to PEP-3107 style annotations. |
|
589 </p> |
|
590 <p> |
|
591 A dummy `ast.Ellipses` constant is injected if the following criteria |
|
592 are met: |
|
593 1. The function node is either a class method or classmethod |
|
594 2. The number of hinted args is at least 1 less than the number |
|
595 of function args |
|
596 </p> |
|
597 <dl> |
|
598 |
|
599 <dt><i>hintTree</i> (ast.FunctionType)</dt> |
|
600 <dd> |
|
601 parsed type hint node |
|
602 </dd> |
|
603 <dt><i>funcObj</i> (Function)</dt> |
|
604 <dd> |
|
605 reference to the Function object |
|
606 </dd> |
|
607 </dl> |
|
608 <dl> |
|
609 <dt>Return:</dt> |
|
610 <dd> |
|
611 reference to the hint node |
|
612 </dd> |
|
613 </dl> |
|
614 <dl> |
|
615 <dt>Return Type:</dt> |
|
616 <dd> |
|
617 ast.FunctionType |
|
618 </dd> |
|
619 </dl> |
|
620 <a NAME="Function._singleLineColonSeeker" ID="Function._singleLineColonSeeker"></a> |
539 <a NAME="Function._singleLineColonSeeker" ID="Function._singleLineColonSeeker"></a> |
621 <h4>Function._singleLineColonSeeker (static)</h4> |
540 <h4>Function._singleLineColonSeeker (static)</h4> |
622 <b>_singleLineColonSeeker</b>(<i>line</i>) |
541 <b>_singleLineColonSeeker</b>(<i>line</i>) |
623 |
542 |
624 <p> |
543 <p> |
745 <dt>Return Type:</dt> |
664 <dt>Return Type:</dt> |
746 <dd> |
665 <dd> |
747 FunctionType |
666 FunctionType |
748 </dd> |
667 </dd> |
749 </dl> |
668 </dl> |
750 <a NAME="Function.tryTypeComment" ID="Function.tryTypeComment"></a> |
|
751 <h4>Function.tryTypeComment (static)</h4> |
|
752 <b>tryTypeComment</b>(<i>node</i>) |
|
753 |
|
754 <p> |
|
755 Static method to infer type hints from a function-level type comment. |
|
756 </p> |
|
757 <p> |
|
758 If a function is type commented it is assumed to have a return |
|
759 annotation, otherwise Python will fail to parse the hint. |
|
760 </p> |
|
761 <dl> |
|
762 |
|
763 <dt><i>funcObj</i> (Function)</dt> |
|
764 <dd> |
|
765 reference to the Function object |
|
766 </dd> |
|
767 <dt><i>node</i> (ast.AsyncFunctionDef or ast.FunctionDef)</dt> |
|
768 <dd> |
|
769 reference to the function definition node |
|
770 </dd> |
|
771 </dl> |
|
772 <dl> |
|
773 <dt>Return:</dt> |
|
774 <dd> |
|
775 reference to the modified Function object |
|
776 </dd> |
|
777 </dl> |
|
778 <dl> |
|
779 <dt>Return Type:</dt> |
|
780 <dd> |
|
781 Function |
|
782 </dd> |
|
783 </dl> |
|
784 <div align="right"><a href="#top">Up</a></div> |
669 <div align="right"><a href="#top">Up</a></div> |
785 <hr /> |
670 <hr /> |
786 <hr /> |
671 <hr /> |
787 <a NAME="FunctionVisitor" ID="FunctionVisitor"></a> |
672 <a NAME="FunctionVisitor" ID="FunctionVisitor"></a> |
788 <h2>FunctionVisitor</h2> |
673 <h2>FunctionVisitor</h2> |