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

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

merged 2 commits into from
Feb 7, 2017

Conversation

gvanrossum
Copy link
Member

Fixes #1381

@@ -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()):
Copy link
Member

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")
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.

@elazarg
Copy link
Contributor

elazarg commented Feb 6, 2017

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.

@gvanrossum
Copy link
Member Author

I actually looked into basing this on #2475, but there's a catch: because of the special-casing of X[Y] for generic types, defining __getitem__ on the metaclass doesn't work -- in fact that's the one special method for which your PR doesn't work...

It might be possible to differentiate on the basis of whether X is a generic class, but that's not currently taken into account. I haven't looked into why, but perhaps it's because we need to know how to interpret X[Y] before the type of X is known?

@JukkaL JukkaL merged commit b4492b7 into master Feb 7, 2017
@gvanrossum gvanrossum deleted the enum-index branch February 7, 2017 17:27
elazarg added a commit to elazarg/mypy that referenced this pull request Feb 8, 2017
gvanrossum pushed a commit that referenced this pull request Feb 12, 2017
…#2827)

Fix #1771. (Seems to be much simpler than I thought)

In particular, this will allow removing some special-casing for enum (#2812).

Related to #1381.
@JukkaL JukkaL mentioned this pull request Feb 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants