-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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 end-to-end support for Literal types #5947
Merged
Michael0x2a
merged 11 commits into
python:master
from
Michael0x2a:add-preliminary-literal-types-semantic-logic
Dec 3, 2018
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0147a76
Add basic end-to-end support for Literal types
Michael0x2a 4ff4294
Correct error strings to what they *should* be
Michael0x2a 4b62235
Remove lingering NewType import
Michael0x2a 5d436dc
Add test case arg that skips path normalization
Michael0x2a 75891b9
Fix lint error
Michael0x2a 9271810
Add a test case for when 'Literal' is imported with a different name
Michael0x2a c206a1b
Respond to feedback regarding tests
Michael0x2a 293d835
Merge branch 'master' into add-preliminary-literal-types-semantic-logic
Michael0x2a fc34c6a
Hoisted by my own petard
Michael0x2a 6aca386
Respond to next wave of code review
Michael0x2a 73e058a
Add an extra error check
Michael0x2a 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
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
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.
Two large scale questions:
RawLiteral
?Type
?It is OK to still put it in
types.py
, but wouldn't it be simpler to updateUnboundType.args
to beList[Union[Type, RawLiteral]]
? I understand this may need some work, but currently we must implement several visitors and this type can sneak into type checking phase.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 can look into doing this, but I believe we currently reference UnboundType.args in approximately 60 difference places throughout our codebase. I'm worried that propagating this change won't be cheap: we'd likely end up having to change a bunch of type signatures so we can pass through
Union[Type, RawLiteral]
and/or add a bunch of runtime checks to filter outRawLiteral
.(We'd also need to modify both
fastparse.TypeConverter
andexprtotype
so they return aUnion[Type, RawLiteral]
, but I think this is a more tractable problem: we could just implement some sort of wrapper around them that reports an error and returnsAnyType
if the top-level type isRawLiteral
)If we're worried about RawLiterals and similar types sneaking through, I'd rather just add another layer to semantic analysis that does a final scan of all types to make sure that RawLiteral, UnboundType, and any other "bad" types are fully removed. That way, would need to scan and check each type just once, instead of in potentially multiple places whenever we manipulate
UnboundType.args
.Or even better, we could add a new base type named
TypeLike
thatType
,UnboundType
, andRawLiteral
all inherit from (along with any other fake types we don't want sneaking into the typechecking phase).UnboundType
would then be modified so that its args contain instances ofTypeLike
rather thenType
.The semantic analysis layer would always work with
TypeLike
, and we'd modifyTypeAnalyzer
so it traverses overTypeLike
and only ever returnsType
-- this would probably let us skip the extra traversal while still giving us the guarantees we want in a typesafe way.The last solution would probably be just as much work, if not more, as changing
UnboundType.args
to be of typeUnion[Type, RawLiteral]
, but I think it would be more principled and help us eliminate the root problem in a more fundamental way. (I'd also be willing to tackle making this change if you + the others think it's a good idea.)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 would recommend just trying this, if it will be soon obvious that it is too hard, then don't spend time on this. The current approach is also OK.
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.
FWIW, changing the type of
UnboundType.args
seems a bit ad hoc to me. We already have someType
subclasses such asTypeList
that are only meaningful during semantic analysis, and I think that adding another one doesn't make things significantly worse if we are careful. Most visitors will crash if they encounter the new type subclass, so issues will likely be found soon.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.
Fair point.