class documentation

class ConditionalTypeBinder: (source)

View In Hierarchy

Keep track of conditional types of variables.

NB: Variables are tracked by literal expression, so it is possible to confuse the binder; for example,

``` class A:

a = None # type: Union[int, str]

x = A() lst = [x] reveal_type(x.a) # Union[int, str] x.a = 1 reveal_type(x.a) # int reveal_type(lst[0].a) # Union[int, str] lst[0].a = 'a' reveal_type(x.a) # int reveal_type(lst[0].a) # str ```

Method __init__ Undocumented
Method accumulate​_type​_assignments Push a new map to collect assigned types in multiassign from union.
Method allow​_jump Undocumented
Method assign​_type Undocumented
Method cleanse Remove all references to a Node from the binder.
Method frame​_context Return a context manager that pushes/pops frames on enter/exit.
Method get Undocumented
Method handle​_break Undocumented
Method handle​_continue Undocumented
Method invalidate​_dependencies Invalidate knowledge of types that include expr, but not expr itself.
Method is​_unreachable Undocumented
Method is​_unreachable​_warning​_suppressed Undocumented
Method most​_recent​_enclosing​_type Undocumented
Method pop​_frame Pop a frame and return it.
Method push​_frame Push a new frame into the binder.
Method put Undocumented
Method suppress​_unreachable​_warnings Undocumented
Method top​_frame​_context A variant of frame_context for use at the top level of a namespace (module, function, or class).
Method unreachable Undocumented
Method update​_from​_options Update the frame to reflect that each key will be updated as in one of the frames. Return whether any item changes.
Instance Variable break​_frames Undocumented
Instance Variable continue​_frames Undocumented
Instance Variable declarations Undocumented
Instance Variable dependencies Undocumented
Instance Variable frames Undocumented
Instance Variable last​_pop​_changed Undocumented
Instance Variable next​_id Undocumented
Instance Variable options​_on​_return Undocumented
Instance Variable try​_frames Undocumented
Instance Variable type​_assignments Undocumented
Method _add​_dependencies Undocumented
Method _cleanse​_key Remove all references to a key from the binder.
Method _get Undocumented
Method _get​_id Undocumented
Method _put Undocumented
def __init__(self): (source)

Undocumented

@contextmanager
def accumulate_type_assignments(self): (source)

Push a new map to collect assigned types in multiassign from union.

If this map is not None, actual binding is deferred until all items in the union are processed (a union of collected items is later bound manually by the caller).

Returns
Iterator[Assigns]Undocumented
def allow_jump(self, index): (source)

Undocumented

Parameters
index:intUndocumented
def assign_type(self, expr, type, declared_type, restrict_any=False): (source)

Undocumented

Parameters
expr:ExpressionUndocumented
type:TypeUndocumented
declared​_type:Optional[Type]Undocumented
restrict​_any:boolUndocumented
def cleanse(self, expr): (source)
Remove all references to a Node from the binder.
Parameters
expr:ExpressionUndocumented
@contextmanager
def frame_context(self, *, can_skip, fall_through=1, break_frame=0, continue_frame=0, conditional_frame=False, try_frame=False): (source)

Return a context manager that pushes/pops frames on enter/exit.

If can_skip is True, control flow is allowed to bypass the newly-created frame.

If fall_through > 0, then it will allow control flow that falls off the end of the frame to escape to its ancestor fall_through levels higher. Otherwise control flow ends at the end of the frame.

If break_frame > 0, then 'break' statements within this frame will jump out to the frame break_frame levels higher than the frame created by this call to frame_context. Similarly for continue_frame and 'continue' statements.

If try_frame is true, then execution is allowed to jump at any point within the newly created frame (or its descendants) to its parent (i.e., to the frame that was on top before this call to frame_context).

After the context manager exits, self.last_pop_changed indicates whether any types changed in the newly-topmost frame as a result of popping this frame.

Parameters
can​_skip:boolUndocumented
fall​_through:intUndocumented
break​_frame:intUndocumented
continue​_frame:intUndocumented
conditional​_frame:boolUndocumented
try​_frame:boolUndocumented
Returns
Iterator[Frame]Undocumented
def get(self, expr): (source)

Undocumented

Parameters
expr:ExpressionUndocumented
Returns
Optional[Type]Undocumented
def handle_break(self): (source)

Undocumented

def handle_continue(self): (source)

Undocumented

def invalidate_dependencies(self, expr): (source)

Invalidate knowledge of types that include expr, but not expr itself.

For example, when expr is foo.bar, invalidate foo.bar.baz.

It is overly conservative: it invalidates globally, including in code paths unreachable from here.

Parameters
expr:BindableExpressionUndocumented
def is_unreachable(self): (source)

Undocumented

Returns
boolUndocumented
def is_unreachable_warning_suppressed(self): (source)

Undocumented

Returns
boolUndocumented
def most_recent_enclosing_type(self, expr, type): (source)

Undocumented

Parameters
expr:BindableExpressionUndocumented
type:TypeUndocumented
Returns
Optional[Type]Undocumented
def pop_frame(self, can_skip, fall_through): (source)

Pop a frame and return it.

See frame_context() for documentation of fall_through.

Parameters
can​_skip:boolUndocumented
fall​_through:intUndocumented
Returns
FrameUndocumented
def push_frame(self, conditional_frame=False): (source)
Push a new frame into the binder.
Parameters
conditional​_frame:boolUndocumented
Returns
FrameUndocumented
def put(self, expr, typ): (source)

Undocumented

Parameters
expr:ExpressionUndocumented
typ:TypeUndocumented
def suppress_unreachable_warnings(self): (source)

Undocumented

@contextmanager
def top_frame_context(self): (source)
A variant of frame_context for use at the top level of a namespace (module, function, or class).
Returns
Iterator[Frame]Undocumented
def unreachable(self): (source)

Undocumented

def update_from_options(self, frames): (source)

Update the frame to reflect that each key will be updated as in one of the frames. Return whether any item changes.

If a key is declared as AnyType, only update it if all the options are the same.

Parameters
frames:List[Frame]Undocumented
Returns
boolUndocumented
break_frames: List[int] = (source)

Undocumented

continue_frames: List[int] = (source)

Undocumented

declarations: Dict[Key, Optional[Type]] = (source)

Undocumented

dependencies: Dict[Key, Set[Key]] = (source)

Undocumented

frames = (source)

Undocumented

last_pop_changed = (source)

Undocumented

next_id: int = (source)

Undocumented

options_on_return: List[List[Frame]] = (source)

Undocumented

try_frames: Set[int] = (source)

Undocumented

type_assignments = (source)

Undocumented

def _add_dependencies(self, key, value=None): (source)

Undocumented

Parameters
key:KeyUndocumented
value:Optional[Key]Undocumented
def _cleanse_key(self, key): (source)
Remove all references to a key from the binder.
Parameters
key:KeyUndocumented
def _get(self, key, index=-1): (source)

Undocumented

Parameters
key:KeyUndocumented
index:intUndocumented
Returns
Optional[Type]Undocumented
def _get_id(self): (source)

Undocumented

Returns
intUndocumented
def _put(self, key, type, index=-1): (source)

Undocumented

Parameters
key:KeyUndocumented
type:TypeUndocumented
index:intUndocumented