visitors._apply_stubber_annotations

Module Contents

Classes

FunctionAnnotation

TypeCollector

Collect type annotations from a stub module.

Annotations

ApplyStubberAnnotationsVisitor

Apply type annotations to a source module using the given stub modules.

Functions

_get_import_alias_names(import_aliases: Sequence[libcst.ImportAlias]) → Set[str]

_get_import_names(imports: Sequence[Union[libcst.Import, libcst.ImportFrom]]) → Set[str]

visitors._apply_stubber_annotations._get_import_alias_names(import_aliases: Sequence[libcst.ImportAlias]) Set[str]
Parameters

import_aliases (Sequence[libcst.ImportAlias]) –

Return type

Set[str]

visitors._apply_stubber_annotations._get_import_names(imports: Sequence[Union[libcst.Import, libcst.ImportFrom]]) Set[str]
Parameters

imports (Sequence[Union[libcst.Import, libcst.ImportFrom]]) –

Return type

Set[str]

class visitors._apply_stubber_annotations.FunctionAnnotation
parameters :libcst.Parameters
returns :Optional[libcst.Annotation]
class visitors._apply_stubber_annotations.TypeCollector(existing_imports: Set[str], context: libcst.codemod._context.CodemodContext)

Bases: libcst.CSTVisitor

Collect type annotations from a stub module.

Parameters
  • existing_imports (Set[str]) –

  • context (libcst.codemod._context.CodemodContext) –

visit_ClassDef(self, node: libcst.ClassDef) None
Parameters

node (libcst.ClassDef) –

Return type

None

leave_ClassDef(self, original_node: libcst.ClassDef) None
Parameters

original_node (libcst.ClassDef) –

Return type

None

visit_FunctionDef(self, node: libcst.FunctionDef) bool
Parameters

node (libcst.FunctionDef) –

Return type

bool

leave_FunctionDef(self, original_node: libcst.FunctionDef) None
Parameters

original_node (libcst.FunctionDef) –

Return type

None

visit_AnnAssign(self, node: libcst.AnnAssign) bool
Parameters

node (libcst.AnnAssign) –

Return type

bool

leave_AnnAssign(self, original_node: libcst.AnnAssign) None
Parameters

original_node (libcst.AnnAssign) –

Return type

None

visit_ImportFrom(self, node: libcst.ImportFrom) None
Parameters

node (libcst.ImportFrom) –

Return type

None

_add_annotation_to_imports(self, annotation: libcst.Attribute) Union[libcst.Name, libcst.Attribute]
Parameters

annotation (libcst.Attribute) –

Return type

Union[libcst.Name, libcst.Attribute]

_handle_Index(self, slice: libcst.Index, node: libcst.Subscript) libcst.Subscript
Parameters
  • slice (libcst.Index) –

  • node (libcst.Subscript) –

Return type

libcst.Subscript

_handle_Subscript(self, node: libcst.Subscript) libcst.Subscript
Parameters

node (libcst.Subscript) –

Return type

libcst.Subscript

_create_import_from_annotation(self, returns: libcst.Annotation) libcst.Annotation
Parameters

returns (libcst.Annotation) –

Return type

libcst.Annotation

_import_parameter_annotations(self, parameters: libcst.Parameters) libcst.Parameters
Parameters

parameters (libcst.Parameters) –

Return type

libcst.Parameters

class visitors._apply_stubber_annotations.Annotations
function_annotations :Dict[str, FunctionAnnotation]
attribute_annotations :Dict[str, libcst.Annotation]
class_definitions :Dict[str, libcst.ClassDef]
class visitors._apply_stubber_annotations.ApplyStubberAnnotationsVisitor(context: libcst.codemod._context.CodemodContext, annotations: Optional[Annotations] = None, overwrite_existing_annotations: bool = False)

Bases: libcst.codemod._visitor.ContextAwareTransformer

Apply type annotations to a source module using the given stub modules. You can also pass in explicit annotations for functions and attributes and pass in new class definitions that need to be added to the source module.

This is one of the transforms that is available automatically to you when running a codemod. To use it in this manner, import ApplyStubberAnnotationsVisitor and then call the static store_stub_in_context() method, giving it the current context (found as self.context for all subclasses of Codemod), the stub module from which you wish to add annotations.

For example, you can store the type annotation int for x using:

stub_module = parse_module("x: int = ...")

ApplyStubberAnnotationsVisitor.store_stub_in_context(self.context, stub_module)

You can apply the type annotation using:

source_module = parse_module("x = 1")
ApplyStubberAnnotationsVisitor.transform_module(source_module)

This will produce the following code:

x: int = 1

If the function or attribute already has a type annotation, it will not be overwritten.

To overwrite existing annotations when applying annotations from a stub, use the keyword argument overwrite_existing_annotations=True when constructing the codemod or when calling store_stub_in_context.

Parameters
  • context (libcst.codemod._context.CodemodContext) –

  • annotations (Optional[Annotations]) –

  • overwrite_existing_annotations (bool) –

CONTEXT_KEY = ApplyStubberAnnotationsVisitor
static store_stub_in_context(context: libcst.codemod._context.CodemodContext, stub: libcst.Module, overwrite_existing_annotations: bool = False) None

Store a stub module in the CodemodContext so that type annotations from the stub can be applied in a later invocation of this class.

If the overwrite_existing_annotations flag is True, the codemod will overwrite any existing annotations.

If you call this function multiple times, only the last values of stub and overwrite_existing_annotations will take effect.

Parameters
  • context (libcst.codemod._context.CodemodContext) –

  • stub (libcst.Module) –

  • overwrite_existing_annotations (bool) –

Return type

None

transform_module_impl(self, tree: libcst.Module) libcst.Module

Collect type annotations from all stubs and apply them to tree.

Gather existing imports from tree so that we don’t add duplicate imports.

Parameters

tree (libcst.Module) –

Return type

libcst.Module

_qualifier_name(self) str
Return type

str

_annotate_single_target(self, node: libcst.Assign, updated_node: libcst.Assign) Union[libcst.Assign, libcst.AnnAssign]
Parameters
  • node (libcst.Assign) –

  • updated_node (libcst.Assign) –

Return type

Union[libcst.Assign, libcst.AnnAssign]

_split_module(self, module: libcst.Module, updated_module: libcst.Module) Tuple[List[Union[libcst.SimpleStatementLine, libcst.BaseCompoundStatement]], List[Union[libcst.SimpleStatementLine, libcst.BaseCompoundStatement]]]
Parameters
  • module (libcst.Module) –

  • updated_module (libcst.Module) –

Return type

Tuple[List[Union[libcst.SimpleStatementLine, libcst.BaseCompoundStatement]], List[Union[libcst.SimpleStatementLine, libcst.BaseCompoundStatement]]]

_add_to_toplevel_annotations(self, name: str) None
Parameters

name (str) –

Return type

None

_update_parameters(self, annotations: FunctionAnnotation, updated_node: libcst.FunctionDef) libcst.Parameters
Parameters
Return type

libcst.Parameters

_insert_empty_line(self, statements: List[Union[libcst.SimpleStatementLine, libcst.BaseCompoundStatement]]) List[Union[libcst.SimpleStatementLine, libcst.BaseCompoundStatement]]
Parameters

statements (List[Union[libcst.SimpleStatementLine, libcst.BaseCompoundStatement]]) –

Return type

List[Union[libcst.SimpleStatementLine, libcst.BaseCompoundStatement]]

visit_ClassDef(self, node: libcst.ClassDef) None
Parameters

node (libcst.ClassDef) –

Return type

None

leave_ClassDef(self, original_node: libcst.ClassDef, updated_node: libcst.ClassDef) libcst.ClassDef
Parameters
  • original_node (libcst.ClassDef) –

  • updated_node (libcst.ClassDef) –

Return type

libcst.ClassDef

visit_FunctionDef(self, node: libcst.FunctionDef) bool
Parameters

node (libcst.FunctionDef) –

Return type

bool

leave_FunctionDef(self, original_node: libcst.FunctionDef, updated_node: libcst.FunctionDef) libcst.FunctionDef
Parameters
  • original_node (libcst.FunctionDef) –

  • updated_node (libcst.FunctionDef) –

Return type

libcst.FunctionDef

leave_Assign(self, original_node: libcst.Assign, updated_node: libcst.Assign) Union[libcst.Assign, libcst.AnnAssign]
Parameters
  • original_node (libcst.Assign) –

  • updated_node (libcst.Assign) –

Return type

Union[libcst.Assign, libcst.AnnAssign]

leave_ImportFrom(self, original_node: libcst.ImportFrom, updated_node: libcst.ImportFrom) libcst.ImportFrom
Parameters
  • original_node (libcst.ImportFrom) –

  • updated_node (libcst.ImportFrom) –

Return type

libcst.ImportFrom

leave_Module(self, original_node: libcst.Module, updated_node: libcst.Module) libcst.Module
Parameters
  • original_node (libcst.Module) –

  • updated_node (libcst.Module) –

Return type

libcst.Module