Debian Codegraph @

README.md

raw
1 <img src="docs/source/mypy_light.svg" alt="mypy logo" width="300px"/>
2
3 Mypy: Static Typing for Python
4 =======================================
5
6 [![Stable Version](https://img.shields.io/pypi/v/mypy?color=blue)](https://pypi.org/project/mypy/)
7 [![Downloads](https://img.shields.io/pypi/dm/mypy)](https://pypistats.org/packages/mypy)
8 [![Build Status](https://github.com/python/mypy/actions/workflows/test.yml/badge.svg)](https://github.com/python/mypy/actions)
9 [![Documentation Status](https://readthedocs.org/projects/mypy/badge/?version=latest)](https://mypy.readthedocs.io/en/latest/?badge=latest)
10 [![Chat at https://gitter.im/python/typing](https://badges.gitter.im/python/typing.svg)](https://gitter.im/python/typing?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
11 [![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)
12 [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
13 [![Linting: Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
14
15 Got a question?
16 ---------------
17
18 We are always happy to answer questions! Here are some good places to ask them:
19
20 - for general questions about Python typing, try [typing discussions](https://github.com/python/typing/discussions)
21 - for anything you're curious about, try [gitter chat](https://gitter.im/python/typing)
22
23 If you're just getting started,
24 [the documentation](https://mypy.readthedocs.io/en/stable/index.html)
25 and [type hints cheat sheet](https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html)
26 can also help answer questions.
27
28 If you think you've found a bug:
29
30 - check our [common issues page](https://mypy.readthedocs.io/en/stable/common_issues.html)
31 - search our [issue tracker](https://github.com/python/mypy/issues) to see if
32 it's already been reported
33
34 To report a bug or request an enhancement:
35
36 - report at [our issue tracker](https://github.com/python/mypy/issues)
37 - if the issue is with a specific library or function, consider reporting it at
38 [typeshed tracker](https://github.com/python/typeshed/issues) or the issue
39 tracker for that library
40
41 To discuss a new type system feature:
42
43 - discuss at [discuss.python.org](https://discuss.python.org/c/typing/32)
44 - there is also some historical discussion at the [typing-sig mailing list](https://mail.python.org/archives/list/[email protected]/) and the [python/typing repo](https://github.com/python/typing/issues)
45
46 What is mypy?
47 -------------
48
49 Mypy is a static type checker for Python.
50
51 Type checkers help ensure that you're using variables and functions in your code
52 correctly. With mypy, add type hints ([PEP 484](https://www.python.org/dev/peps/pep-0484/))
53 to your Python programs, and mypy will warn you when you use those types
54 incorrectly.
55
56 Python is a dynamic language, so usually you'll only see errors in your code
57 when you attempt to run it. Mypy is a *static* checker, so it finds bugs
58 in your programs without even running them!
59
60 Here is a small example to whet your appetite:
61
62 ```python
63 number = input("What is your favourite number?")
64 print("It is", number + 1) # error: Unsupported operand types for + ("str" and "int")
65 ```
66
67 Adding type hints for mypy does not interfere with the way your program would
68 otherwise run. Think of type hints as similar to comments! You can always use
69 the Python interpreter to run your code, even if mypy reports errors.
70
71 Mypy is designed with gradual typing in mind. This means you can add type
72 hints to your code base slowly and that you can always fall back to dynamic
73 typing when static typing is not convenient.
74
75 Mypy has a powerful and easy-to-use type system, supporting features such as
76 type inference, generics, callable types, tuple types, union types,
77 structural subtyping and more. Using mypy will make your programs easier to
78 understand, debug, and maintain.
79
80 See [the documentation](https://mypy.readthedocs.io/en/stable/index.html) for
81 more examples and information.
82
83 In particular, see:
84
85 - [type hints cheat sheet](https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html)
86 - [getting started](https://mypy.readthedocs.io/en/stable/getting_started.html)
87 - [list of error codes](https://mypy.readthedocs.io/en/stable/error_code_list.html)
88
89 Quick start
90 -----------
91
92 Mypy can be installed using pip:
93
94 ```bash
95 python3 -m pip install -U mypy
96 ```
97
98 If you want to run the latest version of the code, you can install from the
99 repo directly:
100
101 ```bash
102 python3 -m pip install -U git+https://github.com/python/mypy.git
103 ```
104
105 Now you can type-check the [statically typed parts] of a program like this:
106
107 ```bash
108 mypy PROGRAM
109 ```
110
111 You can always use the Python interpreter to run your statically typed
112 programs, even if mypy reports type errors:
113
114 ```bash
115 python3 PROGRAM
116 ```
117
118 If you are working with large code bases, you can run mypy in
119 [daemon mode], that will give much faster (often sub-second) incremental updates:
120
121 ```bash
122 dmypy run -- PROGRAM
123 ```
124
125 You can also try mypy in an [online playground](https://mypy-play.net/) (developed by
126 Yusuke Miyazaki).
127
128 [statically typed parts]: https://mypy.readthedocs.io/en/latest/getting_started.html#function-signatures-and-dynamic-vs-static-typing
129 [daemon mode]: https://mypy.readthedocs.io/en/stable/mypy_daemon.html
130
131 Integrations
132 ------------
133
134 Mypy can be integrated into popular IDEs:
135
136 - VS Code: provides [basic integration](https://code.visualstudio.com/docs/python/linting#_mypy) with mypy.
137 - Vim:
138 - Using [Syntastic](https://github.com/vim-syntastic/syntastic): in `~/.vimrc` add
139 `let g:syntastic_python_checkers=['mypy']`
140 - Using [ALE](https://github.com/dense-analysis/ale): should be enabled by default when `mypy` is installed,
141 or can be explicitly enabled by adding `let b:ale_linters = ['mypy']` in `~/vim/ftplugin/python.vim`
142 - Emacs: using [Flycheck](https://github.com/flycheck/)
143 - Sublime Text: [SublimeLinter-contrib-mypy](https://github.com/fredcallaway/SublimeLinter-contrib-mypy)
144 - PyCharm: [mypy plugin](https://github.com/dropbox/mypy-PyCharm-plugin)
145 - IDLE: [idlemypyextension](https://github.com/CoolCat467/idlemypyextension)
146 - pre-commit: use [pre-commit mirrors-mypy](https://github.com/pre-commit/mirrors-mypy), although
147 note by default this will limit mypy's ability to analyse your third party dependencies.
148
149 Web site and documentation
150 --------------------------
151
152 Additional information is available at the web site:
153
154 <https://www.mypy-lang.org/>
155
156 Jump straight to the documentation:
157
158 <https://mypy.readthedocs.io/>
159
160 Follow along our changelog at:
161
162 <https://mypy-lang.blogspot.com/>
163
164 Contributing
165 ------------
166
167 Help in testing, development, documentation and other tasks is
168 highly appreciated and useful to the project. There are tasks for
169 contributors of all experience levels.
170
171 To get started with developing mypy, see [CONTRIBUTING.md](CONTRIBUTING.md).
172
173 Mypyc and compiled version of mypy
174 ----------------------------------
175
176 [Mypyc](https://github.com/mypyc/mypyc) uses Python type hints to compile Python
177 modules to faster C extensions. Mypy is itself compiled using mypyc: this makes
178 mypy approximately 4 times faster than if interpreted!
179
180 To install an interpreted mypy instead, use:
181
182 ```bash
183 python3 -m pip install --no-binary mypy -U mypy
184 ```
185
186 To use a compiled version of a development
187 version of mypy, directly install a binary from
188 <https://github.com/mypyc/mypy_mypyc-wheels/releases/latest>.
189
190 To contribute to the mypyc project, check out the issue tracker at <https://github.com/mypyc/mypyc>