module documentation

Converts pyi files to pickled asts and saves them to disk. Used to speed up module importing. This is done by loading the ast and serializing it to disk. Further users only need to read the serialized data from disk, which is faster to digest than a pyi file.

Class FindClassTypesVisitor Visitor to find class and function types.
Class SerializableAst The data pickled to disk to save an ast.
Class SerializableTupleClass Undocumented
Class UndoModuleAliasesVisitor Visitor to undo module aliases in late types.
Exception UnrestorableDependencyError If a dependency can't be restored in the current state.
Function EnsureAstName Verify that serializable_ast has the name module_name, or repair it.
Function FillLocalReferences Fill in local references.
Function PrepareForExport Prepare an ast as if it was parsed and loaded.
Function ProcessAst Postprocess a pickled ast.
Function SerializeAst Loads and stores an ast to disk.
Function SourceToExportableAst Parse the source code into a pickle-able ast.
Function _LookupClassReferences Fills .cls references in serializable_ast.ast with ones from module_map.
def EnsureAstName(ast, module_name, fix=False): (source)

Verify that serializable_ast has the name module_name, or repair it. Args: ast: An instance of SerializableAst. module_name: The name under which ast.ast should be loaded. fix: If this function should repair the wrong name. Returns: The updated SerializableAst.

def FillLocalReferences(serializable_ast, module_map): (source)

Fill in local references.

def PrepareForExport(module_name, ast, loader): (source)

Prepare an ast as if it was parsed and loaded. External dependencies will not be resolved, as the ast generated by this method is supposed to be exported. Args: module_name: The module_name as a string for the returned ast. ast: pytd.TypeDeclUnit, is only used if src is None. loader: A load_pytd.Loader instance. Returns: A pytd.TypeDeclUnit representing the supplied AST as it would look after being written to a file and parsed.

def ProcessAst(serializable_ast, module_map): (source)

Postprocess a pickled ast. Postprocessing will either just fill the ClassType references from module_map or if module_name changed between pickling and loading rename the module internal references to the new module_name. Renaming is more expensive than filling references, as the whole AST needs to be rebuild. Args: serializable_ast: A SerializableAst instance. module_map: Used to resolve ClassType.cls links to already loaded modules. The loaded module will be added to the dict. Returns: A pytd.TypeDeclUnit, this is either the input raw_ast with the references set or a newly created AST with the new module_name and the references set. Raises: AssertionError: If module_name is already in module_map, which means that module_name is already loaded. UnrestorableDependencyError: If no concrete module exists in module_map for one of the references from the pickled ast.

def SerializeAst(ast, src_path=None, metadata=None): (source)

Loads and stores an ast to disk. Args: ast: The pytd.TypeDeclUnit to save to disk. src_path: Optionally, the filepath of the original source file. metadata: A list of arbitrary string-encoded metadata. Returns: The SerializableAst derived from `ast`.

def SourceToExportableAst(module_name, src, loader): (source)

Parse the source code into a pickle-able ast.

def _LookupClassReferences(serializable_ast, module_map, self_name): (source)

Fills .cls references in serializable_ast.ast with ones from module_map. Already filled references are not changed. References to the module self._name are not filled. Setting self_name=None will fill all references. Args: serializable_ast: A SerializableAst instance. module_map: Used to resolve ClassType.cls links to already loaded modules. The loaded module will be added to the dict. self_name: A string representation of a module which should not be resolved, for example: "foo.bar.module1" or None to resolve all modules. Returns: A SerializableAst with an updated .ast. .class_type_nodes is set to None if any of the Nodes needed to be regenerated.