Skip to content
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

Add intelligent indexing of tuples, NamedTuples, and TypedDict #6124

Merged

Conversation

Michael0x2a
Copy link
Collaborator

This pull request adds a preliminary implementation of intelligent indexing of tuples, NamedTuples, and TypedDicts. It uses the first approach we discussed earlier: modifying the existing plugins and special-casing code to also check if the expression has a Literal[...] type.

Once I'm finished with the baseline literal types implementation, I'll look into circling back and seeing how viable the second approach is (writing some sort of plugin that replaces the signatures of methods like .__getitem__ or .get() with overloads that use the appropriate literal types).

This pull request adds a preliminary implementation of intelligent indexing of
tuples, NamedTuples, and TypedDicts. It uses the first approach we
discussed earlier: modifying the existing plugins and special-casing
code to also check if the expression has a Literal[...] type.

Once I'm finished with the baseline literal types implementation, I'll
look into circling back and seeing how viable the second approach is
(writing some sort of plugin that replaces the signatures of methods
like `.__getitem__` or `.get()` with overloads that use the appropriate
literal types).
Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

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

Looks good to me! The logic is sort of notionally more complicated but the code flow is arguably cleaner so it seems like a positive anyways.

item_name = typ.value
else:
self.msg.typeddict_key_must_be_string_literal(td_type, index)
return AnyType(TypeOfAny.from_error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe not really a question for this diff, but is there a reason for this to not live in the typeddict plugin as a __getitem__ hook?

There is maybe a more relevant question of: could this just use try_getting_str_literal, the answer to which would be more obvious if this lived in the plugin.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree -- I think we should move both this and tuple.__getitem__ out into a plugin, but it felt like it'd be out-of-scope for this PR.

test-data/unit/check-literal.test Outdated Show resolved Hide resolved
@Michael0x2a Michael0x2a merged commit 3ce6b74 into python:master Jan 4, 2019
@Michael0x2a Michael0x2a deleted the add-intelligent-indexing-part-1 branch January 4, 2019 05:32
@Michael0x2a Michael0x2a mentioned this pull request Jan 4, 2019
42 tasks
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.

2 participants