type_helpers¶
Gather all TypeVar and TypeAlias assignments in a module.
Attributes¶
Classes¶
A class for tracking visited TypeVars and TypeAliases. |
|
Visitor loosly based on AddImportsVisitor |
Functions¶
|
Annotated Assign - Foo:TypeAlias = ... |
|
Assign - Foo = Typevar(...) |
|
Assign - FOO = ... |
|
Assign - FOO = ... |
|
Assign - Foo = ParamSpec(...) |
|
import - import foo |
|
single or triple quoted string |
Module Contents¶
- type type_helpers.TypeHelper = cst.Assign | cst.AnnAssign¶
- type type_helpers.TypeHelpers = List[TypeHelper]¶
- type type_helpers.Statement = cst.SimpleStatementLine | cst.BaseCompoundStatement¶
- type_helpers._mod¶
- type_helpers._code¶
- type_helpers.is_TypeVar(statement)¶
Assign - Foo = Typevar(…)
- type_helpers.is_CONSTANT(statement)¶
Assign - FOO = … AnnAssign- FOO:bool = …
- type_helpers.is_AnnCONSTANT(statement)¶
Assign - FOO = … AnnAssign- FOO:bool = …
- type_helpers.is_ParamSpec(statement)¶
Assign - Foo = ParamSpec(…)
- type_helpers.is_import(statement)¶
import - import foo
- type_helpers.is_docstr(statement)¶
single or triple quoted string
- class type_helpers.GatherTypeHelpers(context: libcst.codemod._context.CodemodContext)¶
Bases:
libcst.codemod._visitor.ContextAwareVisitorA class for tracking visited TypeVars and TypeAliases.
- Parameters:
context (libcst.codemod._context.CodemodContext)
- visit_ClassDef(node: libcst.ClassDef) bool | None¶
keep track of the the (class, method) names to the stack
- Parameters:
node (libcst.ClassDef)
- Return type:
Optional[bool]
- leave_ClassDef(original_node: libcst.ClassDef) None¶
remove the class name from the stack
- Parameters:
original_node (libcst.ClassDef)
- Return type:
None
- class type_helpers.AddTypeHelpers(context: libcst.codemod._context.CodemodContext)¶
Bases:
libcst.codemod._visitor.ContextAwareTransformerVisitor loosly based on AddImportsVisitor
- Parameters:
context (libcst.codemod._context.CodemodContext)
- CONTEXT_KEY = 'AddTypeHelpers'¶
- classmethod add_helpers(context: libcst.codemod._context.CodemodContext, helpers: Dict[Tuple[str, Ellipsis], TypeHelpers])¶
- Parameters:
context (libcst.codemod._context.CodemodContext)
helpers (Dict[Tuple[str, Ellipsis], TypeHelpers])
- static skip_first(body: Sequence[libcst.BaseStatement]) bool¶
- Parameters:
body (Sequence[libcst.BaseStatement])
- Return type:
- leave_Module(original_node: libcst.Module, updated_node: libcst.Module) libcst.Module¶
- Parameters:
original_node (libcst.Module)
updated_node (libcst.Module)
- Return type:
libcst.Module
- update_body(body: Sequence[libcst.BaseStatement], stack_id)¶
- Parameters:
body (Sequence[libcst.BaseStatement])
- visit_ClassDef(node: libcst.ClassDef) bool | None¶
keep track of the the (class, method) names to the stack
- Parameters:
node (libcst.ClassDef)
- Return type:
Optional[bool]
- leave_ClassDef(original_node: libcst.ClassDef, updated_node: libcst.ClassDef) libcst.ClassDef¶
- Parameters:
original_node (libcst.ClassDef)
updated_node (libcst.ClassDef)
- Return type:
libcst.ClassDef
- _split_body(body: Sequence[libcst.BaseStatement]) Tuple[Sequence[libcst.BaseStatement], Sequence[libcst.BaseStatement], Sequence[libcst.BaseStatement]]¶
Split the module into 3 parts: - before any TypeAlias, TypeVar or ParamSpec statements - the TypeAlias and TypeVar statements - the rest of the module after the TypeAlias and TypeVar statements
- Parameters:
body (Sequence[libcst.BaseStatement])
- Return type:
Tuple[Sequence[libcst.BaseStatement], Sequence[libcst.BaseStatement], Sequence[libcst.BaseStatement]]