type_helpers

Gather all TypeVar and TypeAlias assignments in a module.

Attributes

TypeHelper

TypeHelpers

Statement

_mod

_code

Classes

GatherTypeHelpers

A class for tracking visited TypeVars and TypeAliases.

AddTypeHelpers

Visitor loosly based on AddImportsVisitor

Functions

is_TypeAlias(→ bool)

Annotated Assign - Foo:TypeAlias = ...

is_TypeVar(statement)

Assign - Foo = Typevar(...)

is_CONSTANT(statement)

Assign - FOO = ...

is_AnnCONSTANT(statement)

Assign - FOO = ...

is_ParamSpec(statement)

Assign - Foo = ParamSpec(...)

is_import(statement)

import - import foo

is_docstr(statement)

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_TypeAlias(statement) bool

Annotated Assign - Foo:TypeAlias = …

Return type:

bool

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.ContextAwareVisitor

A class for tracking visited TypeVars and TypeAliases.

Parameters:

context (libcst.codemod._context.CodemodContext)

all_typehelpers: Dict[Tuple[str, Ellipsis], TypeHelpers]
stack: List[str] = []
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

visit_Assign(node: libcst.Assign) None

Find all TypeVar assignments in the module. format: T = TypeVar(“T”, int, float, str, bytes, Tuple) or Constants

Parameters:

node (libcst.Assign)

Return type:

None

visit_AnnAssign(node: libcst.AnnAssign) None

” Find all TypeAlias assignments in the module. format: T: TypeAlias = str

Parameters:

node (libcst.AnnAssign)

Return type:

None

class type_helpers.AddTypeHelpers(context: libcst.codemod._context.CodemodContext)

Bases: libcst.codemod._visitor.ContextAwareTransformer

Visitor loosly based on AddImportsVisitor

Parameters:

context (libcst.codemod._context.CodemodContext)

CONTEXT_KEY = 'AddTypeHelpers'
new_typehelpers: Dict[Tuple[str, Ellipsis], TypeHelpers]
all_typehelpers: Dict[Tuple[str, Ellipsis], TypeHelpers]
stack: List[str] = []
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:

bool

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]]