Skip to content

Commit 6cf9180

Browse files
committed
Fix types.GenericAlias lookup crash (#17543)
Fixes #17542
1 parent 64c1ebf commit 6cf9180

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

mypy/checkexpr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4341,7 +4341,7 @@ def visit_index_with_type(
43414341
elif isinstance(left_type, FunctionLike) and left_type.is_type_obj():
43424342
if left_type.type_object().is_enum:
43434343
return self.visit_enum_index_expr(left_type.type_object(), e.index, e)
4344-
elif left_type.type_object().type_vars:
4344+
elif left_type.type_object().type_vars and self.chk.options.python_version >= (3, 9):
43454345
return self.named_type("types.GenericAlias")
43464346
elif (
43474347
left_type.type_object().fullname == "builtins.type"

test-data/unit/check-functions.test

+17-3
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,7 @@ def Arg(x, y): pass
17791779
F = Callable[[Arg(int, 'x')], int] # E: Invalid argument constructor "__main__.Arg"
17801780

17811781
[case testCallableParsingFromExpr]
1782+
# flags: --python-version 3.9
17821783
from typing import Callable, List
17831784
from mypy_extensions import Arg, VarArg, KwArg
17841785
import mypy_extensions
@@ -1799,10 +1800,23 @@ L = Callable[[Arg(name='x', type=int)], int] # ok
17991800
# I have commented out the following test because I don't know how to expect the "defined here" note part of the error.
18001801
# M = Callable[[Arg(gnome='x', type=int)], int] E: Invalid type alias: expression is not a valid type E: Unexpected keyword argument "gnome" for "Arg"
18011802
N = Callable[[Arg(name=None, type=int)], int] # ok
1802-
O = Callable[[List[Arg(int)]], int] # E: Invalid type alias: expression is not a valid type # E: Value of type "int" is not indexable # E: Type expected within [...]
1803+
O = Callable[[List[Arg(int)]], int] # E: Invalid type alias: expression is not a valid type \
1804+
# E: Value of type "int" is not indexable \
1805+
# E: Type expected within [...]
18031806
P = Callable[[mypy_extensions.VarArg(int)], int] # ok
1804-
Q = Callable[[Arg(int, type=int)], int] # E: Invalid type alias: expression is not a valid type # E: Value of type "int" is not indexable # E: "Arg" gets multiple values for keyword argument "type"
1805-
R = Callable[[Arg(int, 'x', name='y')], int] # E: Invalid type alias: expression is not a valid type # E: Value of type "int" is not indexable # E: "Arg" gets multiple values for keyword argument "name"
1807+
Q = Callable[[Arg(int, type=int)], int] # E: Invalid type alias: expression is not a valid type \
1808+
# E: Value of type "int" is not indexable \
1809+
# E: "Arg" gets multiple values for keyword argument "type"
1810+
R = Callable[[Arg(int, 'x', name='y')], int] # E: Invalid type alias: expression is not a valid type \
1811+
# E: Value of type "int" is not indexable \
1812+
# E: "Arg" gets multiple values for keyword argument "name"
1813+
1814+
1815+
1816+
1817+
1818+
1819+
18061820
[builtins fixtures/dict.pyi]
18071821

18081822
[case testCallableParsing]

0 commit comments

Comments
 (0)