Skip to content

Allow E[<str>] where E is an Enum type #2812

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 2 commits into from
Feb 7, 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
13 changes: 13 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,9 @@ def visit_index_expr_helper(self, e: IndexExpr) -> Type:
return AnyType()
elif isinstance(left_type, TypedDictType):
return self.visit_typeddict_index_expr(left_type, e.index)
elif (isinstance(left_type, CallableType)
and left_type.is_type_obj() and left_type.type_object().is_enum):
return self.visit_enum_index_expr(left_type.type_object(), e.index, e)
else:
result, method_type = self.check_op('__getitem__', left_type, e.index, e)
e.method_type = method_type
Expand Down Expand Up @@ -1497,6 +1500,16 @@ def visit_typeddict_index_expr(self, td_type: TypedDictType, index: Expression)
return AnyType()
return item_type

def visit_enum_index_expr(self, enum_type: TypeInfo, index: Expression,
context: Context) -> Type:
string_type = self.named_type('builtins.str') # type: Type
if self.chk.options.python_version[0] < 3:
string_type = UnionType.make_union([string_type,
self.named_type('builtins.unicode')])
self.chk.check_subtype(self.accept(index), string_type, context,
"Enum index should be a string", "actual index type")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to have a test that triggers this error.

return Instance(enum_type, [])

def visit_cast_expr(self, expr: CastExpr) -> Type:
"""Type check a cast expression."""
source_type = self.accept(expr.expr, context=AnyType())
Expand Down
6 changes: 5 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2455,7 +2455,11 @@ def visit_unary_expr(self, expr: UnaryExpr) -> None:

def visit_index_expr(self, expr: IndexExpr) -> None:
expr.base.accept(self)
if isinstance(expr.base, RefExpr) and expr.base.kind == TYPE_ALIAS:
if (isinstance(expr.base, RefExpr)
and isinstance(expr.base.node, TypeInfo)
and expr.base.node.is_enum):
expr.index.accept(self)
elif isinstance(expr.base, RefExpr) and expr.base.kind == TYPE_ALIAS:
# Special form -- subscripting a generic type alias.
# Perform the type substitution and create a new alias.
res = analyze_type_alias(expr,
Expand Down
27 changes: 26 additions & 1 deletion test-data/unit/pythoneval-enum.test
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ def takes_some_ext_int_enum(s: SomeExtIntEnum):
pass
takes_some_ext_int_enum(SomeExtIntEnum.x)


[case testNamedTupleEnum]
from typing import NamedTuple
from enum import Enum
Expand All @@ -132,3 +131,29 @@ class E(N, Enum):
def f(x: E) -> None: pass

f(E.X)

[case testEnumCall]
from enum import IntEnum
class E(IntEnum):
a = 1
x = None # type: int
reveal_type(E(x))
[out]
_program.py:5: error: Revealed type is '_testEnumCall.E'

[case testEnumIndex]
from enum import IntEnum
class E(IntEnum):
a = 1
s = None # type: str
reveal_type(E[s])
[out]
_program.py:5: error: Revealed type is '_testEnumIndex.E'

[case testEnumIndexError]
from enum import IntEnum
class E(IntEnum):
a = 1
E[1]
[out]
_program.py:4: error: Enum index should be a string (actual index type "int")