Skip to content

Commit 7abf8f4

Browse files
committed
TypeApplication is for Generic classes only
1 parent ae47a3e commit 7abf8f4

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed

mypy/semanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2623,7 +2623,7 @@ def visit_index_expr(self, expr: IndexExpr) -> None:
26232623
expr.base.accept(self)
26242624
if (isinstance(expr.base, RefExpr)
26252625
and isinstance(expr.base.node, TypeInfo)
2626-
and expr.base.node.is_enum):
2626+
and not expr.base.node.is_generic()):
26272627
expr.index.accept(self)
26282628
elif isinstance(expr.base, RefExpr) and expr.base.kind == TYPE_ALIAS:
26292629
# Special form -- subscripting a generic type alias.

test-data/unit/check-classes.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2845,3 +2845,11 @@ class Concrete(metaclass=Meta):
28452845

28462846
reveal_type(Concrete + X()) # E: Revealed type is 'builtins.str'
28472847
Concrete + "hello" # E: Unsupported operand types for + ("Meta" and "str")
2848+
2849+
[case testMetaclassGetitem]
2850+
class M(type):
2851+
def __getitem__(self, key) -> int: return 1
2852+
2853+
class A(metaclass=M): pass
2854+
2855+
reveal_type(A[M]) # E: Revealed type is 'builtins.int'

test-data/unit/check-generics.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ A[int, str, int]() # E: Type application has too many types (2 expected)
449449
a = None # type: A
450450
class A: pass
451451
a[A]() # E: Value of type "A" is not indexable
452-
A[A]() # E: Type application targets a non-generic function or class
452+
A[A]() # E: Value of type "A" is not indexable
453453
[out]
454454

455455
[case testTypeApplicationArgTypes]
@@ -505,7 +505,7 @@ Alias[int]("a") # E: Argument 1 to "Node" has incompatible type "str"; expected
505505
[out]
506506

507507
[case testTypeApplicationCrash]
508-
type[int] # this was crashing, see #2302 (comment) # E: Type application targets a non-generic function or class
508+
type[int] # this was crashing, see #2302 (comment) # E: Value of type "type" is not indexable
509509
[out]
510510

511511

test-data/unit/semanal-errors.test

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ class A: pass
103103
x = cast(A[int], 1) # E: "A" expects no type arguments, but 1 given
104104
[out]
105105

106-
[case testInvalidNumberOfGenericArgsInTypeApplication]
107-
import typing
108-
class A: pass
109-
class B: pass
110-
x = A[B[int]]() # E: "B" expects no type arguments, but 1 given
111-
[out]
112-
113106
[case testInvalidNumberOfGenericArgsInNestedGenericType]
114107
from typing import TypeVar, Generic
115108
T = TypeVar('T')

0 commit comments

Comments
 (0)