Skip to content

Commit 46b6e6d

Browse files
authored
Merge pull request #2409 from tausbn/python-typing-forward-reference-fp
Python: Support forward references inside return type annotations.
2 parents 536c211 + 033524c commit 46b6e6d

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

python/ql/src/Imports/UnusedImport.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ private string doctest_in_scope(Scope scope) {
6464
pragma[noinline]
6565
private string typehint_annotation_in_file(File file) {
6666
exists(StrConst annotation |
67-
annotation = any(Arguments a).getAnAnnotation()
67+
annotation = any(Arguments a).getAnAnnotation().getASubExpression*()
6868
or
69-
annotation = any(AnnAssign a).getAnnotation()
69+
annotation = any(AnnAssign a).getAnnotation().getASubExpression*()
70+
or
71+
annotation = any(FunctionExpr f).getReturns().getASubExpression*()
7072
|
7173
annotation.pointsTo(Value::forString(result)) and
7274
file = annotation.getLocation().getFile()

python/ql/test/query-tests/Imports/unused/imports_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,24 @@ def func(x: 'Optional[only_used_in_parameter_annotation]'):
9090
import only_used_in_annotated_assignment
9191

9292
var : 'Optional[only_used_in_annotated_assignment]' = 5
93+
94+
import used_in_return_type
95+
96+
def look_at_my_return_type() -> 'Optional[used_in_return_type]':
97+
pass
98+
99+
# Uses inside strings appearing as subexpressions of an annotation:
100+
101+
import subexpression_parameter_annotation
102+
103+
def bar(x: Optional['subexpression_parameter_annotation']):
104+
pass
105+
106+
import subexpression_assignment_annotation
107+
108+
var3 : Optional['subexpression_assignment_annotation'] = None
109+
110+
import subexpression_return_type
111+
112+
def baz() -> Optional['subexpression_return_type']:
113+
pass

0 commit comments

Comments
 (0)