class documentation

class TypeState: (source)

View In Hierarchy

This class provides subtype caching to improve performance of subtype checks. It also holds protocol fine grained dependencies.

Note: to avoid leaking global state, 'reset_all_subtype_caches()' should be called after a build has finished and after a daemon shutdown. This subtype cache only exists for performance reasons, resetting subtype caches for a class has no semantic effect. The protocol dependencies however are only stored here, and shouldn't be deleted unless not needed any more (e.g. during daemon shutdown).

Static Method add​_all​_protocol​_deps Add all known protocol dependencies to deps.
Static Method is​_assumed​_proper​_subtype Undocumented
Static Method is​_assumed​_subtype Undocumented
Static Method is​_cached​_subtype​_check Undocumented
Static Method record​_protocol​_subtype​_check Undocumented
Static Method record​_subtype​_cache​_entry Undocumented
Static Method reset​_all​_subtype​_caches Completely reset all known subtype caches.
Static Method reset​_all​_subtype​_caches​_for Reset subtype caches (if any) for a given supertype TypeInfo and its MRO.
Static Method reset​_protocol​_deps Reset dependencies after a full run or before a daemon shutdown.
Static Method reset​_subtype​_caches​_for Reset subtype caches (if any) for a given supertype TypeInfo.
Static Method update​_protocol​_deps Update global protocol dependency map.
Class Variable proto​_deps Undocumented
Static Method _snapshot​_protocol​_deps Collect protocol attribute dependencies found so far from registered subtype checks.
Constant _assuming Undocumented
Constant _assuming​_proper Undocumented
Constant _attempted​_protocols Undocumented
Constant _checked​_against​_members Undocumented
Constant _inferring Undocumented
Constant _rechecked​_types Undocumented
Constant _subtype​_caches Undocumented
@staticmethod
def add_all_protocol_deps(deps): (source)

Add all known protocol dependencies to deps.

This is used by tests and debug output, and also when collecting all collected or loaded dependencies as part of build.

Parameters
deps:Dict[str, Set[str]]Undocumented
@staticmethod
def is_assumed_proper_subtype(left, right): (source)

Undocumented

Parameters
left:TypeUndocumented
right:TypeUndocumented
Returns
boolUndocumented
@staticmethod
def is_assumed_subtype(left, right): (source)

Undocumented

Parameters
left:TypeUndocumented
right:TypeUndocumented
Returns
boolUndocumented
@staticmethod
def is_cached_subtype_check(kind, left, right): (source)

Undocumented

Parameters
kind:SubtypeKindUndocumented
left:InstanceUndocumented
right:InstanceUndocumented
Returns
boolUndocumented
@staticmethod
def record_protocol_subtype_check(left_type, right_type): (source)

Undocumented

Parameters
left​_type:TypeInfoUndocumented
right​_type:TypeInfoUndocumented
@staticmethod
def record_subtype_cache_entry(kind, left, right): (source)

Undocumented

Parameters
kind:SubtypeKindUndocumented
left:InstanceUndocumented
right:InstanceUndocumented
@staticmethod
def reset_all_subtype_caches(): (source)
Completely reset all known subtype caches.
@staticmethod
def reset_all_subtype_caches_for(info): (source)
Reset subtype caches (if any) for a given supertype TypeInfo and its MRO.
Parameters
info:TypeInfoUndocumented
@staticmethod
def reset_protocol_deps(): (source)
Reset dependencies after a full run or before a daemon shutdown.
@staticmethod
def reset_subtype_caches_for(info): (source)
Reset subtype caches (if any) for a given supertype TypeInfo.
Parameters
info:TypeInfoUndocumented
@staticmethod
def update_protocol_deps(second_map=None): (source)

Update global protocol dependency map.

We update the global map incrementally, using a snapshot only from recently type checked types. If second_map is given, update it as well. This is currently used by FineGrainedBuildManager that maintains normal (non-protocol) dependencies.

Parameters
second​_map:Optional[Dict[str, Set[str]]]Undocumented
proto_deps: ClassVar[Optional[Dict[str, Set[str]]]] = (source)

Undocumented

@staticmethod
def _snapshot_protocol_deps(): (source)

Collect protocol attribute dependencies found so far from registered subtype checks.

There are three kinds of protocol dependencies. For example, after a subtype check:

x: Proto = C()
the following dependencies will be generated:
  1. ..., <SuperProto[wildcard]>, <Proto[wildcard]> -> <Proto>
  2. ..., <B.attr>, <C.attr> -> <C> [for every attr in Proto members]
  3. <C> -> Proto # this one to invalidate the subtype cache

The first kind is generated immediately per-module in deps.py (see also an example there for motivation why it is needed). While two other kinds are generated here after all modules are type checked and we have recorded all the subtype checks. To understand these two kinds, consider a simple example:

class A:
def __iter__(self) -> Iterator[int]:
...

it: Iterable[int] = A()

We add <a.A.__iter__> -> <a.A> to invalidate the assignment (module target in this case), whenever the signature of a.A.__iter__ changes. We also add <a.A> -> typing.Iterable, to invalidate the subtype caches of the latter. (Note that the same logic applies to proper subtype checks, and calculating meets and joins, if this involves calling 'subtypes.is_protocol_implementation').

Returns
Dict[str, Set[str]]Undocumented
_assuming: List[Tuple[TypeAliasType, TypeAliasType]] = (source)

Undocumented

Value
[]
_assuming_proper: List[Tuple[TypeAliasType, TypeAliasType]] = (source)

Undocumented

Value
[]
_attempted_protocols: Dict[str, Set[str]] = (source)

Undocumented

Value
{}
_checked_against_members: Dict[str, Set[str]] = (source)

Undocumented

Value
{}
_inferring: List[TypeAliasType] = (source)

Undocumented

Value
[]
_rechecked_types: Set[TypeInfo] = (source)

Undocumented

Value
set()
_subtype_caches: SubtypeCache = (source)

Undocumented

Value
{}