automatic error suppression for mypy
Project description
mypy-upgrade
Table of Contents
- What is
mypy-upgrade? - Features
- Quick Start
- Basic Usage
- Advanced Usage
- Using the API
- Recommended Mypy Flags
- Known Limitations
- Similar Projects
What is mypy-upgrade?
mypy-upgrade provides automatic error suppression for
mypy (analogous to
pyre-upgrade and
pylint-silent). In addition,
mypy-upgrade exposes an API with the same functionality.
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: ignorecomments -
Replacement of blanket
type: ignorecomments with error code-specific comments -
Support for suppressing multiple mypy errors per-line
-
Preservation of existing in-line comments
-
Optional inclusion of
mypyerror description messages -
Selective suppression of type errors by file, directory, package, module, or mypy error codes
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@development
# or if you don't have 'git' installed
python3 -m pip install -U https://github.com/ugognw/mypy-upgrade/tree/development
Basic Usage
There are two idioms for invocation. To silence all errors in a package, one can:
-
pipe
mypy's output directly tomypy-upgrademypy --strict -p my_package | mypy-upgrade -
create a
mypytype checking report text filemypy --strict -p my_package > mypy_report.txtand then pass the file to
mypy-upgrademypy-upgrade --report mypy_report.txt
Advanced Usage
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 can 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/a/package/ path/to/a/module.py
To selectively silence a particular kind of type error, use the --silence-error option:
mypy-upgrade --report mypy_report.txt --silence-error "type-arg"
For a full list of all options and their descriptions, run mypy-upgrade --help
usage: mypy-upgrade [-h] [-v] [-V] [more options; see below]
[-m MODULE] [-p PACKAGE] [-r REPORT] [-s ERROR_CODE] [files ...]
Place in-line comments into files to silence mypy errors.
options:
-h, --help show this help message and exit
-V, --version Show program's version number and exit.
--dry-run Don't actually silence anything, just print what would
be.
Printing:
Control what information is printed and how.
-v, --verbose Control the verbosity. 0=Print warnings and messages
for each unsilenced error. 1=Also print messages for
each silenced error. 2=Used for debugging. Defaults to
0.
-q, --quiet, --suppress-warnings
Suppress all warnings. Disabled by default.
-S, --summarize Print a summary after running. If the verbosity is
greater than zero, a detailed summary will also be
printed.
-c, --colours Enable coloured output.
Comment Formatting:
Format how error suppression comments are placed.
-d {full,none}, --description-style {full,none}
Specify the style in which mypy error descriptions are
expressed in the error suppression comment. Defaults
to "none".
--fix-me FIX_ME Specify a custom 'Fix Me' message to be placed after
the error suppression comment. Pass " " to omit a 'Fix
Me' message altogether. Defaults to "FIX ME".
Error Filtering:
Specify which errors will be silenced.
-r REPORT, --report REPORT
The path to a text file containing a mypy type
checking report. If not specified, input is read from
standard input.
-m MODULE, --module MODULE
Silence errors from the provided (importable) module.
This flag may be repeated multiple times.
-p PACKAGE, --package PACKAGE
Silence errors from the provided (importable) package.
This flag may be repeated multiple times.
-s ERROR_CODE, --silence-error ERROR_CODE
Silence mypy errors by error code. This flag may be
repeated multiple times.
files Silence errors from the provided files/directories.
Using the API
Identical functionality to the command-line utility can be obtained using the
API. In addition, one obtains detailed information on the results of running
mypy-upgrade. Assuming the mypy type checking report is saved as
mypy_report.txt, we have
import pathlib
from mypy_upgrade.silence import silence_errors_in_report
mypy_report = pathlib.Path("mypy_report.txt")
with mypy_report.open(mode="r", encoding="utf-8") as report:
result = silence_errors_in_report(
report=report,
packages=["package1", "package2"],
modules=["package1.module1", "package2.module2"],
files=["path/to/a/package/", "path/to/a/module.py"],
description_style="full",
fix_me="FIX THIS",
codes_to_silence=["arg-type", "no-untyped-def"],
dry_run=False,
)
silenced, failures, ignored = result
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-upgradein a separate directory thanmypy
- Required if running
-
--strict- Enables
mypy-upgradeto silence all possible mypy errors (see Known Limitations for exceptions)
- Enables
-
--show-error-codes- Ensures that error-code specific comments are added instead of blanket
type: ignorecomments
- Ensures that error-code specific comments are added instead of blanket
-
--warn-unused-ignores- Ensures that unused
type: ignorecomments are removed
- Ensures that unused
-
ignore-without-code- When used with
--show-error-codes, permitsmypy-upgradeto replace existingtype: ignorecomments with code-specifictype: ignorecomments (enable from the command line with themypyoption--enable-error-code)
- When used with
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
-
Improperly specified type hints within comments
-
mypywill report a type error if a type hint is improperly specified; for example, given the following code:x = {} # type: set
mypywill produce atype-argerror in column 1 andmypy-upgradewill place a# type: ignore[type-arg]comment at the end, which will, in turn, negate the effectiveness of the# type: setcommment and eliminate the need for the# type: ignore[type-arg]comment -
-
mypy"syntax"errors are not silenced- It is recommended that you fix your code such that it is syntactically valid prior to using
mypy-upgrade
- It is recommended that you fix your code such that it is syntactically valid prior to using
Similar Projects
If this doesn't fit your use-case, maybe one of these other projects will!
-
geo7/mypy_clean_slate:mypyreports are generated internally in--strictmode; 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: ignorecomments 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-silentwith support for suppressing multiple errors on a single line (on thefeature/multiple-errorsbranch) -
uptickmetachu/mypy-silent: a fork ofwhtsky/mypy-silentwith 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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mypy_upgrade-0.0.1b6.tar.gz.
File metadata
- Download URL: mypy_upgrade-0.0.1b6.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c3c4ea29dec3fe6a911d6ee25608fc598ec7648efb4efb4b04b39b25f494cc2b
|
|
| MD5 |
7cf56f356956834f39e50dbfa0b59e04
|
|
| BLAKE2b-256 |
49f530230e363d2ffa02e8d3d30356ab76e92d9fd0f525a03ecb527906e46ff1
|
File details
Details for the file mypy_upgrade-0.0.1b6-py3-none-any.whl.
File metadata
- Download URL: mypy_upgrade-0.0.1b6-py3-none-any.whl
- Upload date:
- Size: 21.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.1 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea32ebb049f7271e51ca8f7f3ed22560c8bdb2a2d90834a9571120455171bfb4
|
|
| MD5 |
96dd0a9f37a90d11ede99f0fa7fe243d
|
|
| BLAKE2b-256 |
3f2c406029ca603856198f2260198677572bf4aebb91dcddc0f7385856ba4fc7
|