module documentation

Utilities for pytd. This provides a utility function to access data files in a way that works either locally or within a larger repository.

Class OrderedSet A simple ordered set.
Class TypeBuilder Utility class for building union types.
Class TypeMatcher Base class for modules that match types against each other.
Function ASTdiff Undocumented
Function ASTeq Undocumented
Function CanonicalOrdering Convert a PYTD node to a canonical (sorted) ordering.
Function Concat Concatenate two or more pytd ASTs.
Function CreateModule Undocumented
Function DiffNamedPickles Diff two lists of (name, pickled_module).
Function disabled_function Disable a function.
Function DummyMethod Create a simple method using only "Any"s as types.
Function GetAllSubClasses Compute a class->subclasses mapping.
Function GetTypeParameters Undocumented
Function JoinTypes Combine a list of types into a union type, if needed.
Function MakeClassOrContainerType If we have type params, build a generic type, a normal type otherwise.
Function MakeTypeAnnotation Returns a type annotation and any added imports.
Function MatchesFullName Whether t.name matches full_name in format {module}.{member}.
Function MergeBaseClass Merge a base class into a subclass.
Function NamedOrClassType Create Classtype / NamedType.
Function NamedTypeWithModule Create NamedType, dotted if we have a module.
Function Print Undocumented
Function UnpackGeneric Undocumented
Function UnpackUnion Return the type list for union type, or a list with the type itself.
Function WrapTypeDeclUnit Given a list (classes, functions, etc.), wrap a pytd around them.
Constant ANON_PARAM Undocumented
Function _check_intersection Check for duplicate identifiers.
Constant _TUPLE_NAMES Undocumented
def ASTdiff(ast1, ast2): (source)

Undocumented

def ASTeq(ast1, ast2): (source)

Undocumented

def CanonicalOrdering(n): (source)

Convert a PYTD node to a canonical (sorted) ordering.

def Concat(*args, **kwargs): (source)

Concatenate two or more pytd ASTs.

def CreateModule(name='<empty>', **kwargs): (source)

Undocumented

def DiffNamedPickles(named_pickles1, named_pickles2): (source)

Diff two lists of (name, pickled_module).

def disabled_function(*unused_args, **unused_kwargs): (source)

Disable a function. Disable a previously defined function foo as follows: foo = disabled_function Any later calls to foo will raise an AssertionError. This is used, e.g., in cfg.Program to prevent the addition of more nodes after we have begun solving the graph. Raises: AssertionError: If something tried to call the disabled function.

def DummyMethod(name, *params): (source)

Create a simple method using only "Any"s as types. Arguments: name: The name of the method *params: The parameter names. Returns: A pytd.Function.

def GetAllSubClasses(ast): (source)

Compute a class->subclasses mapping. Args: ast: Parsed PYTD. Returns: A dictionary, mapping instances of pytd.Type (types) to lists of pytd.Class (the derived classes).

def GetTypeParameters(node): (source)

Undocumented

def JoinTypes(types): (source)

Combine a list of types into a union type, if needed. Leaves singular return values alone, or wraps a UnionType around them if there are multiple ones, or if there are no elements in the list (or only NothingType) return NothingType. Arguments: types: A list of types. This list might contain other UnionTypes. If so, they are flattened. Returns: A type that represents the union of the types passed in. Order is preserved.

def MakeClassOrContainerType(base_type, type_arguments, homogeneous): (source)

If we have type params, build a generic type, a normal type otherwise.

def MakeTypeAnnotation(ast, multiline_args=False): (source)

Returns a type annotation and any added imports.

def MatchesFullName(t, full_name, current_module_name=None, aliases=None): (source)

Whether t.name matches full_name in format {module}.{member}.

def MergeBaseClass(cls, base): (source)

Merge a base class into a subclass. Arguments: cls: The subclass to merge values into. pytd.Class. base: The superclass whose values will be merged. pytd.Class. Returns: a pytd.Class of the two merged classes.

def NamedOrClassType(name, cls): (source)

Create Classtype / NamedType.

def NamedTypeWithModule(name, module=None): (source)

Create NamedType, dotted if we have a module.

def Print(ast, multiline_args=False): (source)

Undocumented

def UnpackGeneric(t, basename): (source)

Undocumented

def UnpackUnion(t): (source)

Return the type list for union type, or a list with the type itself.

def WrapTypeDeclUnit(name, items): (source)

Given a list (classes, functions, etc.), wrap a pytd around them. Args: name: The name attribute of the resulting TypeDeclUnit. items: A list of items. Can contain pytd.Class, pytd.Function and pytd.Constant. Returns: A pytd.TypeDeclUnit. Raises: ValueError: In case of an invalid item in the list. NameError: For name conflicts.

ANON_PARAM = (source)

Undocumented

Value
re.compile(r'_[0-9]+')
def _check_intersection(items1, items2, name1, name2): (source)

Check for duplicate identifiers.

_TUPLE_NAMES: tuple[str, ...] = (source)

Undocumented

Value
('builtins.tuple', 'typing.Tuple')