python

mypy: Check Python Types Without Running Your Code

mypy is a static type checker that finds type-related issues in Python code before it runs. Add type hints incrementally, then use mypy locally or in a development workflow to catch mistakes and make code easier to maintain.

Stars
20.7k
Forks
3.3k
Stars, last 8 days
+11

Our take

Health score

68/100

How it is scored
Activity
53
Community
82
Issues
77
Pull requests
60

+11 stars in the last 8 days

  • Merges more pull requests than 86% of the projects we track
  • More contributors than 77% of the projects we track

Overview

mypy checks Python code against its type hints and reports type errors without executing the program. It helps developers find certain mistakes earlier than runtime testing can, especially in code with annotated functions and variables.

Its gradual typing approach lets teams adopt annotations a piece at a time and retain dynamic typing where it is more convenient. It is useful for individual developers and teams that want type feedback during development or as part of code checks.

Key Features

  • Checks Python type hints statically, without running the checked code.
  • Supports gradual typing, so annotations can be introduced incrementally.
  • Provides type inference, generics, callable types, tuple types, union types, and structural subtyping.
  • Runs from the command line against a program or codebase.
  • Offers daemon mode for faster incremental checks on large codebases.
  • Integrates with editors and tools including VS Code, Vim, Emacs, PyCharm, and pre-commit.
  • Can be used in an online playground to try type-checking examples.

Use Cases

  • Python developers can catch mismatched values and function arguments while writing annotated code, before running it.
  • Teams maintaining large codebases can introduce type hints incrementally and use checks during development.
  • Library authors can use static checking to help keep function interfaces and internal code consistent.
  • Developers can run the daemon while editing to get quicker feedback on incremental changes.

Getting Started

Install mypy and check a Python program:

python3 -m pip install -U mypy
mypy PROGRAM

See the README and documentation for setup and usage details.

Alternatives

  • pyre-check: Pyre is another Python type checker, with incremental checking and Pysa security analysis, but its repository is archived.
ProjectLanguageLicenseStarsStatus
mypyPythonOther20.7kNot checked yet
pyre-checkOCamlMIT7.2kArchived

Considerations

mypy checks declared or inferred types; it does not execute the program, so static checking is not a substitute for running tests. Its feedback depends on the type information available, and gradual typing means unannotated or dynamically typed areas may receive less static checking. The repository metadata reports the license as NOASSERTION, so verify licensing details before adopting it where that matters.

Found this useful?

Share it with someone who would like mypy.

Comparisons