Skip to content

Allow using __getitem__ defined in metaclasses of non-generic classes #2827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,13 @@ def has_no_attr(self, typ: Type, member: str, context: Context) -> Type:
self.format(typ)), context)
elif member == '__getitem__':
# Indexed get.
self.fail('Value of type {} is not indexable'.format(
self.format(typ)), context)
# TODO: Fix this consistently in self.format
if isinstance(typ, CallableType) and typ.is_type_obj():
self.fail('The type {} is not generic and not indexable'.format(
self.format(typ)), context)
else:
self.fail('Value of type {} is not indexable'.format(
self.format(typ)), context)
elif member == '__setitem__':
# Indexed set.
self.fail('Unsupported target for indexed assignment', context)
Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2623,7 +2623,7 @@ def visit_index_expr(self, expr: IndexExpr) -> None:
expr.base.accept(self)
if (isinstance(expr.base, RefExpr)
and isinstance(expr.base.node, TypeInfo)
and expr.base.node.is_enum):
and not expr.base.node.is_generic()):
expr.index.accept(self)
elif isinstance(expr.base, RefExpr) and expr.base.kind == TYPE_ALIAS:
# Special form -- subscripting a generic type alias.
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2883,6 +2883,14 @@ class Concrete(metaclass=Meta):
reveal_type(Concrete + X()) # E: Revealed type is 'builtins.str'
Concrete + "hello" # E: Unsupported operand types for + ("Meta" and "str")

[case testMetaclassGetitem]
class M(type):
def __getitem__(self, key) -> int: return 1

class A(metaclass=M): pass

reveal_type(A[M]) # E: Revealed type is 'builtins.int'

[case testMetaclassSelftype]
from typing import TypeVar, Type

Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ A[int, str, int]() # E: Type application has too many types (2 expected)
a = None # type: A
class A: pass
a[A]() # E: Value of type "A" is not indexable
A[A]() # E: Type application targets a non-generic function or class
A[A]() # E: The type "A" is not generic and not indexable
[out]

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

[case testTypeApplicationCrash]
type[int] # this was crashing, see #2302 (comment) # E: Type application targets a non-generic function or class
type[int] # this was crashing, see #2302 (comment) # E: The type "type" is not generic and not indexable
[out]


Expand Down
7 changes: 0 additions & 7 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,6 @@ class A: pass
x = cast(A[int], 1) # E: "A" expects no type arguments, but 1 given
[out]

[case testInvalidNumberOfGenericArgsInTypeApplication]
import typing
class A: pass
class B: pass
x = A[B[int]]() # E: "B" expects no type arguments, but 1 given
[out]

[case testInvalidNumberOfGenericArgsInNestedGenericType]
from typing import TypeVar, Generic
T = TypeVar('T')
Expand Down