Skip to content

Commit 8e2ce96

Browse files
authored
Fix annotated with function as type keyword list parameter (#20094)
Fixes #20090
1 parent 17f8b31 commit 8e2ce96

File tree

3 files changed

+87
-12
lines changed

3 files changed

+87
-12
lines changed

mypy/exprtotype.py

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ def expr_to_unanalyzed_type(
100100
else:
101101
raise TypeTranslationError()
102102
elif isinstance(expr, IndexExpr):
103-
base = expr_to_unanalyzed_type(expr.base, options, allow_new_syntax, expr)
103+
base = expr_to_unanalyzed_type(
104+
expr.base, options, allow_new_syntax, expr, lookup_qualified=lookup_qualified
105+
)
104106
if isinstance(base, UnboundType):
105107
if base.args:
106108
raise TypeTranslationError()
@@ -124,9 +126,18 @@ def expr_to_unanalyzed_type(
124126
# TODO: this is not the optimal solution as we are basically getting rid
125127
# of the Annotation definition and only returning the type information,
126128
# losing all the annotations.
127-
return expr_to_unanalyzed_type(args[0], options, allow_new_syntax, expr)
129+
return expr_to_unanalyzed_type(
130+
args[0], options, allow_new_syntax, expr, lookup_qualified=lookup_qualified
131+
)
128132
base.args = tuple(
129-
expr_to_unanalyzed_type(arg, options, allow_new_syntax, expr, allow_unpack=True)
133+
expr_to_unanalyzed_type(
134+
arg,
135+
options,
136+
allow_new_syntax,
137+
expr,
138+
allow_unpack=True,
139+
lookup_qualified=lookup_qualified,
140+
)
130141
for arg in args
131142
)
132143
if not base.args:
@@ -141,8 +152,12 @@ def expr_to_unanalyzed_type(
141152
):
142153
return UnionType(
143154
[
144-
expr_to_unanalyzed_type(expr.left, options, allow_new_syntax),
145-
expr_to_unanalyzed_type(expr.right, options, allow_new_syntax),
155+
expr_to_unanalyzed_type(
156+
expr.left, options, allow_new_syntax, lookup_qualified=lookup_qualified
157+
),
158+
expr_to_unanalyzed_type(
159+
expr.right, options, allow_new_syntax, lookup_qualified=lookup_qualified
160+
),
146161
],
147162
uses_pep604_syntax=True,
148163
)
@@ -178,12 +193,16 @@ def expr_to_unanalyzed_type(
178193
if typ is not default_type:
179194
# Two types
180195
raise TypeTranslationError()
181-
typ = expr_to_unanalyzed_type(arg, options, allow_new_syntax, expr)
196+
typ = expr_to_unanalyzed_type(
197+
arg, options, allow_new_syntax, expr, lookup_qualified=lookup_qualified
198+
)
182199
continue
183200
else:
184201
raise TypeTranslationError()
185202
elif i == 0:
186-
typ = expr_to_unanalyzed_type(arg, options, allow_new_syntax, expr)
203+
typ = expr_to_unanalyzed_type(
204+
arg, options, allow_new_syntax, expr, lookup_qualified=lookup_qualified
205+
)
187206
elif i == 1:
188207
name = _extract_argument_name(arg)
189208
else:
@@ -192,7 +211,14 @@ def expr_to_unanalyzed_type(
192211
elif isinstance(expr, ListExpr):
193212
return TypeList(
194213
[
195-
expr_to_unanalyzed_type(t, options, allow_new_syntax, expr, allow_unpack=True)
214+
expr_to_unanalyzed_type(
215+
t,
216+
options,
217+
allow_new_syntax,
218+
expr,
219+
allow_unpack=True,
220+
lookup_qualified=lookup_qualified,
221+
)
196222
for t in expr.items
197223
],
198224
line=expr.line,
@@ -203,7 +229,9 @@ def expr_to_unanalyzed_type(
203229
elif isinstance(expr, BytesExpr):
204230
return parse_type_string(expr.value, "builtins.bytes", expr.line, expr.column)
205231
elif isinstance(expr, UnaryExpr):
206-
typ = expr_to_unanalyzed_type(expr.expr, options, allow_new_syntax)
232+
typ = expr_to_unanalyzed_type(
233+
expr.expr, options, allow_new_syntax, lookup_qualified=lookup_qualified
234+
)
207235
if isinstance(typ, RawExpressionType):
208236
if isinstance(typ.literal_value, int):
209237
if expr.op == "-":
@@ -225,7 +253,10 @@ def expr_to_unanalyzed_type(
225253
return EllipsisType(expr.line)
226254
elif allow_unpack and isinstance(expr, StarExpr):
227255
return UnpackType(
228-
expr_to_unanalyzed_type(expr.expr, options, allow_new_syntax), from_star_syntax=True
256+
expr_to_unanalyzed_type(
257+
expr.expr, options, allow_new_syntax, lookup_qualified=lookup_qualified
258+
),
259+
from_star_syntax=True,
229260
)
230261
elif isinstance(expr, DictExpr):
231262
if not expr.items:
@@ -236,12 +267,18 @@ def expr_to_unanalyzed_type(
236267
if not isinstance(item_name, StrExpr):
237268
if item_name is None:
238269
extra_items_from.append(
239-
expr_to_unanalyzed_type(value, options, allow_new_syntax, expr)
270+
expr_to_unanalyzed_type(
271+
value,
272+
options,
273+
allow_new_syntax,
274+
expr,
275+
lookup_qualified=lookup_qualified,
276+
)
240277
)
241278
continue
242279
raise TypeTranslationError()
243280
items[item_name.value] = expr_to_unanalyzed_type(
244-
value, options, allow_new_syntax, expr
281+
value, options, allow_new_syntax, expr, lookup_qualified=lookup_qualified
245282
)
246283
result = TypedDictType(
247284
items, set(), set(), Instance(MISSING_FALLBACK, ()), expr.line, expr.column

test-data/unit/check-python312.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,25 @@ reveal_type(x[0]) # N: Revealed type is "Any"
21702170
[builtins fixtures/tuple.pyi]
21712171
[typing fixtures/typing-full.pyi]
21722172

2173+
[case testAnnotatedWithCallableAsParameterTypeKeyword]
2174+
from typing_extensions import Annotated
2175+
2176+
def something() -> None: ...
2177+
2178+
type A = list[Annotated[str, something()]]
2179+
a: A
2180+
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
2181+
[builtins fixtures/tuple.pyi]
2182+
2183+
[case testAnnotatedWithCallableAsParameterTypeKeywordDeeper]
2184+
from typing_extensions import Annotated
2185+
2186+
def something() -> None: ...
2187+
2188+
type A = list[Annotated[Annotated[str, something()], something()]]
2189+
a: A
2190+
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
2191+
[builtins fixtures/tuple.pyi]
21732192
[case testPEP695TypeAliasRecursiveInParameterBound]
21742193
from typing import Any
21752194

test-data/unit/check-type-aliases.test

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,25 @@ from typing_extensions import TypeAlias
13191319
Foo: TypeAlias = ClassVar[int] # E: ClassVar[...] can't be used inside a type alias
13201320
[builtins fixtures/tuple.pyi]
13211321

1322+
[case testAnnotatedWithCallableAsParameterTypeAlias]
1323+
from typing_extensions import Annotated, TypeAlias
1324+
1325+
def something() -> None: ...
1326+
1327+
A: TypeAlias = list[Annotated[str, something()]]
1328+
a: A
1329+
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
1330+
[builtins fixtures/tuple.pyi]
1331+
1332+
[case testAnnotatedWithCallableAsParameterTypeAliasDeeper]
1333+
from typing_extensions import Annotated, TypeAlias
1334+
1335+
def something() -> None: ...
1336+
1337+
A: TypeAlias = list[Annotated[Annotated[str, something()], something()]]
1338+
a: A
1339+
reveal_type(a) # N: Revealed type is "builtins.list[builtins.str]"
1340+
[builtins fixtures/tuple.pyi]
13221341

13231342
[case testTypeAliasDict]
13241343
D = dict[str, int]

0 commit comments

Comments
 (0)