@@ -6,15 +6,17 @@ Getting started
6
6
This chapter introduces some core concepts of mypy, including function
7
7
annotations, the :py:mod: `typing ` module, stub files, and more.
8
8
9
- Be sure to read this chapter carefully, as the rest of the documentation
9
+ If you're looking for a quick intro, see the
10
+ :ref: `mypy cheatsheet <cheat-sheet-py3 >`.
11
+
12
+ If you're unfamiliar with the concepts of static and dynamic type checking,
13
+ be sure to read this chapter carefully, as the rest of the documentation
10
14
may not make much sense otherwise.
11
15
12
16
Installing and running mypy
13
17
***************************
14
18
15
- Mypy requires Python 3.6 or later to run. Once you've
16
- `installed Python 3 <https://www.python.org/downloads/ >`_,
17
- install mypy using pip:
19
+ Mypy requires Python 3.7 or later to run. You can install mypy using pip:
18
20
19
21
.. code-block :: shell
20
22
@@ -31,13 +33,16 @@ out any errors it finds. Mypy will type check your code *statically*: this
31
33
means that it will check for errors without ever running your code, just
32
34
like a linter.
33
35
34
- This means that you are always free to ignore the errors mypy reports and
35
- treat them as just warnings, if you so wish: mypy runs independently from
36
- Python itself .
36
+ This also means that you are always free to ignore the errors mypy reports,
37
+ if you so wish. You can always use the Python interpreter to run your code,
38
+ even if mypy reports errors .
37
39
38
40
However, if you try directly running mypy on your existing Python code, it
39
- will most likely report little to no errors: you must add *type annotations *
40
- to your code to take full advantage of mypy. See the section below for details.
41
+ will most likely report little to no errors. This is a feature! It makes it
42
+ easy to adopt mypy incrementally.
43
+
44
+ In order to get useful diagnostics from mypy, you must add *type annotations *
45
+ to your code. See the section below for details.
41
46
42
47
Function signatures and dynamic vs static typing
43
48
************************************************
@@ -77,9 +82,6 @@ calls since the arguments have invalid types:
77
82
greeting(3 ) # Argument 1 to "greeting" has incompatible type "int"; expected "str"
78
83
greeting(b ' Alice' ) # Argument 1 to "greeting" has incompatible type "bytes"; expected "str"
79
84
80
- Note that this is all still valid Python 3 code! The function annotation syntax
81
- shown above was added to Python :pep: `as a part of Python 3.0 <3107 >`.
82
-
83
85
Being able to pick whether you want a function to be dynamically or statically
84
86
typed can be very helpful. For example, if you are migrating an existing
85
87
Python codebase to use static types, it's usually easier to migrate by incrementally
@@ -91,12 +93,6 @@ Once you are finished migrating or prototyping your code, you can make mypy warn
91
93
if you add a dynamic function by mistake by using the :option: `--disallow-untyped-defs <mypy --disallow-untyped-defs> `
92
94
flag. See :ref: `command-line ` for more information on configuring mypy.
93
95
94
- .. note ::
95
-
96
- The earlier stages of analysis performed by mypy may report errors
97
- even for dynamically typed functions. However, you should not rely
98
- on this, as this may change in the future.
99
-
100
96
More function signatures
101
97
************************
102
98
0 commit comments