-
-
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
Changes from 1 commit
d07259a
34de9ae
6765a08
8dc40d6
b76dafc
aa3831e
50056c4
b2212dd
598a511
4d8cef1
f90dabf
7970238
8c35be0
91e2be4
5cc1953
7e7c1b7
efe6342
91bda84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1626,6 +1626,11 @@ def alias_fallback(self, tp: Type) -> Instance: | |
|
||
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. |
||
"""Check if 'rvalue' represents a valid type allowed for aliasing | ||
(e.g. not a type variable). If yes, return the corresponding type and a list of | ||
qualified type variable names for generic aliases. | ||
If 'allow_unnormalized' is True, allow types like builtins.list[T]. | ||
""" | ||
res = analyze_type_alias(rvalue, | ||
self.lookup_qualified, | ||
self.lookup_fully_qualified, | ||
|
@@ -1643,9 +1648,13 @@ def analyze_alias(self, rvalue: Expression, | |
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 | ||
# assignments create class valued members and local variables with type object types. | ||
"""Check if assignment creates a type alias and set it up as needed. | ||
For simple aliases like L = List we use a simpler mechanism, just copying TypeInfo. | ||
For subscripted (including generic) aliases we mark corresponding node as TYPE_ALIAS | ||
and store the corresponding type in node.override and in rvalue.analyzed. | ||
""" | ||
# Type aliases are created only at module scope and class scope (for subscripted types), | ||
# at function scope assignments always create local variables with type object types. | ||
if (len(s.lvalues) == 1 and not self.is_func_scope() and | ||
not (self.type and isinstance(s.rvalue, NameExpr) and s.lvalues[0].is_def) | ||
and not s.type): | ||
|
@@ -1668,6 +1677,8 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> None: | |
# so we need to replace it with non-explicit Anys | ||
res = make_any_non_explicit(res) | ||
if isinstance(res, Instance) and not res.args and isinstance(rvalue, RefExpr): | ||
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 describes the purpose of this special case. |
||
# For simple (on-generic) aliases we use aliasing TypeInfo's | ||
# to allow using them in runtime context where it makes sense. | ||
node.node = res.type | ||
if isinstance(rvalue, RefExpr): | ||
sym = self.lookup_type_node(rvalue) | ||
|
@@ -1678,6 +1689,8 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> None: | |
node.type_override = res | ||
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. |
||
# We only need this for subscripted aliases, since simple aliases | ||
# are already processed using aliasing TypeInfo's above. | ||
s.rvalue.analyzed = TypeAliasExpr(res, node.alias_tvars, | ||
fallback=self.alias_fallback(res)) | ||
s.rvalue.analyzed.line = s.rvalue.line | ||
|
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.
Add comment.
Are these short or full names? If short ones, it would be more correct to have full names. For example,
a.T
andb.T
could refer to different type variables -- this is very unlikely, though.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 looks like these are qualified names. I added a test for this, and a comment here.