automatic error suppression for mypy
Project description
mypy-upgrade
Table of Contents
- What is
mypy-upgrade
? - Features
- Basic Usage
- Recommended Mypy Flags
- Command-Line Options
- Quick Start
- Known Limitations
- Similar Projects
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] # FIX ME
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 # FIX ME
comment will be
added instead.
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:
-
pipe
mypy
's output directly tomypy-upgrade
mypy --strict -p my_package | mypy-upgrade
-
create a
mypy
type checking report text filemypy --strict -p my_package > mypy_report.txt
and then pass the file to
mypy-upgrade
mypy-upgrade --report mypy_report.txt
Command-Line Options
You may want to include the error messages provided by mypy
in the
suppression comments so that you can fix them later. You can do so using
the -d
(or --description-style
) option
mypy-upgrade --report mypy_report.txt -d full -p package
You also customize the "fix me" message placed after the error suppression
comment using the --fix-me
option
mypy-upgrade --report mypy_report.txt --fix-me "FIX THIS" -p package
To selectively silence errors in packages and modules, use the -p
(--package
) and -m
(--module
) options along with the fully qualified
module/package name, respectively:
mypy-upgrade --report mypy_report.txt -p package1 -p package2 -m package1.module1 -m package2.module2
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
Recommended Mypy Flags
To enable all checks utilized by mypy-upgrade
to silence as many errors as possible, the
following flags should be set when creating the type checking report to pass to mypy-upgrade
:
-
--show-absolute-path
- Required if running
mypy-upgrade
in a separate directory thanmypy
- Required if running
-
--strict
- This will ensure that
mypy-upgrade
will attempt to silence all possible mypy errors
- This will ensure that
-
--show-column-numbers
- This allows for more precise treatment of certain type error edge cases (e.g. type checking errors before the start of multiline strings)
-
--show-error-codes
- This ensures that error-code specific comments are added instead of blanket
type: ignore
comments
- This ensures that error-code specific comments are added instead of blanket
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 Limitations
The following limitations derive mainly from Python syntax issues and are unable to be handled
by mypy-upgrade
. If you can't resolve the error directly, please consider refactoring to permit
error suppression.
-
Type errors on lines ending in line continuation characters or within multiline f-strings
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 ofmypy-upgrade
's implementation -
whtsky/mypy-silent
: relies solely ontyper
+ the standard library; includes support for removing unusedtype: ignore
comments but no support for suppressing multiple errors on a single line; another inspiration for much ofmypy-upgrade
's implementation -
patrick91/mypy-silent
: a fork ofwhtsky/mypy-silent
with support for suppressing multiple errors on a single line (on thefeature/multiple-errors
branch) -
uptickmetachu/mypy-silent
: a fork ofwhtsky/mypy-silent
with support for suppressing multiple errors on a single line
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for mypy_upgrade-0.0.1a4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | feef8755e1874a680b33b02e03b91e9c6d0d43e3f1d5abd0b4a27174114353ad |
|
MD5 | d931613619d7c479b31e494cb8f4ffe9 |
|
BLAKE2b-256 | a083497f372e30529b4dd6a4a08b5891d5e5a42938401b205221c7c370a7d57e |