A plugin for flake8 integrating Mypy.

Overview

flake8-mypy

NOTE: THIS PROJECT IS DEAD

It was created in early 2017 when Mypy performance was often insufficient for in-editor linting. The Flake8 plugin traded correctness for performance, making Mypy only check a bare minimum of problems. These days Mypy is accelerated with mypyc, as well as uses aggressive caching to speed up incremental checks. It's no longer worth it to use hacks such as flake8-mypy.

What was the project anyway?

A plugin for Flake8 integrating mypy. The idea is to enable limited type checking as a linter inside editors and other tools that already support Flake8 warning syntax and config.

List of warnings

flake8-mypy reserves T4 for all current and future codes, T being the natural letter for typing-related errors. There are other plugins greedily reserving the entire letter T. To this I say: ¯\_(ツ)_/¯.

T400: any typing note.

T484: any typing error (after PEP 484, geddit?).

T498: internal mypy error.

T499: internal mypy traceback, stderr output, or an unmatched line.

I plan to support more fine-grained error codes for specific mypy errors in the future.

Two levels of type checking

mypy shines when given a full program to analyze. You can then use options like --follow-imports or --disallow-untyped-calls to exercise the full transitive closure of your modules, catching errors stemming from bad API usage or incompatible types. That being said, those checks take time, and require access to the entire codebase. For some tools, like an editor with an open file, or a code review tool, achieving this is not trivial. This is where a more limited approach inside a linter comes in.

Flake8 operates on unrelated files, it doesn't perform full program analysis. In other words, it doesn't follow imports. This is a curse and a blessing. We cannot find complex problems and the number of warnings we can safely show without risking false positives is lower. In return, we can provide useful warnings with great performance, usable for realtime editor integration.

As it turns out, in this mode of operation, mypy is still able to provide useful information on the annotations within and at least usage of stubbed standard library and third party libraries. However, for best effects, you will want to use separate configuration for mypy's standalone mode and for usage as a Flake8 plugin.

Configuration

Due to the reasoning above, by default flake8-mypy will operate with options equivalent to the following:

[mypy]
# Specify the target platform details in config, so your developers are
# free to run mypy on Windows, Linux, or macOS and get consistent
# results.
python_version=3.6
platform=linux

# flake8-mypy expects the two following for sensible formatting
show_column_numbers=True
show_error_context=False

# do not follow imports (except for ones found in typeshed)
follow_imports=skip

# since we're ignoring imports, writing .mypy_cache doesn't make any sense
cache_dir=/dev/null

# suppress errors about unsatisfied imports
ignore_missing_imports=True

# allow untyped calls as a consequence of the options above
disallow_untyped_calls=False

# allow returning Any as a consequence of the options above
warn_return_any=False

# treat Optional per PEP 484
strict_optional=True

# ensure all execution paths are returning
warn_no_return=True

# lint-style cleanliness for typing needs to be disabled; returns more errors
# than the full run.
warn_redundant_casts=False
warn_unused_ignores=False

# The following are off by default since they're too noisy.
# Flip them on if you feel adventurous.
disallow_untyped_defs=False
check_untyped_defs=False

If you disagree with the defaults above, you can specify your own mypy configuration by providing the --mypy-config= command-line option to Flake8 (with the .flake8/setup.cfg equivalent being called mypy_config). The value of that option should be a path to a mypy.ini or setup.cfg compatible file. For full configuration syntax, follow mypy documentation.

For the sake of simplicity and readability, the config you provide will fully replace the one listed above. Values left out will be using mypy's own defaults.

Remember that for the best user experience, your linter integration mode shouldn't generally display errors that a full run of mypy wouldn't. This would be confusing.

Note: chaing the follow_imports option might have surprising effects. If the file you're linting with Flake8 has other files around it, then in "silent" or "normal" mode those files will be used to follow imports. This includes imports from typeshed.

Tests

Just run:

python setup.py test

OMG, this is Python 3 only!

Yes, so is mypy. Relax, you can run Flake8 with all popular plugins as a tool perfectly fine under Python 3.5+ even if you want to analyze Python 2 code. This way you'll be able to parse all of the new syntax supported on Python 3 but also effectively all the Python 2 syntax at the same time.

By making the code exclusively Python 3.5+, I'm able to focus on the quality of the checks and re-use all the nice features of the new releases (check out pathlib) instead of wasting cycles on Unicode compatibility, etc.

License

MIT

Change Log

17.8.0

  • avoid raising errors in the default config which don't happen during a full run (disable warn_unused_ignores and warn_redundant_casts)

  • always run type checks from a temporary directory to avoid clashing with unrelated files in the same directory

17.3.3

  • suppress mypy messages about relative imports

17.3.2

  • bugfix: using Flake8 with absolute paths now correctly matches mypy messages

  • bugfix: don't crash on relative imports in the form from . import X

17.3.1

  • switch follow_imports from "silent" to "skip" to avoid name clashing files being used to follow imports within typeshed

  • set MYPYPATH by default to give stubs from typeshed higher priority than local sources

17.3.0

  • performance optimization: skip running mypy over files that contain no annotations or imports from typing

  • bugfix: when running over an entire directory, T484 is now correctly used instead of T499

17.2.0

  • first published version

  • date-versioned

Authors

Glued together by Łukasz Langa.

Owner
Łukasz Langa
Python 3.8 & 3.9 Release Manager. llanga on Twitter. Python core developer, hobbyist musician, dad.
Łukasz Langa
Pyright extension for coc.nvim

coc-pyright Pyright extension for coc.nvim Install :CocInstall coc-pyright Note: Pyright may not work as expected if can't detect project root correct

Heyward Fann 1.1k Jan 02, 2023
:sparkles: Surface lint errors during code review

✨ Linty Fresh ✨ Keep your codebase sparkly clean with the power of LINT! Linty Fresh parses lint errors and report them back to GitHub as comments on

Lyft 183 Dec 18, 2022
🦆 Better duck-typing with mypy-compatible extensions to Protocol

🦆 Quacks If it walks like a duck and it quacks like a duck, then it must be a duck Thanks to PEP544, Python now has protocols: a way to define duck t

Arie Bovenberg 9 Nov 14, 2022
Tools for improving Python imports

imptools Tools for improving Python imports. Installation pip3 install imptools Overview Detailed docs import_path Import a module from any path on th

Danijar Hafner 7 Aug 07, 2022
open source tools to generate mypy stubs from protobufs

mypy-protobuf: Generate mypy stub files from protobuf specs We just released a new major release mypy-protobuf 2. on 02/02/2021! It includes some back

Dropbox 527 Jan 03, 2023
docstring style checker

pydocstyle - docstring style checker pydocstyle is a static analysis tool for checking compliance with Python docstring conventions. pydocstyle suppor

Python Code Quality Authority 982 Jan 03, 2023
A static-analysis bot for Github

Imhotep, the peaceful builder. What is it? Imhotep is a tool which will comment on commits coming into your repository and check for syntactic errors

Justin Abrahms 221 Nov 10, 2022
mypy plugin for loguru

loguru-mypy A fancy plugin to boost up your logging with loguru mypy compatibility logoru-mypy should be compatible with mypy=0.770. Currently there

Tomasz Trębski 13 Nov 02, 2022
The strictest and most opinionated python linter ever!

wemake-python-styleguide Welcome to the strictest and most opinionated python linter ever. wemake-python-styleguide is actually a flake8 plugin with s

wemake.services 2.1k Jan 01, 2023
A static type analyzer for Python code

pytype - 🦆 ✔ Pytype checks and infers types for your Python code - without requiring type annotations. Pytype can: Lint plain Python code, flagging c

Google 4k Dec 31, 2022
Type annotations builder for boto3 compatible with VSCode, PyCharm, Emacs, Sublime Text, pyright and mypy.

mypy_boto3_builder Type annotations builder for boto3-stubs project. Compatible with VSCode, PyCharm, Emacs, Sublime Text, mypy, pyright and other too

Vlad Emelianov 2 Dec 05, 2022
Simple Python style checker in one Python file

pycodestyle (formerly called pep8) - Python style guide checker pycodestyle is a tool to check your Python code against some of the style conventions

Python Code Quality Authority 4.7k Jan 01, 2023
Mypy stubs, i.e., type information, for numpy, pandas and matplotlib

Mypy type stubs for NumPy, pandas, and Matplotlib This is a PEP-561-compliant stub-only package which provides type information for matplotlib, numpy

Predictive Analytics Lab 194 Dec 19, 2022
A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.

flake8-bugbear A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycode

Python Code Quality Authority 869 Dec 30, 2022
Pylint plugin to enforce some secure coding standards for Python.

Pylint Secure Coding Standard Plugin pylint plugin that enforces some secure coding standards. Installation pip install pylint-secure-coding-standard

Nguyen Damien 2 Jan 04, 2022
Pymxs, the 3DsMax bindings of Maxscript to Python doesn't come with any stubs

PyMXS Stubs generator What Pymxs, the 3DsMax bindings of Maxscript to Python doe

Frieder Erdmann 19 Dec 27, 2022
Flake8 plugin for managing type-checking imports & forward references

flake8-type-checking Lets you know which imports to put in type-checking blocks. For the imports you've already defined inside type-checking blocks, i

snok 67 Dec 16, 2022
Naming Convention checker for Python

PEP 8 Naming Conventions Check your code against PEP 8 naming conventions. This module provides a plugin for flake8, the Python code checker. (It repl

Python Code Quality Authority 411 Dec 23, 2022
Pylint plugin for improving code analysis for when using Django

pylint-django About pylint-django is a Pylint plugin for improving code analysis when analysing code using Django. It is also used by the Prospector t

Python Code Quality Authority 544 Jan 06, 2023
Flake8 plugin to validate annotations complexity

flake8-annotations-complexity An extension for flake8 to report on too complex type annotations. Complex type annotations often means bad annotations

BestDoctor 41 Dec 28, 2022