Skip to main content

automatic error suppression for mypy

Project description

mypy-upgrade

PyPI - Version PyPI - Python Version Hatch project linting - Ruff code style - Black types - Mypy License - MIT


Table of Contents

What is mypy-upgrade?

mypy-upgrade is a command-line utility that provides automatic error suppression for mypy (analogous to pyre-upgrade and pylint-silent).

Given a type checking report from mypy, mypy-upgrade will silence the listed errors using error suppression comments. For example, with the following output from mypy:

package/subpackage/module.py:13: error: Incompatible default for argument "filename" (default has type "None", argument has type "str") [assignment]

mypy-upgrade will place a # type: ignore[assignment] comment at the end of line 13 in package/subpackage/module.py. If error codes are not present in the mypy report (e.g., the hide-error-codes flag is set when mypy was invoked), then a non-specific # type: ignore comment will be added instead.

:warning: Warning: mypy-check must be run in the same directory that mypy was run.

Features

  • Removal of unused type: ignore comments

  • Optional inclusion of mypy error description messages

  • Support for suppressing multiple mypy errors per-line

  • Preservation of existing in-line comments

  • Replacement of blanket type: ignore comments with error code-specific comments

Basic Usage

There are two idioms for invocation. To silence all errors in a package, one can:

  1. pipe mypy's output directly to mypy-upgrade

     mypy --strict -p my_package | mypy-upgrade
    
  2. create a mypy type checking report text file

     mypy --strict -p my_package > mypy_report.txt
    

    and then pass the file to mypy-upgrade

     mypy-upgrade --report mypy_report.txt
    

:memo: Note: To ensure desired behaviour, packages and modules must be passed using their fully qualified names (e.g., my_package.my_module).

Command-Line Options

You may want to include the error descriptions provided by mypy in the suppression comments so that you can fix them later. You can do so using the -d (or --with-descriptions) option

mypy-upgrade --report mypy_report.txt -d -p MY_PACKAGE

To selectively silence errors in packages and modules, use the -p (--package) and -m (--module) options, respectively:

Similarly, to selectively silence errors in files and directories, pass them in as positional arguments:

mypy-upgrade --report mypy_report.txt path/to/my_package/ path/to/a/module.py

For a full list of all options and their descriptions, run

mypy-upgrade --help

Quick Start

mypy-upgrade can be installed via pip.

python3 -m pip install mypy-upgrade

If you want to run the latest version of the code, you can install from the repo directly:

python3 -m pip install -U git+https://github.com/ugognw/mypy-upgrade.git
# or if you don't have 'git' installed
python3 -m pip install -U https://github.com/ugognw/mypy-upgrade/tree/development

Known Bugs

This utility is unable to silence mypy errors which occur on lines ending in line continuation characters since any non-whitespace following such a character is a syntax error. Pre-formatting your code with a PEP8 adherent formatter (e.g., black) to replace such lines with parentheses is recommended.

Similar Projects

If this doesn't fit your use-case, maybe one of these other projects will!

  • geo7/mypy_clean_slate: mypy reports are generated internally in --strict mode; includes support for suppressing multiple errors on a single line; an inspiration for much of mypy-upgrade's implementation

  • whtsky/mypy-silent: relies solely on typer + the standard library; includes support for removing unused type: ignore comments but no support for suppressing multiple errors on a single line; another inspiration for much of mypy-upgrade's implementation

  • patrick91/mypy-silent: a fork of whtsky/mypy-silent with support for suppressing multiple errors on a single line (on the feature/multiple-errors branch)

  • uptickmetachu/mypy-silent: a fork of whtsky/mypy-silent with support for suppressing multiple errors on a single line

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mypy_upgrade-0.0.1a3.tar.gz (11.5 kB view hashes)

Uploaded Source

Built Distribution

mypy_upgrade-0.0.1a3-py3-none-any.whl (12.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page