-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[WIP] Tighten Lvalues and remove all references to Node #2221
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c53a1c4
NewType+namedtuple: common method for typeinfo
elazarg 3b4cd13
Merge remote-tracking branch 'upstream/master'
elazarg 23db5cc
Typedef TypeMap everywhere
elazarg 7a651a8
lint
elazarg eb04f80
TypeMap is Expression->Type
elazarg fa752e3
remove Node from binder
elazarg f6bdc20
tighten lvalues for binder
elazarg 9ae6934
remove some checks
elazarg 6684f70
try to make Lvalue real in one huge step
elazarg 009c0db
freeze behavior
elazarg ace8989
fix var in binder, make MypyFile non-statement
elazarg 9aff952
remove references to node from everywhere outside mypy.nodes
elazarg 4e37a6e
Merge
elazarg a2df9c4
separate different TypeMaps. Better names for binder methods
elazarg 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
tighten lvalues for binder
- Loading branch information
commit f6bdc207c17bb6fd7e5e08dcf993f4df1400b2cc
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from builtins import type as realtype | ||
from typing import (Any, Dict, List, Set, Iterator, Union) | ||
from contextlib import contextmanager | ||
|
||
|
@@ -8,15 +9,20 @@ | |
|
||
from mypy.sametypes import is_same_type | ||
|
||
from mypy.nodes import IndexExpr, MemberExpr, NameExpr, SuperExpr | ||
|
||
class Frame(Dict[Any, Type]): | ||
pass | ||
|
||
BLval = Union[IndexExpr, MemberExpr, NameExpr] | ||
|
||
|
||
class Key(AnyType): | ||
pass | ||
|
||
|
||
class Frame(Dict[Key, Type]): | ||
pass | ||
|
||
|
||
class ConditionalTypeBinder: | ||
"""Keep track of conditional types of variables. | ||
|
||
|
@@ -97,6 +103,7 @@ def _get(self, key: Key, index: int=-1) -> Type: | |
return None | ||
|
||
def push(self, expr: Expression, typ: Type) -> None: | ||
assert isinstance(expr, (IndexExpr, MemberExpr, NameExpr)) | ||
if not expr.literal: | ||
return | ||
key = expr.literal_hash | ||
|
@@ -105,11 +112,16 @@ def push(self, expr: Expression, typ: Type) -> None: | |
self._add_dependencies(key) | ||
self._push(key, typ) | ||
|
||
def get(self, expr: Union[Expression, Var]) -> Type: | ||
def get(self, expr: Union[BLval, Var]) -> Type: | ||
assert isinstance(expr, BLval.__union_params__ + (Var,)) | ||
if isinstance(expr, Var): | ||
# where is this literal hash initialized? | ||
return self._get(expr.literal_hash) | ||
return self._get(expr.literal_hash) | ||
|
||
def cleanse(self, expr: Expression) -> None: | ||
def cleanse(self, expr: BLval) -> None: | ||
"""Remove all references to an Expression from the binder.""" | ||
assert isinstance(expr, BLval.__union_params__) | ||
self._cleanse_key(expr.literal_hash) | ||
|
||
def _cleanse_key(self, key: Key) -> None: | ||
|
@@ -165,7 +177,8 @@ def pop_frame(self, fall_through: int = 0) -> Frame: | |
|
||
return result | ||
|
||
def get_declaration(self, expr: Expression) -> Type: | ||
def get_declaration(self, expr: BLval) -> Type: | ||
assert isinstance(expr, BLval.__union_params__) | ||
if isinstance(expr, (RefExpr, SymbolTableNode)) and isinstance(expr.node, Var): | ||
type = expr.node.type | ||
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. The test here is never true, and I think the original (changed in one of my previous PRs) was never true either. I wonder why. |
||
if isinstance(type, PartialType): | ||
|
@@ -174,11 +187,12 @@ def get_declaration(self, expr: Expression) -> Type: | |
else: | ||
return None | ||
|
||
def assign_type(self, expr: Expression, | ||
def assign_type(self, expr: Union[BLval, SuperExpr], | ||
type: Type, | ||
declared_type: Type, | ||
restrict_any: bool = False) -> None: | ||
if not expr.literal: | ||
assert isinstance(expr, BLval.__union_params__ + (SuperExpr,)) | ||
if isinstance(expr, SuperExpr) or not expr.literal: | ||
return | ||
self.invalidate_dependencies(expr) | ||
|
||
|
@@ -211,7 +225,8 @@ def assign_type(self, expr: Expression, | |
# just copy this variable into a single stored frame. | ||
self.allow_jump(i) | ||
|
||
def invalidate_dependencies(self, expr: Expression) -> None: | ||
def invalidate_dependencies(self, expr: BLval) -> None: | ||
assert isinstance(expr, BLval.__union_params__) | ||
"""Invalidate knowledge of types that include expr, but not expr itself. | ||
|
||
For example, when expr is foo.bar, invalidate foo.bar.baz. | ||
|
@@ -222,7 +237,8 @@ def invalidate_dependencies(self, expr: Expression) -> None: | |
for dep in self.dependencies.get(expr.literal_hash, set()): | ||
self._cleanse_key(dep) | ||
|
||
def most_recent_enclosing_type(self, expr: Expression, type: Type) -> Type: | ||
def most_recent_enclosing_type(self, expr: BLval, type: Type) -> Type: | ||
assert isinstance(expr, BLval.__union_params__) | ||
if isinstance(type, AnyType): | ||
return self.get_declaration(expr) | ||
key = expr.literal_hash | ||
|
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 why is it needed. This can be fixed regardless of this PR.