Skip to main content
Documentation Status

An analysis tool to compare handwritten mypy stubs to stubgen-generated stubs.

stubalyzer makes the process of crafting types for untyped dependencies easier. Some example inconsistencies which are found and reported by stubalyzer are listed below:

  • handcrafted and generated stubs have different method signatures

  • generated stubs are missing functions/class members

  • generated types are more specific than the handcrafted types

How to use?

Installation

Install stubalyzer with the following command:

$ pip install stubalyzer

How to run

Run stubalyzer with the following command:

$ stubalyzer -h
usage: stubalyzer [-h] -c CONFIG [-e EXPECTED_MISMATCHES] [-r REFERENCE_STUBS] [-x CHECKSTYLE_REPORT] [-s] [-p]
                  STUBS_HANDWRITTEN

Analyze a set of (handcrafted) mypy stubs by comparing them to (generated)
reference stubs

required arguments:
-c CONFIG, --config CONFIG
                        Mypy config file

positional arguments:
  STUBS_HANDWRITTEN     Directory of handwritten stubs that need to be
                        analyzed

optional arguments:
  -h, --help            show this help message and exit
  -e EXPECTED_MISMATCHES, --expected-mismatches EXPECTED_MISMATCHES
                        A JSON file, which defines expected mismatching
                        symbols and their match results. If any symbol is
                        declared in an expected_mismatches JSON file,
                        stubalyzer will count it as an expected failure, and
                        ignore this inconsistency.

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

                        According to the example above, we expect the signature
                        of my.module.function to mismatch, and module.Class to
                        be missing in the generated stubs. stubalyzer will
                        ignore these inconsistencies.
  -r REFERENCE_STUBS, --reference REFERENCE_STUBS

                        Directory of reference stubs to compare against. If
                        not specified stubgen will be used to generate the
                        reference stubs.
  -x CHECKSTYLE_REPORT, --checkstyle-report CHECKSTYLE_REPORT

                        Write an xml report in checkstyle format to the given file.
  -s, --silent
                        Suppress all non-error output.
  -p, --include-private

                        Include definitions stubgen would otherwise consider
                        private, when generating the reference stubs. (e.g.
                        names with a single leading underscore, like "_foo")

Output

If the comparison ends successfully with zero inconsistencies, stubalyzer will print a success message to stdout, alongside with an ignore message.

The ignore message includes the number of failures ignored, which are declared as expected in the file for expected mismatches. If this file is not provided, the ignore message will not be printed.

Successfully validated 68 stubs.

If there are mismatches in the given types, stubalyzer will print a list of all inconsistencies with a result message, alongside with an ignore message -if there is any, similar to the following:

Symbol "vars.any_var" not found in generated stubs.

Types for functions.additional_args do not match:

   Handwritten type: def (foo: builtins.int, bar: builtins.int) -> builtins.str

   Reference type  : def (foo: builtins.int) -> builtins.str


Failure: 33 of 68 stubs seem not to be valid.

2 more fail(s) were ignored, because they were defined in expected mismatches.

Development

The following section contains instructions on how to set up and use the development environment for this project.

Development Setup

Requirements for development:

  • A recent Python version (we currently use 3.9)

For a development setup, run the following shell script:

$ ./dev/setup.sh

This will create a virtual environment in the directory venv and install the project’s dependencies. To activate the virtual environment, run source ./venv/bin/activate from the project directory.

Tests

Tests are run using pytest:

$ pytest

Type Checking

Type checking is done with Mypy:

$ mypy stubalyzer

Code Formatting

To set up the pre-commit hook to automatically format files, run:

$ pre-commit install

The source code is formatted using black and isort. The following will format all files in the project:

$ pre-commit run -a

Linting

Linting is done using flake8, in the root directory run:

$ flake8

On commit, pre-commit automtically runs flake8 on changed files.

Dependency Management

If you need new dependencies, add them in pyproject.toml.

Documentation

The documentation is written using Sphinx. Build the documentation using:

$ cd doc; make html

The output will be in docs/_build/html/index.html.

You can update the API documentation using the following:

$ ./dev/update-apidoc.sh

Releases

Stubalyzer has no fixed release schedule. Instead releases are made when needed.

To prepare a new release, run the following and follow the instructions in the output:

$ ./dev/release.sh <patch|minor|major>

Release files for stubalyzer 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stubalyzer 0.5.1
File Size Uploaded
stubalyzer-0.5.1.tar.gz 43.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stubalyzer 0.5.1
File Interpreter ABI Platform
stubalyzer-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 75.4 kB

Release files / stubalyzer-0.5.1.tar.gz

Download URL stubalyzer-0.5.1.tar.gz
Size 43.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f3a5f76f12fee81fc651cba9a7d6b498c1280902efc0a6b6f60a66dd316065ba
BLAKE2b-256 checksum
How to use checksums
2962527517ed62bb995212f756133e571120cb37843e16fb3b93b32b0abad741
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release files / stubalyzer-0.5.1-py3-none-any.whl

Download URL stubalyzer-0.5.1-py3-none-any.whl
Size 31.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a77a0d80d89ef62d93334da72439efbeefa21c98b88925237bfefb59cbef9fa
BLAKE2b-256 checksum
How to use checksums
4a37b44bd95d830e4e551719d6cabb465b9c60c0e601276ce19d5b51ec0990fb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.28.1

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page