-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Simplify and tighten type aliases #3524
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
Changes from 5 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d07259a
Start reworking type aliases
ilevkivskyi 34de9ae
Fix another alias issue
ilevkivskyi 6765a08
Add some tests
ilevkivskyi 8dc40d6
Prohibit reassigning aliases
ilevkivskyi b76dafc
Merge upstream
ilevkivskyi aa3831e
Merge remote-tracking branch 'upstream/master' into rework-type-aliases
ilevkivskyi 50056c4
Factor out common code, fix runtime aliases and deserialization; add …
ilevkivskyi b2212dd
Fix typo
ilevkivskyi 598a511
Don't create aliases at function scope, create variables instead
4d8cef1
Merge branch 'rework-type-aliases' of https://github.com/ilevkivskyi/…
f90dabf
Fix broken merge
7970238
Merge branch 'master' into rework-type-aliases
ilevkivskyi 8c35be0
Fix strict-optional self-test :-)
91e2be4
Merge branch 'master' into rework-type-aliases
ilevkivskyi 5cc1953
First part of review comments
ilevkivskyi 7e7c1b7
Second part of review comments
ilevkivskyi efe6342
Minor fix; reduce nesting in check_and_set_up_type_alias
ilevkivskyi 91bda84
Better type on error; shorten docstring; minor refactoring
ilevkivskyi 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
<
8000
/div>
View file
Open in desktop
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1598,6 +1598,20 @@ def alias_fallback(self, tp: Type) -> Instance: | |
fb_info.mro = [fb_info, self.object_type().type] | ||
return Instance(fb_info, []) | ||
|
||
def analyze_alias(self, rvalue: Expression, | ||
allow_unnormalized: bool) -> Tuple[Optional[Type], List[str]]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add docstring. Explain arguments and the return value. |
||
res = analyze_type_alias(rvalue, | ||
self.lookup_qualified, | ||
self.lookup_fully_qualified, | ||
self.tvar_scope, | ||
self.fail, self.plugin, allow_unnormalized=True) | ||
if res: | ||
alias_tvars = [name for (name, _) in | ||
res.accept(TypeVariableQuery(self.lookup_qualified, self.tvar_scope))] | ||
else: | ||
alias_tvars = [] | ||
return res, alias_tvars | ||
|
||
def check_and_set_up_type_alias(self, s: AssignmentStmt) -> None: | ||
"""Check if assignment creates a type alias and set it up as needed.""" | ||
# Type aliases are created only at module scope, at class and function scopes | ||
|
@@ -1607,11 +1621,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> None: | |
lvalue = s.lvalues[0] | ||
if isinstance(lvalue, NameExpr): | ||
rvalue = s.rvalue | ||
res = analyze_type_alias(rvalue, | ||
self.lookup_qualified, | ||
self.lookup_fully_qualified, | ||
self.tvar_scope, | ||
self.fail, allow_unnormalized=True) | ||
res, alias_tvars = self.analyze_alias(rvalue, allow_unnormalized=True) | ||
if res: | ||
node = self.lookup(lvalue.name, lvalue) | ||
if not lvalue.is_def: | ||
|
@@ -1630,12 +1640,12 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> None: | |
return | ||
node.kind = TYPE_ALIAS | ||
node.type_override = res | ||
node.alias_tvars = [name for (name, _) in | ||
res.accept(TypeVariableQuery(self.lookup_qualified, | ||
self.tvar_scope))] | ||
node.alias_tvars = alias_tvars | ||
if isinstance(s.rvalue, IndexExpr): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment that explains the motivation of this if statement. |
||
s.rvalue.analyzed = TypeAliasExpr(res, | ||
s.rvalue.analyzed = TypeAliasExpr(res, node.alias_tvars, | ||
fallback=self.alias_fallback(res)) | ||
s.rvalue.analyzed.line = s.rvalue.line | ||
s.rvalue.analyzed.column = s.rvalue.column | ||
|
||
def analyze_lvalue(self, lval: Lvalue, nested: bool = False, | ||
add_global: bool = False, | ||
|
@@ -3125,15 +3135,11 @@ def visit_index_expr(self, expr: IndexExpr) -> None: | |
elif isinstance(expr.base, RefExpr) and expr.base.kind == TYPE_ALIAS: | ||
# Special form -- subscripting a generic type alias. | ||
# Perform the type substitution and create a new alias. | ||
res = analyze_type_alias(expr, | ||
self.lookup_qualified, | ||
self.lookup_fully_qualified, | ||
self.tvar_scope, | ||
self.fail, | ||
self.plugin, | ||
allow_unnormalized=self.is_stub_file) | ||
expr.analyzed = TypeAliasExpr(res, fallback=self.alias_fallback(res), | ||
res, alias_tvars = self.analyze_alias(expr, allow_unnormalized=self.is_stub_file) | ||
expr.analyzed = TypeAliasExpr(res, alias_tvars, fallback=self.alias_fallback(res), | ||
in_runtime=True) | ||
expr.analyzed.line = expr.line | ||
expr.analyzed.column = expr.column | ||
elif refers_to_class_or_function(expr.base): | ||
# Special form -- type application. | ||
# Translate index to an unanalyzed type. | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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
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 don't understand this -- add a comment. Is there a test case that tests this change?
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.
Now I am also not sure it is needed. It was an attempt to fix #3355, but now I have a better idea. I removed this for now, and will make a separate PR later.