class documentation

class CombineContainers(visitors.Visitor): (source)

View In Hierarchy

Change unions of containers to containers of unions. For example, this transforms Union[list[int], list[float]] to list[Union[int, float]] .

Method VisitUnionType Push unions down into containers.
Method _key Undocumented
Method _should_merge Determine whether pytd_type values in the union should be merged.
Constant _CONTAINER_NAMES Undocumented

Inherited from Visitor:

Method __init__ Undocumented
Method Enter Undocumented
Method Leave Undocumented
Method Visit Undocumented
Class Variable old_node Undocumented
Class Variable unchecked_node_names Undocumented
Class Variable visits_all_node_types Undocumented
Instance Variable enter_functions Undocumented
Instance Variable leave_functions Undocumented
Instance Variable visit_class_names Undocumented
Instance Variable visit_functions Undocumented
Class Variable _visitor_functions_cache Undocumented
def VisitUnionType(self, union): (source)

Push unions down into containers. This collects similar container types in unions and merges them into single instances with the union type pushed down to the element_type level. Arguments: union: A pytd.Union instance. Might appear in a parameter, a return type, a constant type, etc. Returns: A simplified pytd.Union.

def _key(self, t): (source)

Undocumented

def _should_merge(self, pytd_type, union): (source)

Determine whether pytd_type values in the union should be merged. If the union contains the homogeneous flavor of pytd_type (e.g., GenericType(base_type=tuple) when pytd_type is TupleType), or pytd_type values of different lengths, we want to turn all of the pytd_type values into homogeneous ones so that they can be merged into a single container. Args: pytd_type: The pytd type, either TupleType or CallableType. union: a pytd.UnionType Returns: True if the pytd_type values should be merged, False otherwise.

_CONTAINER_NAMES = (source)

Undocumented

Value
{pytd.TupleType: ('builtins.tuple', 'typing.Tuple'),
 pytd.CallableType: ('typing.Callable')}