Skip to content

Commit 0b09116

Browse files
authored
Indexing a type also produces a GenericAlias (#17546)
Mentioned by Jelle in #17543 (comment)
1 parent 0a040dd commit 0b09116

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

mypy/checkexpr.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4341,14 +4341,11 @@ 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 and self.chk.options.python_version >= (3, 9):
4345-
return self.named_type("types.GenericAlias")
4346-
elif (
4347-
left_type.type_object().fullname == "builtins.type"
4348-
and self.chk.options.python_version >= (3, 9)
4344+
elif self.chk.options.python_version >= (3, 9) and (
4345+
left_type.type_object().type_vars
4346+
or left_type.type_object().fullname == "builtins.type"
43494347
):
4350-
# builtins.type is special: it's not generic in stubs, but it supports indexing
4351-
return self.named_type("typing._SpecialForm")
4348+
return self.named_type("types.GenericAlias")
43524349

43534350
if isinstance(left_type, TypeVarType) and not self.has_member(
43544351
left_type.upper_bound, "__getitem__"

test-data/unit/pythoneval.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,7 @@ C = str | int
17811781
D: TypeAlias = str | int
17821782
[out]
17831783
_testTypeAliasNotSupportedWithNewStyleUnion.py:3: error: Invalid type alias: expression is not a valid type
1784-
_testTypeAliasNotSupportedWithNewStyleUnion.py:3: error: Unsupported left operand type for | ("<typing special form>")
1784+
_testTypeAliasNotSupportedWithNewStyleUnion.py:3: error: Unsupported left operand type for | ("GenericAlias")
17851785
_testTypeAliasNotSupportedWithNewStyleUnion.py:4: error: Invalid type alias: expression is not a valid type
17861786
_testTypeAliasNotSupportedWithNewStyleUnion.py:4: error: Unsupported left operand type for | ("Type[str]")
17871787
_testTypeAliasNotSupportedWithNewStyleUnion.py:5: error: Invalid type alias: expression is not a valid type

0 commit comments

Comments
 (0)