Skip to content

Declare literal_hash as Optional[Key] #3658

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 3 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions mypy/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def put(self, expr: Expression, typ: Type) -> None:
if not expr.literal:
return
key = expr.literal_hash
assert key is not None, 'Internal error: binder tried to put non-literal'
if key not in self.declarations:
assert isinstance(expr, BindableTypes)
self.declarations[key] = get_declaration(expr)
Expand All @@ -132,6 +133,7 @@ def unreachable(self) -> None:
self.frames[-1].unreachable = True

def get(self, expr: Expression) -> Optional[Type]:
assert expr.literal_hash is not None, 'Internal error: binder tried to get non-literal'
return self._get(expr.literal_hash)

def is_unreachable(self) -> bool:
Expand All @@ -141,6 +143,7 @@ def is_unreachable(self) -> bool:

def cleanse(self, expr: Expression) -> None:
"""Remove all references to a Node from the binder."""
assert expr.literal_hash is not None, 'Internal error: binder tried cleanse non-literal'
self._cleanse_key(expr.literal_hash)

def _cleanse_key(self, key: Key) -> None:
Expand Down Expand Up @@ -258,13 +261,15 @@ def invalidate_dependencies(self, expr: BindableExpression) -> None:
It is overly conservative: it invalidates globally, including
in code paths unreachable from here.
"""
assert expr.literal_hash is not None
for dep in self.dependencies.get(expr.literal_hash, set()):
self._cleanse_key(dep)

def most_recent_enclosing_type(self, expr: BindableExpression, type: Type) -> Optional[Type]:
if isinstance(type, AnyType):
return get_declaration(expr)
key = expr.literal_hash
assert key is not None
enclosers = ([get_declaration(expr)] +
[f[key] for f in self.frames
if key in f and is_subtype(type, f[key])])
Expand Down
2 changes: 1 addition & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def accept(self, visitor: StatementVisitor[T]) -> T:
class Expression(Node):
"""An expression node."""
literal = LITERAL_NO
literal_hash = None # type: Key
literal_hash = None # type: Optional[Key]

def accept(self, visitor: ExpressionVisitor[T]) -> T:
raise RuntimeError('Not implemented')
Expand Down