8000 Explicit types for bindables ("Tighten Types" continued) by elazarg · Pull Request #2238 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Explicit types for bindables ("Tighten Types" continued) #2238

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 19 commits into from
Mar 27, 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
Prev Previous commit
Next Next commit
make get_declaration a function
  • Loading branch information
elazarg committed Nov 9, 2016
commit 208c16d162f4183236107a3e1c1b2bac18b5fa0d
21 changes: 11 additions & 10 deletions mypy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def put(self, expr: Expression, typ: Type) -> None:
return
key = expr.literal_hash
if key not in self.declarations:
self.declarations[key] = self.get_declaration(expr)
self.declarations[key] = get_declaration(expr)
self._add_dependencies(key)
self._put(key, typ)

Expand Down Expand Up @@ -197,13 +197,6 @@ def pop_frame(self, can_skip: bool, fall_through: int) -> Frame:

return result

def get_declaration(self, expr: BindableExpression) -> Type:
if isinstance(expr, RefExpr) and isinstance(expr.node, Var):
type = expr.node.type
if not isinstance(type, PartialType):
return type
return None

def assign_type(self, expr: Expression,
type: Type,
declared_type: Type,
Expand Down Expand Up @@ -256,9 +249,9 @@ def invalidate_dependencies(self, expr: BindableExpression) -> None:

def most_recent_enclosing_type(self, expr: BindableExpression, type: Type) -> Type:
if isinstance(type, AnyType):
return self.get_declaration(expr)
return get_declaration(expr)
key = expr.literal_hash
enclosers = ([self.get_declaration(expr)] +
enclosers = ([get_declaration(expr)] +
[f[key] for f in self.frames
if key in f and is_subtype(type, f[key])])
return enclosers[-1]
Expand Down Expand Up @@ -342,3 +335,11 @@ def top_frame_context(self) -> Iterator[Frame]:
assert len(self.frames) == 1
yield self.push_frame()
self.pop_frame(True, 0)


def get_declaration(expr: BindableExpression) -> Type:
if isinstance(expr, RefExpr) and isinstance(expr.node, Var):
type = expr.node.type
if not isinstance(type, PartialType):
return type
return None
13 changes: 4 additions & 9 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from mypy.join import join_types
from mypy.treetransform import TransformVisitor
from mypy.meet import meet_simple, is_overlapping_types
from mypy.binder import ConditionalTypeBinder
from mypy.binder import ConditionalTypeBinder, get_declaration
from mypy.options import Options

from mypy import experiments
Expand Down Expand Up @@ -1149,10 +1149,7 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
rvalue_type = self.check_simple_assignment(lvalue_type, rvalue, lvalue)

if rvalue_type and infer_lvalue_type:
self.binder.assign_type(lvalue,
rvalue_type,
lvalue_type,
False)
self.binder.assign_type(lvalue, rvalue_type, lvalue_type, False)
elif index_lvalue:
self.check_indexed_assignment(index_lvalue, rvalue, rvalue)

Expand Down Expand Up @@ -1858,10 +1855,8 @@ def flatten(t: Expression) -> List[Expression]:
s.expr.accept(self)
for elt in flatten(s.expr):
if isinstance(elt, NameExpr):
self.binder.assign_type(elt,
DeletedType(source=elt.name),
self.binder.get_declaration(elt),
False)
self.binder.assign_type(elt, DeletedType(source=elt.name),
get_declaration(elt), False)
return None

def visit_decorator(self, e: Decorator) -> Type:
Expand Down
0