module documentation

Parsing/inferring signatures from documentation. This module provides several functions to generate better stubs using docstrings and Sphinx docs (.rst files).

Class ArgSig Signature info for a single argument.
Class DocStringParser Parse function signatures in documentation.
Class FunctionSig Undocumented
Function build_signature Build function signature from lists of positional and optional argument names.
Function find_unique_signatures Remove names with duplicate found signatures.
Function infer_arg_sig_from_anon_docstring Convert signature in form of "(self: TestClass, arg0: str='ada')" to List[TypedArgList].
Function infer_prop_type_from_docstring Check for Google/Numpy style docstring type annotation for a property.
Function infer_ret_type_sig_from_anon_docstring Convert signature in form of "(self: TestClass, arg0) -> int" to their return type.
Function infer_ret_type_sig_from_docstring Convert signature in form of "func(self: TestClass, arg0) -> int" to their return type.
Function infer_sig_from_docstring Convert function signature to list of TypedFunctionSig
Function is_valid_type Try to determine whether a string might be a valid type annotation.
Function parse_all_signatures Parse all signatures in a given reST document.
Function parse_signature Split function signature into its name, positional an optional arguments.
Constant STATE_ARGUMENT_DEFAULT Undocumented
Constant STATE_ARGUMENT_LIST Undocumented
Constant STATE_ARGUMENT_TYPE Undocumented
Constant STATE_FUNCTION_NAME Undocumented
Constant STATE_INIT Undocumented
Constant STATE_OPEN_BRACKET Undocumented
Constant STATE_RETURN_VALUE Undocumented
Type Alias Sig Undocumented
Constant _ARG_NAME_RE Undocumented
Constant _TYPE_RE Undocumented
def build_signature(positional: Sequence[str], optional: Sequence[str]) -> str: (source)

Build function signature from lists of positional and optional argument names.

def find_unique_signatures(sigs: Sequence[Sig]) -> list[Sig]: (source)

Remove names with duplicate found signatures.

def infer_arg_sig_from_anon_docstring(docstr: str) -> list[ArgSig]: (source)

Convert signature in form of "(self: TestClass, arg0: str='ada')" to List[TypedArgList].

def infer_prop_type_from_docstring(docstr: str|None) -> str|None: (source)

Check for Google/Numpy style docstring type annotation for a property. The docstring has the format "<type>: <descriptions>". In the type string, we allow the following characters: * dot: because sometimes classes are annotated using full path * brackets: to allow type hints like List[int] * comma/space: things like Tuple[int, int]

def infer_ret_type_sig_from_anon_docstring(docstr: str) -> str|None: (source)

Convert signature in form of "(self: TestClass, arg0) -> int" to their return type.

def infer_ret_type_sig_from_docstring(docstr: str, name: str) -> str|None: (source)

Convert signature in form of "func(self: TestClass, arg0) -> int" to their return type.

def infer_sig_from_docstring(docstr: str|None, name: str) -> list[FunctionSig]|None: (source)

Convert function signature to list of TypedFunctionSig Look for function signatures of function in docstring. Signature is a string of the format <function_name>(<signature>) -> <return type> or perhaps without the return type. Returns empty list, when no signature is found, one signature in typical case, multiple signatures, if docstring specifies multiple signatures for overload functions. Return None if the docstring is empty. Arguments: * docstr: docstring * name: name of function for which signatures are to be found

def is_valid_type(s: str) -> bool: (source)

Try to determine whether a string might be a valid type annotation.

def parse_all_signatures(lines: Sequence[str]) -> tuple[list[Sig], list[Sig]]: (source)

Parse all signatures in a given reST document. Return lists of found signatures for functions and classes.

def parse_signature(sig: str) -> tuple[str, list[str], list[str]]|None: (source)

Split function signature into its name, positional an optional arguments. The expected format is "func_name(arg, opt_arg=False)". Return the name of function and lists of positional and optional argument names.

STATE_ARGUMENT_DEFAULT: int = (source)

Undocumented

Value
5
STATE_ARGUMENT_LIST: int = (source)

Undocumented

Value
3
STATE_ARGUMENT_TYPE: int = (source)

Undocumented

Value
4
STATE_FUNCTION_NAME: int = (source)

Undocumented

Value
2
STATE_INIT: int = (source)

Undocumented

Value
1
STATE_OPEN_BRACKET: int = (source)

Undocumented

Value
7
STATE_RETURN_VALUE: int = (source)

Undocumented

Value
6
Sig: _TypeAlias = (source)

Undocumented

Value
Tuple[str, str]
_ARG_NAME_RE = (source)

Undocumented

Value
re.compile(r'\**[A-Za-z_][A-Za-z0-9_]*$')
_TYPE_RE = (source)

Undocumented

Value
re.compile(r'^[a-zA-Z_][\w\[\], ]*(\.[a-zA-Z_][\w\[\], ]*)*$')