type_helpers ============ .. py:module:: type_helpers .. autoapi-nested-parse:: Gather all TypeVar and TypeAlias assignments in a module. Attributes ---------- .. autoapisummary:: type_helpers.TypeHelper type_helpers.TypeHelpers type_helpers.Statement type_helpers._mod type_helpers._code Classes ------- .. autoapisummary:: type_helpers.GatherTypeHelpers type_helpers.AddTypeHelpers Functions --------- .. autoapisummary:: type_helpers.is_TypeAlias type_helpers.is_TypeVar type_helpers.is_CONSTANT type_helpers.is_AnnCONSTANT type_helpers.is_ParamSpec type_helpers.is_import type_helpers.is_docstr Module Contents --------------- .. py:type:: TypeHelper :canonical: Union[cst.Assign, cst.AnnAssign] .. py:type:: TypeHelpers :canonical: List[TypeHelper] .. py:type:: Statement :canonical: Union[cst.SimpleStatementLine, cst.BaseCompoundStatement] .. py:data:: _mod .. py:data:: _code .. py:function:: is_TypeAlias(statement) -> bool Annotated Assign - Foo:TypeAlias = ... .. py:function:: is_TypeVar(statement) Assign - Foo = Typevar(...) .. py:function:: is_CONSTANT(statement) Assign - FOO = ... AnnAssign- FOO:bool = ... .. py:function:: is_AnnCONSTANT(statement) Assign - FOO = ... AnnAssign- FOO:bool = ... .. py:function:: is_ParamSpec(statement) Assign - Foo = ParamSpec(...) .. py:function:: is_import(statement) import - import foo .. py:function:: is_docstr(statement) single or triple quoted string .. py:class:: GatherTypeHelpers(context: libcst.codemod._context.CodemodContext) Bases: :py:obj:`libcst.codemod._visitor.ContextAwareVisitor` A class for tracking visited TypeVars and TypeAliases. .. py:attribute:: all_typehelpers :type: Dict[Tuple[str, Ellipsis], TypeHelpers] .. py:attribute:: stack :type: List[str] :value: [] .. py:method:: visit_ClassDef(node: libcst.ClassDef) -> Optional[bool] keep track of the the (class, method) names to the stack .. py:method:: leave_ClassDef(original_node: libcst.ClassDef) -> None remove the class name from the stack .. py:method:: visit_Assign(node: libcst.Assign) -> None Find all TypeVar assignments in the module. format: T = TypeVar("T", int, float, str, bytes, Tuple) or Constants .. py:method:: visit_AnnAssign(node: libcst.AnnAssign) -> None " Find all TypeAlias assignments in the module. format: T: TypeAlias = str .. py:class:: AddTypeHelpers(context: libcst.codemod._context.CodemodContext) Bases: :py:obj:`libcst.codemod._visitor.ContextAwareTransformer` Visitor loosly based on AddImportsVisitor .. py:attribute:: CONTEXT_KEY :value: 'AddTypeHelpers' .. py:attribute:: new_typehelpers :type: Dict[Tuple[str, Ellipsis], TypeHelpers] .. py:attribute:: all_typehelpers :type: Dict[Tuple[str, Ellipsis], TypeHelpers] .. py:attribute:: stack :type: List[str] :value: [] .. py:method:: add_helpers(context: libcst.codemod._context.CodemodContext, helpers: Dict[Tuple[str, Ellipsis], TypeHelpers]) :classmethod: .. py:method:: skip_first(body: Sequence[libcst.BaseStatement]) -> bool :staticmethod: .. py:method:: leave_Module(original_node: libcst.Module, updated_node: libcst.Module) -> libcst.Module .. py:method:: update_body(body: Sequence[libcst.BaseStatement], stack_id) .. py:method:: visit_ClassDef(node: libcst.ClassDef) -> Optional[bool] keep track of the the (class, method) names to the stack .. py:method:: leave_ClassDef(original_node: libcst.ClassDef, updated_node: libcst.ClassDef) -> libcst.ClassDef .. py:method:: _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