talira

Optional static typing for Python

open sourceMITfree

mypy is an optional static type checker for Python that combines the convenience of dynamic (duck) typing with compile-time type checking. You add type hints to standard Python code, a function at a time if you like, and mypy finds type errors before the code runs; the program still runs on any Python VM with no runtime overhead, and static and dynamic typing can be mixed freely within a module or an expression. It supports gradual typing, generics, protocols and the typing features defined in Python's PEPs, uses the typeshed stubs for the standard library and third-party packages, and runs as a command, a daemon for fast incremental checks, or through editor integrations. The same project also ships mypyc, which compiles type-annotated Python modules to C extensions.

Problems it solves

  • catch type errors in my Python code before it runs in production
  • add type hints to an existing Python codebase gradually, a module at a time
  • check that my Python type annotations are actually correct in CI
  • make a large Python codebase easier to refactor without introducing bugs
  • type check Python code fast enough to run on every save with a daemon
  • compile type-annotated Python modules to C extensions for speed

Features

  • static type checking of standard Python code
  • gradual typing: mix static and dynamic typing
  • generics, protocols and typing PEP support
  • typeshed stubs for the standard library and packages
  • mypy daemon (dmypy) for fast incremental checks
  • plugin system
  • mypyc compiler for type-annotated modules

Limitations

  • mypy needs Python 3.10 or newer to run.1

Capabilities

Has a command line toolplan not known
Takes pluginsplan not known

Alternatives

The same job

These do what mypy does.

  1. Pyright Static type checker for Python
  2. ty An extremely fast Python type checker and language server, written in Rust
  3. Pylance A performant, feature-rich language server for Python in VS Code
  4. Flow A static type checker and typed dialect of JavaScript

Compare mypy with the top 3