-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Conversation
mypy/checkexpr.py
Outdated
@@ -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()): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this already works correctly, but I think it would be safer to check for left_type.type_object().is_enum
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") |
There was a problem hiding this comment.
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.
Perhaps I am confusing things, but I think this can (and probably should?) be fixed after #2475 is merged, by augmenting EnumMeta and without further special casing. |
I actually looked into basing this on #2475, but there's a catch: because of the special-casing of It might be possible to differentiate on the basis of whether |
670a34c
to
d829376
Compare
Fixes #1381