8000 Simplify and tighten type aliases by ilevkivskyi · Pull Request #3524 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

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 18 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Start reworking type aliases
  • Loading branch information
ilevkivskyi committed Jun 10, 2017
commit d07259ade278dcf0f0e2f4c6bdcef21a3e4c1187
52 changes: 24 additions & 28 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,26 +1517,11 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
allow_tuple_literal = isinstance(s.lvalues[-1], (TupleExpr, ListExpr))
s.type = self.anal_type(s.type, allow_tuple_literal=allow_tuple_literal)
else:
# For simple assignments, allow binding type aliases.
# Also set the type if the rvalue is a simple literal.
# Set the type if the rvalue is a simple literal.
if (s.type is None and len(s.lvalues) == 1 and
isinstance(s.lvalues[0], NameExpr)):
if s.lvalues[0].is_def:
s.type = self.analyze_simple_literal_type(s.rvalue)
res = analyze_type_alias(s.rvalue,
self.lookup_qualified,
self.lookup_fully_qualified,
self.tvar_scope,
self.fail, allow_unnormalized=True)
if res and (not isinstance(res, Instance) or res.args):
# TODO: What if this gets reassigned?
name = s.lvalues[0]
node = self.lookup(name.name, name)
node.kind = TYPE_ALIAS
node.type_override = res
if isinstance(s.rvalue, IndexExpr):
s.rvalue.analyzed = TypeAliasExpr(res,
fallback=self.alias_fallback(res))
if s.type:
# Store type into nodes.
for lvalue in s.lvalues:
Expand Down Expand Up @@ -1592,24 +1577,35 @@ def alias_fallback(self, tp: Type) -> Instance:

def check_and_set_up_type_alias(self, s: AssignmentStmt) -> None:
"""Check if assignment creates a type alias and set it up as needed."""
# For now, type aliases only work at the top level of a module.
if (len(s.lvalues) == 1 and not self.is_func_scope() and not self.type
and not s.type):
# For now, type aliases are not created at class scope,
# instead they are treated as class valued members.
if len(s.lvalues) == 1 and not s.type and (not self.type or self.is_func_scope()):
lvalue = s.lvalues[0]
if isinstance(lvalue, NameExpr):
if not lvalue.is_def:
# Only a definition can create a type alias, not regular assignment.
return
rvalue = s.rvalue
if isinstance(rvalue, RefExpr):
node = rvalue.node
if isinstance(node, TypeInfo):
# TODO: We should record the fact that this is a variable
# that refers to a type, rather than making this
# just an alias for the type.
sym = self.lookup_type_node(rvalue)
if sym:
self.globals[lvalue.name] = sym
res = analyze_type_alias(rvalue,
self.lookup_qualified,
self.lookup_fully_qualified,
self.tvar_scope,
self.fail, allow_unnormalized=True)
if res:
# TODO: What if this gets reassigned?
node = self.lookup(lvalue.name, lvalue)
if isinstance(res, Instance) and not res.args:
node.node = res.type
if isinstance(rvalue, RefExpr):
sym = self.lookup_type_node(rvalue)
if sym:
node.normalized = sym.normalized
return
node.kind = TYPE_ALIAS
node.type_override = res
if isinstance(s.rvalue, IndexExpr):
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
fallback=self.alias_fallback(res))

def analyze_lvalue(self, lval: Lvalue, nested: bool = False,
add_global: bool = False,
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class B: pass
import typing
def f() -> None:
a = A
a(A()) # E: Too many arguments
a(A()) # E: Too many arguments for "A"
a()
t = a # type: type

Expand Down
0