module documentation

Utilities for abstract.py.

Class AsInstance Wrapper, used for marking things that we want to convert to an instance.
Class AsReturnValue Specially mark return values, to handle NoReturn properly.
Class BadType Undocumented
Class LazyFormalTypeParameters Undocumented
Class Local A possibly annotated local variable.
Exception ConversionError Undocumented
Exception EvaluationError Used to signal an errorlog error during type name evaluation.
Exception GenericTypeError The error for user-defined generic types.
Exception ModuleLoadError Signal an error when trying to lazily load a submodule.
Function apply_mutations Apply mutations yielded from a get_mutations function.
Function build_generic_template Build a typing.Generic template from a sequence of type parameters.
Function check_against_mro Check if any of the classes are in the target's MRO.
Function combine_substs Combines the two collections of type parameter substitutions.
Function equivalent_to Whether binding.data is equivalent to cls, modulo parameterization.
Function eval_expr Evaluate an expression with the given node and globals.
Function expand_type_parameter_instances Expands any TypeParameterInstance values in `bindings`.
Function flatten Flatten the contents of value into classes.
Function full_type_name Compute complete type parameter name with scope.
Function get_annotations_dict Get __annotations__ from a members map.
Function get_atomic_python_constant Get the concrete atomic Python value stored in this variable.
Function get_atomic_value Get the atomic value stored in this variable.
Function get_dict_fullhash_component Hash a dictionary.
Function get_generic_type Gets the generic type of an abstract value.
Function get_mro_bases Get bases for MRO computation.
Function get_type_parameter_substitutions Get values for type_params from val's type parameters.
Function get_var_fullhash_component Undocumented
Function get_views Get all possible views of the given variables at a particular node.
Function is_callable Returns whether 'value' is a callable.
Function is_concrete_dict Undocumented
Function is_concrete_list Undocumented
Function is_ellipsis Undocumented
Function is_generic_protocol Undocumented
Function is_indefinite_iterable True if val is a non-concrete instance of typing.Iterable.
Function is_recursive_annotation Undocumented
Function is_subclass Wehther binding.data is a subclass of cls, modulo parameterization.
Function is_var_indefinite_iterable True if all bindings of var are indefinite sequences.
Function is_var_splat Undocumented
Function match_atomic_python_constant Undocumented
Function match_atomic_value Undocumented
Function match_type_container Unpack the type parameter from ContainerType[T].
Function maybe_extract_tuple Returns a tuple of Variables.
Function maybe_unwrap_decorated_function Undocumented
Function merged_type_parameter Undocumented
Function parse_formal_type_parameters Parse type parameters from base class.
Function show_constant Pretty-print a value if it is a constant.
Function simplify_variable Deduplicates identical data in `var`.
Function unwrap_final Unwrap Final[T] -> T.
Function unwrap_splat Undocumented
Function update_args_dict Update a {str: Variable} dict by merging bindings.
Function with_empty_substitutions Undocumented
Constant ARGS Undocumented
Constant CLASS_LEVEL_IGNORE Undocumented
Constant DUMMY_CONTAINER Undocumented
Constant DYNAMIC_ATTRIBUTE_MARKERS Undocumented
Constant K Undocumented
Constant RET Undocumented
Constant T Undocumented
Constant T2 Undocumented
Constant TOP_LEVEL_IGNORE Undocumented
Constant V Undocumented
Variable log Undocumented
Class _None Undocumented
Function _isinstance Do an isinstance() call for a class defined in pytype.abstract.
Function _make Make an instance of cls_name with the given arguments.
Function _merge_type Merge two types.
Type Alias _ArgsDictType Undocumented
def apply_mutations(node, get_mutations): (source)

Apply mutations yielded from a get_mutations function.

def build_generic_template(type_params: Sequence[_BaseValueType], base_type: _BaseValueType) -> Tuple[Sequence[str], Sequence[_TypeParamType]]: (source)

Build a typing.Generic template from a sequence of type parameters.

def check_against_mro(ctx, target, class_spec): (source)

Check if any of the classes are in the target's MRO. Args: ctx: The abstract context. target: A BaseValue whose MRO will be checked. class_spec: A Class or PythonConstant tuple of classes (i.e. the second argument to isinstance or issubclass). Returns: True if any class in classes is found in the target's MRO, False if no match is found and None if it's ambiguous.

Combines the two collections of type parameter substitutions.

def equivalent_to(binding, cls): (source)

Whether binding.data is equivalent to cls, modulo parameterization.

def eval_expr(ctx, node, f_globals, f_locals, expr): (source)

Evaluate an expression with the given node and globals.

def expand_type_parameter_instances(bindings: Iterable[cfg.Binding]): (source)

Expands any TypeParameterInstance values in `bindings`.

def flatten(value, classes): (source)

Flatten the contents of value into classes. If value is a Class, it is appended to classes. If value is a PythonConstant of type tuple, then each element of the tuple that has a single binding is also flattened. Any other type of value, or tuple elements that have multiple bindings are ignored. Args: value: An abstract value. classes: A list to be modified. Returns: True iff a value was ignored during flattening.

def full_type_name(val, name): (source)

Compute complete type parameter name with scope. Args: val: The object with type parameters. name: The short type parameter name (e.g., T). Returns: The full type parameter name (e.g., List.T).

def get_annotations_dict(members): (source)

Get __annotations__ from a members map. Returns None rather than {} if the dict does not exist so that callers always have a reference to the actual dictionary, and can mutate it if needed. Args: members: A dict of member name to variable Returns: members['__annotations__'] unpacked as a python dict, or None

def get_atomic_python_constant(variable, constant_type=None): (source)

Get the concrete atomic Python value stored in this variable. This is used for things that are stored in cfg.Variable, but we need the actual data in order to proceed. E.g. function / class definitions. Args: variable: A cfg.Variable. It can only have one possible value. constant_type: Optionally, the required type of the constant. Returns: A Python constant. (Typically, a string, a tuple, or a code object.) Raises: ConversionError: If the value in this Variable is purely abstract, i.e. doesn't store a Python value, or if it has more than one possible value.

def get_atomic_value(variable, constant_type=None, default=_None()): (source)

Get the atomic value stored in this variable.

def get_dict_fullhash_component(vardict: Dict[str, cfg.Variable], *, names: Optional[Set[str]] = None, seen: Optional[Set[_BaseValueType]] = None) -> Tuple[Any, ...]: (source)

Hash a dictionary. This contains the keys and the full hashes of the data in the values. Arguments: vardict: A dictionary mapping str to Variable. names: If this is non-None, the snapshot will include only those dictionary entries whose keys appear in names. seen: Optionally, a set of seen values for recursion detection. Returns: A hashable tuple of the dictionary.

Gets the generic type of an abstract value. Args: val: The abstract value. Returns: The type of the value, with concrete type parameters replaced by TypeVars. For example, the generic type of `[0]` is `List[T]`.

def get_mro_bases(bases): (source)

Get bases for MRO computation.

def get_type_parameter_substitutions(val: _BaseValueType, type_params: Iterable[_TypeParamType]) -> Mapping[str, cfg.Variable]: (source)

Get values for type_params from val's type parameters.

def get_var_fullhash_component(var: cfg.Variable, seen: Optional[Set[_BaseValueType]] = None) -> Tuple[Any, ...]: (source)

Undocumented

def get_views(variables, node): (source)

Get all possible views of the given variables at a particular node. For performance reasons, this method uses node.CanHaveCombination for filtering. For a more precise check, you can call node.HasCombination(list(view.values())). Do so judiciously, as the latter method can be very slow. This function can be used either as a regular generator or in an optimized way to yield only functionally unique views: views = get_views(...) skip_future = None while True: try: view = views.send(skip_future) except StopIteration: break ... The caller should set `skip_future` to True when it is safe to skip equivalent future views and False otherwise. Args: variables: The variables. node: The node. Yields: A datatypes.AcessTrackingDict mapping variables to bindings.

def is_callable(value: _BaseValueType) -> bool: (source)

Returns whether 'value' is a callable.

def is_concrete_dict(val: _BaseValueType) -> bool: (source)

Undocumented

def is_concrete_list(val: _BaseValueType) -> bool: (source)

Undocumented

def is_ellipsis(val): (source)

Undocumented

def is_generic_protocol(val: _BaseValueType) -> bool: (source)

Undocumented

def is_indefinite_iterable(val: _BaseValueType) -> bool: (source)

True if val is a non-concrete instance of typing.Iterable.

def is_recursive_annotation(annot): (source)

Undocumented

def is_subclass(binding, cls): (source)

Wehther binding.data is a subclass of cls, modulo parameterization.

def is_var_indefinite_iterable(var): (source)

True if all bindings of var are indefinite sequences.

def is_var_splat(var): (source)

Undocumented

def match_atomic_python_constant(variable, typ=None): (source)

Undocumented

def match_atomic_value(variable, typ=None): (source)

Undocumented

def match_type_container(typ, container_type_name: Union[str, Tuple[str, ...]]): (source)

Unpack the type parameter from ContainerType[T].

def maybe_extract_tuple(t): (source)

Returns a tuple of Variables.

def maybe_unwrap_decorated_function(func): (source)

Undocumented

def merged_type_parameter(node, var, param): (source)

Undocumented

def parse_formal_type_parameters(base, prefix, formal_type_parameters, container=None): (source)

Parse type parameters from base class. Args: base: base class. prefix: the full name of subclass of base class. formal_type_parameters: the mapping of type parameter name to its type. container: An abstract value whose class template is used when prefix=None to decide how to handle type parameters that are aliased to other type parameters. Values that are in the class template are kept, while all others are ignored. Raises: GenericTypeError: If the lazy types of type parameter don't match

def show_constant(val: _BaseValueType) -> str: (source)

Pretty-print a value if it is a constant. Recurses into a constant, printing the underlying Python value for constants and just using "..." for everything else (e.g., Variables). This is useful for generating clear error messages that show the exact values related to an error while preventing implementation details from leaking into the message. Args: val: an abstract value. Returns: A string of the pretty-printed constant.

def simplify_variable(var, node, ctx): (source)

Deduplicates identical data in `var`.

def unwrap_final(val): (source)

Unwrap Final[T] -> T.

def unwrap_splat(var): (source)

Undocumented

def update_args_dict(args: _ArgsDictType, update: _ArgsDictType, node: cfg.CFGNode): (source)

Update a {str: Variable} dict by merging bindings.

def with_empty_substitutions(subst, pytd_type, node, ctx): (source)

Undocumented

Undocumented

Value
'_ARGS'
CLASS_LEVEL_IGNORE = (source)

Undocumented

Value
frozenset(set(['__builtins__',
               '__class__',
               '__module__',
               '__name__',
               '__qualname__',
               '__slots__',
               '__annotations__']))
DUMMY_CONTAINER = (source)

Undocumented

Value
object()
DYNAMIC_ATTRIBUTE_MARKERS: list[str] = (source)

Undocumented

Value
['HAS_DYNAMIC_ATTRIBUTES', '_HAS_DYNAMIC_ATTRIBUTES', 'has_dynamic_attributes']

Undocumented

Value
'_K'

Undocumented

Value
'_RET'

Undocumented

Value
'_T'

Undocumented

Value
'_T2'
TOP_LEVEL_IGNORE = (source)

Undocumented

Value
frozenset(set(['__builtins__',
               '__doc__',
               '__file__',
               '__future__',
               '__module__',
               '__name__',
               '__annotations__']))

Undocumented

Value
'_V'

Undocumented

def _isinstance(obj, name_or_names): (source)

Do an isinstance() call for a class defined in pytype.abstract. Args: obj: An instance. name_or_names: A name or tuple of names of classes in pytype.abstract. Returns: Whether obj is an instance of name_or_names.

def _make(cls_name, *args, **kwargs): (source)

Make an instance of cls_name with the given arguments.

def _merge_type(t0, t1, name, cls): (source)

Merge two types. Rules: Type `Any` can match any type, we will return the other type if one of them is `Any`. Return the sub-class if the types have inheritance relationship. Args: t0: The first type. t1: The second type. name: Type parameter name. cls: The class_mixin.Class on which any error should be reported. Returns: A type. Raises: GenericTypeError: if the types don't match.

_ArgsDictType = (source)

Undocumented

Value
Dict[str, cfg.Variable]