stubalyzer.analyze module

class stubalyzer.analyze.CheckStyleWriter(path_map: Dict[Union[mypy.nodes.Decorator, mypy.nodes.FuncDef, mypy.nodes.OverloadedFuncDef, mypy.nodes.Var, mypy.nodes.TypeInfo, mypy.nodes.TypeVarExpr, mypy.nodes.TypeAlias], str])[source]

Bases: object

build_tree() ElementTree[source]
collect_error(*messages: str, sep: str = '', symbol: Union[mypy.nodes.Decorator, mypy.nodes.FuncDef, mypy.nodes.OverloadedFuncDef, mypy.nodes.Var, mypy.nodes.TypeInfo, mypy.nodes.TypeVarExpr, mypy.nodes.TypeAlias]) None[source]
class stubalyzer.analyze.ErrorEntry(symbol, message)[source]

Bases: tuple

property message

Alias for field number 1

property symbol

Alias for field number 0

class stubalyzer.analyze.EvaluationResult(value)[source]

Bases: Enum

An enumeration.

EXPECTED_FAILURE = 'expected_failure'
FAILURE = 'failure'
SUCCESS = 'success'
stubalyzer.analyze.analyze_stubs(mypy_conf_path: str, base_stubs_path: str, reference_stubs_path: Optional[str] = None, expected_mismatches_path: Optional[str] = None, checkstyle_report: Optional[str] = None, silent: bool = False, include_private: bool = False) bool[source]

Determine if the (presumably) handwritten stubs in base_stubs_path are correct; i.e. if they match the API of the modules that they are stubbing.

For this they are compared to reference stubs, which by default are generated with mypy’s stubgen tool. For each type mismatch (e.g. different function signature, missing class member) a message will be printed to stdout. The function will return False if any mismatches are found, unless they have been declared as expected.

Parameters
  • mypy_conf_path – path to mypy.ini

  • base_stubs_path – path to the directory that contains the stubs to analyze

  • reference_stubs_path – Path to the folder that contains the reference stubs. If not provided mypy’s stubgen tool will be used to generate them.

  • expected_mismatches_path

    Path to JSON file that defines expected mismatches. Example:

    {
        "my.module.function": "mismatch",
        "another.module.Class": "not_found"
    }
    

  • checkstyle_report – if this path is given, a xml report in checkstyle format will be written.

  • silent – Suppress all non-error output.

  • include_private – Call stubgen with –include-private.

Returns

True if the stubs in base_stubs_path are considered correct

stubalyzer.analyze.call_stubgen(command_line_args: List[str]) None[source]

Call stubgen like the command line tool.

Parameters

command_line_args – list of command line args

stubalyzer.analyze.compare(hand_written: Iterable[Union[mypy.nodes.Decorator, mypy.nodes.FuncDef, mypy.nodes.OverloadedFuncDef, mypy.nodes.Var, mypy.nodes.TypeInfo, mypy.nodes.TypeVarExpr, mypy.nodes.TypeAlias]], generated: Iterable[Union[mypy.nodes.Decorator, mypy.nodes.FuncDef, mypy.nodes.OverloadedFuncDef, mypy.nodes.Var, mypy.nodes.TypeInfo, mypy.nodes.TypeVarExpr, mypy.nodes.TypeAlias]]) Generator[ComparisonResult, None, None][source]

Compare hand written to generated stubs.

stubalyzer.analyze.evaluate_compare_result(compare_result: ComparisonResult, mismatches: Dict[str, MatchResult], mismatches_left: Set[str], expected_mismatches_path: Optional[str] = None, *, loggers: List[Callable[[...], None]]) EvaluationResult[source]
stubalyzer.analyze.generate_stub_types(base_stubs_path: str, mypy_conf_path: str, silent: bool = False, include_private: bool = False) Iterable[Tuple[Union[mypy.nodes.Decorator, mypy.nodes.FuncDef, mypy.nodes.OverloadedFuncDef, mypy.nodes.Var, mypy.nodes.TypeInfo, mypy.nodes.TypeVarExpr, mypy.nodes.TypeAlias], str]][source]

Use stubgen to generate reference stub types of the modules stubbed in base_stubs_path. For this to work the modules need to be installed.

Parameters
  • base_stubs_path – path to directory with (handwritten) stubs

  • mypy_conf_path – path to mypy.ini

Returns

returns the reference stub types

stubalyzer.analyze.main() None[source]
stubalyzer.analyze.parse_command_line() Namespace[source]
stubalyzer.analyze.restore_output() None[source]

Restore output of stdout and stderr to defaults.

stubalyzer.analyze.setup_expected_mismatches(file_path: Optional[str] = None) Tuple[Dict[str, MatchResult], Set[str]][source]
stubalyzer.analyze.silence_output() Tuple[StringIO, StringIO][source]

Redirect all output to in-memory buffers instead of stdout and stderr.

stubalyzer.analyze.write_error(*messages: str, sep: str = '', symbol: Optional[Union[mypy.nodes.Decorator, mypy.nodes.FuncDef, mypy.nodes.OverloadedFuncDef, mypy.nodes.Var, mypy.nodes.TypeInfo, mypy.nodes.TypeVarExpr, mypy.nodes.TypeAlias]] = None) None[source]