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).

Method __init__ Undocumented
Method add_all_protocol_deps Add all known protocol dependencies to deps.
Method get_assumptions Undocumented
Method is_assumed_proper_subtype Undocumented
Method is_assumed_subtype Undocumented
Method is_cached_subtype_check Undocumented
Method record_protocol_subtype_check Undocumented
Method record_subtype_cache_entry Undocumented
Method reset_all_subtype_caches Completely reset all known subtype caches.
Method reset_all_subtype_caches_for Reset subtype caches (if any) for a given supertype TypeInfo and its MRO.
Method reset_protocol_deps Reset dependencies after a full run or before a daemon shutdown.
Method reset_subtype_caches_for Reset subtype caches (if any) for a given supertype TypeInfo.
Method update_protocol_deps Update global protocol dependency map.
Constant inferring Undocumented
Instance Variable infer_unions Undocumented
Instance Variable proto_deps Undocumented
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 _rechecked_types Undocumented
Constant _subtype_caches Undocumented
def __init__(self): (source)

Undocumented

def add_all_protocol_deps(self, deps: dict[str, set[str]]): (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.

def get_assumptions(self, is_proper: bool) -> list[tuple[Type, Type]]: (source)

Undocumented

def is_assumed_proper_subtype(self, left: Type, right: Type) -> bool: (source)

Undocumented

def is_assumed_subtype(self, left: Type, right: Type) -> bool: (source)

Undocumented

def is_cached_subtype_check(self, kind: SubtypeKind, left: Instance, right: Instance) -> bool: (source)

Undocumented

def record_protocol_subtype_check(self, left_type: TypeInfo, right_type: TypeInfo): (source)

Undocumented

def record_subtype_cache_entry(self, kind: SubtypeKind, left: Instance, right: Instance): (source)

Undocumented

def reset_all_subtype_caches(self): (source)

Completely reset all known subtype caches.

def reset_all_subtype_caches_for(self, info: TypeInfo): (source)

Reset subtype caches (if any) for a given supertype TypeInfo and its MRO.

def reset_protocol_deps(self): (source)

Reset dependencies after a full run or before a daemon shutdown.

def reset_subtype_caches_for(self, info: TypeInfo): (source)

Reset subtype caches (if any) for a given supertype TypeInfo.

def update_protocol_deps(self, second_map: dict[str, set[str]]|None = 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.

inferring: list = (source)

Undocumented

infer_unions: bool = (source)

Undocumented

proto_deps: dict = (source)

Undocumented

def _snapshot_protocol_deps(self) -> dict[str, set[str]]: (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').

_assuming: list = (source)

Undocumented

_assuming_proper: list = (source)

Undocumented

_attempted_protocols: dict = (source)

Undocumented

_checked_against_members: dict = (source)

Undocumented

_rechecked_types = (source)

Undocumented

_subtype_caches: dict = (source)

Undocumented