-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 basic support for enum literals #6668
Merged
Michael0x2a
merged 4 commits into
python:master
from
Michael0x2a:add-basic-support-for-enum-literals
Apr 12, 2019
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 there is some misunderstanding/bug somewhere. This function should never be used for things like local lookup. It should be only used to construct a known type like
builtins.str
. For local lookup one needs to uselookup_qualified()
. Why do you need this modification?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 made this change in order to get this example working:
Basically, the literal-handling code in
typeanal
receives back the string "module.Outer.Inner". We then calltypeanal.named_type_with_normalized_str
which in turn callstypeanal.named_type
which in turn callssemanal.lookup_fully_qualified
. That function then ends up crashing because "Outer" is a TypeInfo, not a MypyFile.FWIW I was under the impression that:
lookup_qualified
is for looking up fully-qualified types, and has a bunch of error handling for when the thing we're trying to look up doesn't exist.lookup_fully_qualified
is a simpler version oflookup_qualified
, except without error handling.lookup
is for looking up some unqualified name across all namespaces (local and global).I also noticed that
lookup_qualified
handles both MypyFile and TypeInfo, and so figured that the fact thatlookup_fully_qualified
doesn't was an oversight.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.
This is a wrong impression. Which is normal, we have more than dozen lookup functions in the code-base (likely caused by the fact that many people who are not familiar with mypy contributed some reinvented bicycles, which is again normal). Largely, they can be separated in two classes:
The
named_type()
and friends are from the second group. While the first group is roughly two methodslookup()
andlookup_qualified()
.Essentially what is going on here looks like a hack (and TBH I don't like it). You already have the
TypeInfo
needed to construct the literal type, but instead you get its full name and pass it toRawExpressionType
that later passes the name to find the sameTypeInfo
again.Initially, string name was passed to
RawLiteralExpression
because it is was constructed at very early stage. Here however, it looks unnecessary. Why can't we just immediately returnLiteralType
there with a fallback instance constructed from a knownTypeInfo
?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.
Hmm, ok. In that case, what's the correct way of looking up a nested type via a global lookup?
It seems all of the "global lookup" functions assume that all types are defined within modules only, which I'm not so sure is correct.
Sure, we can do that. Is it safe to just construct new whenever we want Instances though? E.g. there isn't a need to register them in some global registry?
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 is not correct, but this is kind of optimization. After the refactoring we will have two methods, one fast for builtin things like
builtins.str
, and another one that can be used in plugins etc., with full semantics.What kind of registry do you mean? I think one can just call
Instance(info, args, line, column)
or am I missing something?.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.
Honestly, I have no idea. Instances and TypeInfos are stuffed with so much functionality that I always get a little worried that touching either of them or creating new ones will break something or interfere with something related to incremental or fine-grained mode in some weird way.