stubber.typing_collector

helper functions for stub transformations

Attributes

MODULE_KEY

MOD_DOCSTR_KEY

_code

Exceptions

TransformError

Error raised upon encountering a known error while attempting to transform

Classes

TypeInfo

contains the functionDefs and classDefs info read from the stubs source

AnnoValue

The different values for the annotations

StubTypingCollector

Collect the function/method and class definitions from ta rich .py or .pyi source

Functions

is_property(→ bool)

check if the function is a property

is_setter(→ bool)

check if the function is a setter

is_getter(→ bool)

check if the function is a getter

is_deleter(→ bool)

check if the function is a deleter

update_def_docstr(→ Any)

Update the docstring of a function/method or class

update_module_docstr(→ Any)

Add or update the docstring of a module

Module Contents

class stubber.typing_collector.TypeInfo

contains the functionDefs and classDefs info read from the stubs source

name: str
decorators: Sequence[libcst.Decorator]
params: libcst.Parameters | None = None
returns: libcst.Annotation | None = None
docstr_node: libcst.SimpleStatementLine | None = None
def_node: libcst.FunctionDef | libcst.ClassDef | None = None
def_type: str = '?'
class stubber.typing_collector.AnnoValue

The different values for the annotations

docstring: str | None = ''

Module docstring or function/method docstring

docstring_node: libcst.SimpleStatementLine | None = None

the docstring node for a function method to reuse with overloads

type_info: TypeInfo | None = None

function/method or class definition read from the docstub source

overloads: List[TypeInfo] = []

function / method overloads read from the docstub source

mp_available: List[TypeInfo] = []

function / method overloads read from the docstub source

literal_docstrings: Dict[str, libcst.SimpleStatementLine]

literal/constant name -> docstring node mappings for literal docstrings

exception stubber.typing_collector.TransformError

Bases: Exception

Error raised upon encountering a known error while attempting to transform the tree.

stubber.typing_collector.MODULE_KEY = ('__module',)
stubber.typing_collector.MOD_DOCSTR_KEY = ('__module_docstring',)
stubber.typing_collector._code
stubber.typing_collector.is_property(node: libcst.FunctionDef) bool

check if the function is a property

Parameters:

node (libcst.FunctionDef)

Return type:

bool

stubber.typing_collector.is_setter(node: libcst.FunctionDef) bool

check if the function is a setter

Parameters:

node (libcst.FunctionDef)

Return type:

bool

stubber.typing_collector.is_getter(node: libcst.FunctionDef) bool

check if the function is a getter

Parameters:

node (libcst.FunctionDef)

Return type:

bool

stubber.typing_collector.is_deleter(node: libcst.FunctionDef) bool

check if the function is a deleter

Parameters:

node (libcst.FunctionDef)

Return type:

bool

class stubber.typing_collector.StubTypingCollector

Bases: libcst.CSTVisitor

Collect the function/method and class definitions from ta rich .py or .pyi source

stack: List[str] = []
annotations: Dict[Tuple[str, Ellipsis], AnnoValue]
comments: List[str] = []
_collect_literal_docstrings(body: Sequence[libcst.BaseStatement]) Dict[str, libcst.SimpleStatementLine]

Collect literal docstrings from a sequence of statements. Looks for patterns like: CONSTANT = value ‘’’docstring for constant’’’

Parameters:

body (Sequence[libcst.BaseStatement])

Return type:

Dict[str, libcst.SimpleStatementLine]

visit_Module(node: libcst.Module) bool

Store the module docstring and collect literal docstrings

Parameters:

node (libcst.Module)

Return type:

bool

visit_Comment(node: libcst.Comment) None

connect comments from the source

Parameters:

node (libcst.Comment)

Return type:

None

visit_ClassDef(node: libcst.ClassDef) bool | None

collect info from a classdef: - name, decorators, docstring - class-level literal docstrings

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_FunctionDef(node: libcst.FunctionDef) bool | None

collect info from a function/method - name, decorators, params, returns, docstring

Parameters:

node (libcst.FunctionDef)

Return type:

Optional[bool]

update_append_first_node(node)

Store the function/method docstring or function/method sig

leave_FunctionDef(original_node: libcst.FunctionDef) None

remove the function/method name from the stack

Parameters:

original_node (libcst.FunctionDef)

Return type:

None

stubber.typing_collector.update_def_docstr(dest_node: libcst.FunctionDef | libcst.ClassDef, src_docstr: libcst.SimpleStatementLine | str | None = None, src_node=None) Any

Update the docstring of a function/method or class The supplied src_docstr can be a string or a SimpleStatementLine

for function defs ending in an ellipsis, the entire body needs to be replaced. in this case src_node is required.

Parameters:
  • dest_node (Union[libcst.FunctionDef, libcst.ClassDef])

  • src_docstr (Optional[Union[libcst.SimpleStatementLine, str]])

Return type:

Any

stubber.typing_collector.update_module_docstr(node: libcst.Module, doc_tree: str | libcst.SimpleStatementLine | libcst.BaseCompoundStatement | None) Any

Add or update the docstring of a module

Parameters:
  • node (libcst.Module)

  • doc_tree (Optional[Union[str, libcst.SimpleStatementLine, libcst.BaseCompoundStatement]])

Return type:

Any