Optional static typing for Python 3 and 2 (PEP 484)

Overview

mypy logo

Mypy: Optional Static Typing for Python

Build Status Chat at https://gitter.im/python/typing Checked with mypy

Got a question? Join us on Gitter!

We don't have a mailing list; but we are always happy to answer questions on gitter chat. If you are sure you've found a bug please search our issue trackers for a duplicate before filing a new issue:

What is mypy?

Mypy is an optional static type checker for Python. You can add type hints (PEP 484) to your Python programs, and use mypy to type check them statically. Find bugs in your programs without even running them!

You can mix dynamic and static typing in your programs. You can always fall back to dynamic typing when static typing is not convenient, such as for legacy code.

Here is a small example to whet your appetite (Python 3):

from typing import Iterator

def fib(n: int) -> Iterator[int]:
    a, b = 0, 1
    while a < n:
        yield a
        a, b = b, a + b

See the documentation for more examples.

For Python 2.7, the standard annotations are written as comments:

def is_palindrome(s):
    # type: (str) -> bool
    return s == s[::-1]

See the documentation for Python 2 support.

Mypy is in development; some features are missing and there are bugs. See 'Development status' below.

Requirements

You need Python 3.5 or later to run mypy. You can have multiple Python versions (2.x and 3.x) installed on the same system without problems.

In Ubuntu, Mint and Debian you can install Python 3 like this:

$ sudo apt-get install python3 python3-pip

For other Linux flavors, macOS and Windows, packages are available at

https://www.python.org/getit/

Quick start

Mypy can be installed using pip:

$ python3 -m pip install -U mypy

If you want to run the latest version of the code, you can install from git:

$ python3 -m pip install -U git+git://github.com/python/mypy.git

Now, if Python on your system is configured properly (else see "Troubleshooting" below), you can type-check the statically typed parts of a program like this:

$ mypy PROGRAM

You can always use a Python interpreter to run your statically typed programs, even if they have type errors:

$ python3 PROGRAM

You can also try mypy in an online playground (developed by Yusuke Miyazaki).

IDE, Linter Integrations, and Pre-commit

Mypy can be integrated into popular IDEs:

Mypy can also be set up as a pre-commit hook using pre-commit mirrors-mypy.

Web site and documentation

Documentation and additional information is available at the web site:

http://www.mypy-lang.org/

Or you can jump straight to the documentation:

https://mypy.readthedocs.io/

Troubleshooting

Depending on your configuration, you may have to run pip like this:

$ python3 -m pip install -U mypy

This should automatically install the appropriate version of mypy's parser, typed-ast. If for some reason it does not, you can install it manually:

$ python3 -m pip install -U typed-ast

If the mypy command isn't found after installation: After python3 -m pip install, the mypy script and dependencies, including the typing module, will be installed to system-dependent locations. Sometimes the script directory will not be in PATH, and you have to add the target directory to PATH manually or create a symbolic link to the script. In particular, on macOS, the script may be installed under /Library/Frameworks:

/Library/Frameworks/Python.framework/Versions/<version>/bin

In Windows, the script is generally installed in \PythonNN\Scripts. So, type check a program like this (replace \Python34 with your Python installation path):

C:\>\Python34\python \Python34\Scripts\mypy PROGRAM

Working with virtualenv

If you are using virtualenv, make sure you are running a python3 environment. Installing via pip3 in a v2 environment will not configure the environment to run installed modules from the command line.

$ python3 -m pip install -U virtualenv
$ python3 -m virtualenv env

Quick start for contributing to mypy

If you want to contribute, first clone the mypy git repository:

$ git clone https://github.com/python/mypy.git

From the mypy directory, use pip to install mypy:

$ cd mypy
$ python3 -m pip install -U .

Replace python3 with your Python 3 interpreter. You may have to do the above as root. For example, in Ubuntu:

$ sudo python3 -m pip install -U .

Now you can use the mypy program just as above. In case of trouble see "Troubleshooting" above.

NOTE: Installing with sudo can be a security risk. Please try with the --user flag first. $ python3 -m pip install --user -U .

Tests

The basic way to run tests:

$ pip3 install -r test-requirements.txt
$ python2 -m pip install -U typing
$ ./runtests.py

For more on the tests, such as how to write tests and how to control which tests to run, see Test README.md.

Development status

Mypy is beta software, but it has already been used in production for several years at Dropbox and in many other organizations, and it has an extensive test suite.

See the roadmap if you are interested in plans for the future.

Changelog

Follow mypy's updates on the blog: https://mypy-lang.blogspot.com/

Issue tracker

Please report any bugs and enhancement ideas using the mypy issue tracker: https://github.com/python/mypy/issues

If you have any questions about using mypy or types, please ask in the typing gitter instead: https://gitter.im/python/typing

Compiled version of mypy

We have built a compiled version of mypy using the mypyc compiler for mypy-annotated Python code. It is approximately 4 times faster than interpreted mypy and is available (and the default) for 64-bit Windows, macOS, and Linux.

To install an interpreted mypy instead, use:

$ python3 -m pip install --no-binary mypy -U mypy

If you wish to test out the compiled version of a development version of mypy, you can directly install a binary from https://github.com/mypyc/mypy_mypyc-wheels/releases/latest.

Help wanted

Any help in testing, development, documentation and other tasks is highly appreciated and useful to the project. There are tasks for contributors of all experience levels. If you're just getting started, ask on the gitter chat for ideas of good beginner issues.

For more details, see the file CONTRIBUTING.md.

License

Mypy is licensed under the terms of the MIT License (see the file LICENSE).

Owner
Python
Repositories related to the Python Programming language
Python
🦔 PostHog is developer-friendly, open-source product analytics.

PostHog provides open-source product analytics, built for developers. Automate the collection of every event on your website or app, with no need to send data to 3rd parties. With just 1 click you ca

PostHog 10.3k Jan 01, 2023
fixup: Automatically add and remove python import statements

fixup: Automatically add and remove python import statements The goal is that running fixup my_file.py will automatically add or remove import stateme

2 May 08, 2022
An analysis tool for Python that blurs the line between testing and type systems.

CrossHair An analysis tool for Python that blurs the line between testing and type systems. THE LATEST NEWS: Check out the new crosshair cover command

Phillip Schanely 836 Jan 08, 2023
Data parsing and validation using Python type hints

pydantic Data validation and settings management using Python type hinting. Fast and extensible, pydantic plays nicely with your linters/IDE/brain. De

Samuel Colvin 12.1k Jan 05, 2023
A formatter for Python files

YAPF Introduction Most of the current formatters for Python --- e.g., autopep8, and pep8ify --- are made to remove lint errors from code. This has som

Google 13k Dec 31, 2022
Metrinome is an all-purpose tool for working with code complexity metrics.

Overview Metrinome is an all-purpose tool for working with code complexity metrics. It can be used as both a REPL and API, and includes: Converters to

26 Dec 26, 2022
Unbearably fast O(1) runtime type-checking in pure Python.

Look for the bare necessities, the simple bare necessities. Forget about your worries and your strife. — The Jungle Book.

1.4k Dec 29, 2022
Typing-toolbox for Python 3 _and_ 2.7 w.r.t. PEP 484.

Welcome to the pytypes project pytypes is a typing toolbox w.r.t. PEP 484 (PEP 526 on the road map, later also 544 if it gets accepted). Its main feat

Stefan Richthofer 188 Dec 29, 2022
A Python utility / library to sort imports.

Read Latest Documentation - Browse GitHub Code Repository isort your imports, so you don't have to. isort is a Python utility / library to sort import

Python Code Quality Authority 5.5k Jan 06, 2023
Collects all accepted (partial and full scored) codes submitted within the given timeframe and saves them locally for plagiarism check.

Collects all accepted (partial and full scored) codes submitted within the given timeframe of any contest.

ARITRA BELEL 2 Dec 28, 2021
Alarmer is a tool focus on error reporting for your application.

alarmer Alarmer is a tool focus on error reporting for your application. Installation pip install alarmer Usage It's simple to integrate alarmer in yo

long2ice 20 Jul 03, 2022
C/C++ Dependency Analyzer: a rewrite of John Lakos' dep_utils (adep/cdep/ldep) from

Version bêta d'un système pour suivre les prix des livres chez Books to Scrape, un revendeur de livres en ligne. En pratique, dans cette version bêta, le programme n'effectuera pas une véritable surv

Olzhas Rakhimov 125 Sep 21, 2022
TidyPy is a tool that encapsulates a number of other static analysis tools and makes it easy to configure, execute, and review their results.

TidyPy Contents Overview Features Usage Docker Configuration Ignoring Issues Included Tools Included Reporters Included Integrations Extending TidyPy

Jason Simeone 33 Nov 27, 2022
Auto-generate PEP-484 annotations

PyAnnotate: Auto-generate PEP-484 annotations Insert annotations into your source code based on call arguments and return types observed at runtime. F

Dropbox 1.4k Dec 26, 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 05, 2023
A bytecode vm written in python.

CHex A bytecode vm written in python. hex command meaning note: the first two hex values of a CHex program are the magic number 0x01 (offset in memory

1 Aug 26, 2022
Code audit tool for python.

Pylama Code audit tool for Python and JavaScript. Pylama wraps these tools: pycodestyle (formerly pep8) © 2012-2013, Florent Xicluna; pydocstyle (form

Kirill Klenov 966 Dec 29, 2022
Guesslang detects the programming language of a given source code

Detect the programming language of a source code

Y. SOMDA 618 Dec 29, 2022
Print a directory tree structure in your Python code.

directory-structure Print a directory tree structure in your Python code. Download You can simply: pip install directory-structure Or you can also: Cl

Gabriel Stork 45 Dec 19, 2022
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