class documentation

Matcher for abstract values.

Exception MatchError Undocumented
Method __init__ Undocumented
Method compute_matches Compute information about type parameters using one-way unification.
Method compute_one_match Match a Variable against a type.
Method match_from_mro Checks a type's MRO for a match for a formal type.
Method match_var_against_type Match a variable against a type.
Method _check_type_param_consistency Undocumented
Method _discard_ambiguous_values Undocumented
Method _error_details Package up additional error details.
Method _get_attribute_for_protocol_matching Gets the specified attribute from cls, for protocol matching.
Method _get_attribute_names Get the attributes implemented (or implicit) on a type.
Method _get_attribute_types Undocumented
Method _get_bad_type Undocumented
Method _get_full_name Undocumented
Method _get_param_matcher Helper for matching the parameters of a callable.
Method _get_signatures Undocumented
Method _get_type Undocumented
Method _instantiate_and_match Instantiate and match an abstract value.
Method _is_native_callable Undocumented
Method _match_against_protocol Checks whether a type is compatible with a protocol.
Method _match_all_bindings Matches all of var's bindings against other_type.
Method _match_base_class_flat Undocumented
Method _match_callable_args_against_callable Undocumented
Method _match_callable_instance Used by _match_instance.
Method _match_dict_against_typed_dict Undocumented
Method _match_heterogeneous_tuple_instance Used by _match_instance.
Method _match_instance Used by _match_instance_against_type. Matches one MRO entry.
Method _match_instance_against_type Checks whether an instance of a type is compatible with a (formal) type.
Method _match_maybe_parameterized_instance Used by _match_instance.
Method _match_nonfinal_value_against_type Match after unwrapping any `Final` annotations.
Method _match_protocol_attribute Checks whether left and other_type are compatible in the given attribute.
Method _match_signature_against_callable Match a function.Signature against a parameterized callable.
Method _match_signature_args_against_callable Helper function for _match_signature_against_callable.
Method _match_subst_against_subst Undocumented
Method _match_type_against_callback_protocol See https://www.python.org/dev/peps/pep-0544/#callback-protocols.
Method _match_type_against_type Checks whether a type is compatible with a (formal) type.
Method _match_type_param_against_type_param Match a TypeVar against another TypeVar.
Method _match_value_against_type One-way unify value into pytd type given a substitution.
Method _merge_matches Undocumented
Method _merge_substs Undocumented
Method _mutate_type_parameters Undocumented
Method _reset_errors Undocumented
Method _resolve_function_attribute_value Undocumented
Method _resolve_function_attribute_var Returns a resolved attribute and whether any unbinding occurred.
Method _satisfies_common_superclass Enforce that the variable's values share a superclass below object.
Method _satisfies_noniterable_str Enforce a str to NOT be matched against a conflicting iterable type.
Method _satisfies_single_type Enforce that the variable contains only one concrete type.
Method _subst_with_type_parameters_from Undocumented
Method _track_partially_matched_protocols Context manager for handling the protocol cache.
Instance Variable _error_subst Undocumented
Instance Variable _node Undocumented
Instance Variable _noniterable_str_error Undocumented
Instance Variable _paramspecs Undocumented
Instance Variable _protocol_cache Undocumented
Instance Variable _protocol_error Undocumented
Instance Variable _recursive_annots_cache Undocumented
Instance Variable _type_params Undocumented
Instance Variable _typed_dict_error Undocumented

Inherited from ContextWeakrefMixin:

Class Variable __slots__ Undocumented
Instance Variable ctx_weakref Undocumented
Property ctx Undocumented
def __init__(self, node, ctx): (source)
def compute_matches(self, args: List[function.Arg], match_all_views: bool, keep_all_views: bool = False, alias_map: Optional[datatypes.UnionFind] = None) -> List[GoodMatch]: (source)

Compute information about type parameters using one-way unification. Given the arguments of a function call, try to find substitutions that match them against their expected types. Args: args: A sequence of function arguments. match_all_views: If True, every possible match must succeed for the overall match to be considered a success. Otherwise, the overall match succeeds as long as at least one possible match succeeds. keep_all_views: If True, avoid optimizations that discard views. alias_map: Optionally, a datatypes.UnionFind, which stores all the type renaming information, mapping of type parameter name to its representative. Returns: A sequence of GoodMatch results containing the computed substitutions. Raises: MatchError: if any of the arguments does not match its expected type.

def compute_one_match(self, var, other_type, name=None, match_all_views=True, keep_all_views=False, alias_map=None) -> MatchResult: (source)

Match a Variable against a type. Args: var: A cfg.Variable, containing instances. other_type: An instance of BaseValue. name: Optionally, the variable name. match_all_views: If True, every possible match must succeed for the overall match to be considered a success. Otherwise, the overall match succeeds as long as at least one possible match succeeds. keep_all_views: If True, avoid optimizations that discard views. alias_map: Optionally, a datatypes.UnionFind, which stores all the type renaming information, mapping of type parameter name to its representative. Returns: The match result.

def match_from_mro(self, left, other_type, allow_compat_builtins=True): (source)

Checks a type's MRO for a match for a formal type. Args: left: The type. other_type: The formal type. allow_compat_builtins: Whether to allow compatible builtins to match - e.g., int against float. Returns: The match, if any, None otherwise.

def match_var_against_type(self, var, other_type, subst, view): (source)

Match a variable against a type.

def _check_type_param_consistency(self, new_value: abstract.BaseValue, new_value_binding: cfg.Binding, t: abstract.TypeParameter, subst: _SubstType) -> Tuple[cfg.Variable, bool]: (source)

Undocumented

def _discard_ambiguous_values(self, values): (source)

Undocumented

def _error_details(self): (source)

Package up additional error details.

def _get_attribute_for_protocol_matching(self, cls: abstract.BaseValue, name: str, instance: Optional[abstract.BaseValue], unbind: bool) -> Tuple[Optional[cfg.Variable], bool]: (source)

Gets the specified attribute from cls, for protocol matching. Args: cls: The class to get the attribute from. name: The name of the attribute. instance: Optionally, an instance of the class. unbind: Whether to unbind the attribute if it is a bound function. Returns: A tuple of the attribute and whether any unbinding occurred.

def _get_attribute_names(self, left): (source)

Get the attributes implemented (or implicit) on a type.

def _get_attribute_types(self, other_type, attribute): (source)

Undocumented

def _get_bad_type(self, name: Optional[str], expected: abstract.BaseValue) -> abstract_utils.BadType: (source)

Undocumented

def _get_full_name(self, cls): (source)

Undocumented

def _get_param_matcher(self, callable_type): (source)

Helper for matching the parameters of a callable. Args: callable_type: The callable being matched against. Returns: A special param matcher: (left, right, subst) -> Optional[subst]. left: An argument to be matched against a parameter of callable_type. right: A parameter of callable_type. subst: The current substitution dictionary. If the matcher returns a non-None subst dict, then the match has succeeded via special matching rules for single TypeVars. Otherwise, the caller should next attempt normal matching on the inputs. (See _match_signature_against_callable for a usage example.)

def _get_signatures(self, func, subst, view): (source)

Undocumented

def _get_type(self, value): (source)

Undocumented

def _instantiate_and_match(self, left, other_type, subst, view, container=None): (source)

Instantiate and match an abstract value.

def _is_native_callable(self, val): (source)

Undocumented

def _match_against_protocol(self, left, other_type, subst, view): (source)

Checks whether a type is compatible with a protocol. Args: left: An instance of a type. other_type: A protocol. subst: The current type parameter assignment. view: The current mapping of Variable to Value. Returns: A new type parameter assignment if the matching succeeded, None otherwise.

def _match_all_bindings(self, var, other_type, subst, view): (source)

Matches all of var's bindings against other_type.

def _match_base_class_flat(self, base_cls, other_type, allow_compat_builtins): (source)

Undocumented

def _match_callable_args_against_callable(self, left, other_type, subst, view): (source)

Undocumented

def _match_callable_instance(self, left, instance, other_type, subst, view): (source)

Used by _match_instance.

def _match_dict_against_typed_dict(self, left: abstract.BaseValue, other_type: typed_dict.TypedDictClass) -> bool: (source)

Undocumented

def _match_heterogeneous_tuple_instance(self, left, instance, other_type, subst, view): (source)

Used by _match_instance.

def _match_instance(self, left, instance, other_type, subst, view): (source)

Used by _match_instance_against_type. Matches one MRO entry. Called after the instance has been successfully matched against a formal type to do any remaining matching special to the type. Args: left: The instance type, which may be different from instance.cls depending on where in the mro the match happened. instance: The instance. other_type: The formal type that was successfully matched against. subst: The current type parameter assignment. view: The current mapping of Variable to Value. Returns: A new type parameter assignment if the matching succeeded, None otherwise.

def _match_instance_against_type(self, left, other_type, subst, view): (source)

Checks whether an instance of a type is compatible with a (formal) type. Args: left: An instance of a type. other_type: A formal type. E.g. abstract.Class or abstract.Union. subst: The current type parameter assignment. view: The current mapping of Variable to Value. Returns: A new type parameter assignment if the matching succeeded, None otherwise.

def _match_maybe_parameterized_instance(self, left, instance, other_type, subst, view): (source)

Used by _match_instance.

def _match_nonfinal_value_against_type(self, left: abstract.BaseValue, value: cfg.Binding, other_type: abstract.BaseValue, subst: _SubstType, view: _ViewType) -> Optional[_SubstType]: (source)

Match after unwrapping any `Final` annotations.

def _match_protocol_attribute(self, left, other_type, attribute, subst, view): (source)

Checks whether left and other_type are compatible in the given attribute. Args: left: An instance of a type. other_type: A protocol. attribute: An attribute name. subst: The current type parameter assignment. view: The current mapping of Variable to Value. Returns: A new type parameter assignment if the matching succeeded, None otherwise.

def _match_signature_against_callable(self, sig, other_type, subst, view): (source)

Match a function.Signature against a parameterized callable.

def _match_signature_args_against_callable(self, sig, other_type, subst, view, param_match): (source)

Helper function for _match_signature_against_callable.

def _match_subst_against_subst(self, old_subst, new_subst, type_param_map, has_self): (source)

Undocumented

def _match_type_against_callback_protocol(self, left, other_type, subst, view): (source)

See https://www.python.org/dev/peps/pep-0544/#callback-protocols.

def _match_type_against_type(self, left, other_type, subst, view): (source)

Checks whether a type is compatible with a (formal) type. Args: left: A type. other_type: A formal type. E.g. abstract.Class or abstract.Union. subst: The current type parameter assignment. view: The current mapping of Variable to Value. Returns: A new type parameter assignment if the matching succeeded, None otherwise.

def _match_type_param_against_type_param(self, t1, t2, subst, view): (source)

Match a TypeVar against another TypeVar.

def _match_value_against_type(self, value: cfg.Binding, other_type: abstract.BaseValue, subst: _SubstType, view: _ViewType) -> Optional[_SubstType]: (source)

One-way unify value into pytd type given a substitution. Args: value: A cfg.Binding. other_type: A BaseValue instance. subst: The current substitution. This dictionary is not modified. view: A mapping of Variable to Value. Returns: A new (or unmodified original) substitution dict if the matching succeeded, None otherwise.

def _merge_matches(self, name: str, formal: abstract.BaseValue, old_matches: Optional[List[GoodMatch]], new_matches: List[GoodMatch], keep_all_views: bool, has_self: bool) -> List[GoodMatch]: (source)

Undocumented

def _merge_substs(self, subst, new_substs): (source)

Undocumented

def _mutate_type_parameters(self, params, value, subst): (source)

Undocumented

def _reset_errors(self): (source)

Undocumented

def _resolve_function_attribute_value(self, attr: abstract.BaseValue, unbind: bool) -> abstract.BaseValue: (source)

Undocumented

def _resolve_function_attribute_var(self, attribute: cfg.Variable, unbind: bool) -> Tuple[cfg.Variable, bool]: (source)

Returns a resolved attribute and whether any unbinding occurred.

def _satisfies_common_superclass(self, values): (source)

Enforce that the variable's values share a superclass below object.

def _satisfies_noniterable_str(self, left, other_type): (source)

Enforce a str to NOT be matched against a conflicting iterable type.

def _satisfies_single_type(self, values): (source)

Enforce that the variable contains only one concrete type.

def _subst_with_type_parameters_from(self, subst, typ): (source)

Undocumented

@contextlib.contextmanager
def _track_partially_matched_protocols(self): (source)

Context manager for handling the protocol cache. Some protocols have methods that return instances of the protocol, e.g. Iterator.next returns Iterator. This will cause an infinite loop, which can be avoided by tracking partially matched protocols. To prevent collisions, keys are removed from the cache as soon as match is completed. Yields: Into the protocol matching context.

_error_subst = (source)

Undocumented

Undocumented

_noniterable_str_error = (source)

Undocumented

_paramspecs: dict = (source)

Undocumented

_protocol_cache = (source)

Undocumented

_protocol_error = (source)

Undocumented

Undocumented

_type_params = (source)

Undocumented

_typed_dict_error = (source)

Undocumented