Skip to content

Commit 01f3893

Browse files
author
Guido van Rossum
committed
Merge master (22ee713)
2 parents 7884ed4 + 22ee713 commit 01f3893

File tree

111 files changed

+1893
-3444
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+1893
-3444
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ docs/build/
1414
.cache
1515
.runtest_log.json
1616
dmypy.json
17+
.dmypy.json
1718

1819
# Packages
1920
*.egg

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ this:
136136

137137
$ python3 -m pip install -U mypy
138138

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

docs/source/basics.rst

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,29 @@ Arguments with default values can be annotated as follows:
4646
def greeting(name: str, prefix: str = 'Mr.') -> str:
4747
return 'Hello, {} {}'.format(name, prefix)
4848
49-
Mixing dynamic and static typing
50-
********************************
49+
Running mypy
50+
************
5151

52-
Mixing dynamic and static typing within a single file is often
53-
useful. For example, if you are migrating existing Python code to
54-
static typing, it may be easiest to do this incrementally, such as by
55-
migrating a few functions at a time. Also, when prototyping a new
56-
feature, you may decide to first implement the relevant code using
57-
dynamic typing and only add type signatures later, when the code is
58-
more stable.
52+
You can type check a program by using the ``mypy`` tool, which is
53+
basically a linter -- it checks your program for errors without actually
54+
running it::
5955

60-
.. code-block:: python
56+
$ mypy program.py
6157

62-
def f():
63-
1 + 'x' # No static type error (dynamically typed)
58+
All errors reported by mypy are essentially warnings that you are free
59+
to ignore, if you so wish.
6460

65-
def g() -> None:
66-
1 + 'x' # Type check error (statically typed)
61+
The next chapter explains how to download and install mypy:
62+
:ref:`getting-started`.
63+
64+
More command line options are documented in :ref:`command-line`.
6765

6866
.. note::
6967

70-
The earlier stages of mypy, known as the semantic analysis, may
71-
report errors even for dynamically typed functions. However, you
72-
should not rely on this, as this may change in the future.
68+
Depending on how mypy is configured, you may have to run mypy like
69+
this::
70+
71+
$ python3 -m mypy program.py
7372

7473
The typing module
7574
*****************
@@ -87,37 +86,33 @@ them (we'll explain ``Iterable`` later in this document):
8786
print('Hello, {}'.format(name))
8887
8988
For brevity, we often omit the ``typing`` import in code examples, but
90-
you should always include it in modules that contain statically typed
91-
code.
92-
93-
The presence or absence of the ``typing`` module does not affect
94-
whether your code is type checked; it is only required when you use
95-
one or more special features it defines.
89+
mypy will give an error if you use definitions such as ``Iterable``
90+
without first importing them.
9691

97-
Type checking programs
98-
**********************
99-
100-
You can type check a program by using the ``mypy`` tool, which is
101-
basically a linter -- it checks your program for errors without actually
102-
running it::
92+
Mixing dynamic and static typing
93+
********************************
10394

104-
$ mypy program.py
95+
Mixing dynamic and static typing within a single file is often
96+
useful. For example, if you are migrating existing Python code to
97+
static typing, it may be easiest to do this incrementally, such as by
98+
migrating a few functions at a time. Also, when prototyping a new
99+
feature, you may decide to first implement the relevant code using
100+
dynamic typing and only add type signatures later, when the code is
101+
more stable.
105102

106-
All errors reported by mypy are essentially warnings that you are free
107-
to ignore, if you so wish.
103+
.. code-block:: python
108104
109-
The next chapter explains how to download and install mypy:
110-
:ref:`getting-started`.
105+
def f():
106+
1 + 'x' # No static type error (dynamically typed)
111107
112-
More command line options are documented in :ref:`command-line`.
108+
def g() -> None:
109+
1 + 'x' # Type check error (statically typed)
113110
114111
.. note::
115112

116-
Depending on how mypy is configured, you may have to explicitly use
117-
the Python 3 interpreter to run mypy. The mypy tool is an ordinary
118-
mypy (and so also Python) program. For example::
119-
120-
$ python3 -m mypy program.py
113+
The earlier stages of mypy, known as the semantic analysis, may
114+
report errors even for dynamically typed functions. However, you
115+
should not rely on this, as this may change in the future.
121116

122117
.. _library-stubs:
123118

@@ -147,7 +142,7 @@ the builtins contains a definition like this for ``chr``:
147142
148143
def chr(code: int) -> str: ...
149144
150-
In stub files we don't care about the function bodies, so we use
145+
In stub files we don't care about the function bodies, so we use
151146
an ellipsis instead. That ``...`` is three literal dots!
152147

153148
Mypy complains if it can't find a stub (or a real module) for a
@@ -175,7 +170,7 @@ typeshed yet).
175170

176171
That's it! Now you can access the module in mypy programs and type check
177172
code that uses the library. If you write a stub for a library module,
178-
consider making it available for other programmers that use mypy
173+
consider making it available for other programmers that use mypy
179174
by contributing it back to the typeshed repo.
180175

181176
There is more information about creating stubs in the

docs/source/builtin_types.rst

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,26 @@ Built-in types
33

44
These are examples of some of the most common built-in types:
55

6-
=================== ===============================
7-
Type Description
8-
=================== ===============================
9-
``int`` integer of arbitrary size
10-
``float`` floating point number
11-
``bool`` boolean value
12-
``str`` unicode string
13-
``bytes`` 8-bit string
14-
``object`` an arbitrary object (``object`` is the common base class)
15-
``List[str]`` list of ``str`` objects
16-
``Tuple[int, int]`` tuple of two ``int`` objects (``Tuple[()]`` is the empty tuple)
17-
``Tuple[int, ...]`` tuple of an arbitrary number of ``int`` objects
18-
``Dict[str, int]`` dictionary from ``str`` keys to ``int`` values
19-
``Iterable[int]`` iterable object containing ints
20-
``Sequence[bool]`` sequence of booleans
21-
``Any`` dynamically typed value with an arbitrary type
22-
=================== ===============================
6+
====================== ===============================
7+
Type Description
8+
====================== ===============================
9+
``int`` integer
10+
``float`` floating point number
11+
``bool`` boolean value
12+
``str`` string (unicode)
13+
``bytes`` 8-bit string
14+
``object`` an arbitrary object (``object`` is the common base class)
15+
``List[str]`` list of ``str`` objects
16+
``Tuple[int, int]`` tuple of two ``int`` objects (``Tuple[()]`` is the empty tuple)
17+
``Tuple[int, ...]`` tuple of an arbitrary number of ``int`` objects
18+
``Dict[str, int]`` dictionary from ``str`` keys to ``int`` values
19+
``Iterable[int]`` iterable object containing ints
20+
``Sequence[bool]`` sequence of booleans (read-only)
21+
``Mapping[str, int]`` mapping from ``str`` keys to ``int`` values (read-only)
22+
``Any`` dynamically typed value with an arbitrary type
23+
====================== ===============================
2324

24-
The type ``Any`` and type constructors ``List``, ``Dict``,
25+
The type ``Any`` and type constructors such as ``List``, ``Dict``,
2526
``Iterable`` and ``Sequence`` are defined in the ``typing`` module.
2627

2728
The type ``Dict`` is a *generic* class, signified by type arguments within
@@ -30,7 +31,7 @@ strings and and ``Dict[Any, Any]`` is a dictionary of dynamically typed
3031
(arbitrary) values and keys. ``List`` is another generic class. ``Dict`` and
3132
``List`` are aliases for the built-ins ``dict`` and ``list``, respectively.
3233

33-
``Iterable`` and ``Sequence`` are generic abstract base classes that
34+
``Iterable``, ``Sequence``, and ``Mapping`` are generic types that
3435
correspond to Python protocols. For example, a ``str`` object or a
3536
``List[str]`` object is valid
3637
when ``Iterable[str]`` or ``Sequence[str]`` is expected. Note that even though

docs/source/command_line.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,20 @@ Here are some more useful flags:
463463

464464
.. _shadow-file:
465465

466-
- ``--shadow-file SOURCE_FILE SHADOW_FILE`` makes mypy typecheck SHADOW_FILE in
467-
place of SOURCE_FILE. Primarily intended for tooling. Allows tooling to
468-
make transformations to a file before type checking without having to change
469-
the file in-place. (For example, tooling could use this to display the type
470-
of an expression by wrapping it with a call to reveal_type in the shadow
471-
file and then parsing the output.)
466+
- ``--shadow-file SOURCE_FILE SHADOW_FILE``: when mypy is asked to typecheck
467+
``SOURCE_FILE``, this makes it read from and typecheck the contents of
468+
``SHADOW_FILE`` instead. However, diagnostics will continue to refer to
469+
``SOURCE_FILE``. Specifying this argument multiple times
470+
(``--shadow-file X1 Y1 --shadow-file X2 Y2``)
471+
will allow mypy to perform multiple substitutions.
472+
473+
This allows tooling to create temporary files with helpful modifications
474+
without having to change the source file in place. For example, suppose we
475+
have a pipeline that adds ``reveal_type`` for certain variables.
476+
This pipeline is run on ``original.py`` to produce ``temp.py``.
477+
Running ``mypy --shadow-file original.py temp.py original.py`` will then
478+
cause mypy to typecheck the contents of ``temp.py`` instead of ``original.py``,
479+
but error messages will still reference ``original.py``.
472480

473481
.. _no-implicit-optional:
474482

docs/source/common_issues.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,25 @@ understand how mypy handles a particular piece of code. Example:
430430
431431
reveal_type((1, 'hello')) # Revealed type is 'Tuple[builtins.int, builtins.str]'
432432
433+
You can also use ``reveal_locals()`` at any line in a file
434+
to see the types of all local varaibles at once. Example:
435+
436+
.. code-block:: python
437+
438+
a = 1
439+
b = 'one'
440+
reveal_locals()
441+
# Revealed local types are:
442+
# a: builtins.int
443+
# b: builtins.str
433444
.. note::
434445

435-
``reveal_type`` is only understood by mypy and doesn't exist
436-
in Python, if you try to run your program. You'll have to remove
437-
any ``reveal_type`` calls before you can run your code.
438-
``reveal_type`` is always available and you don't need to import it.
446+
``reveal_type`` and ``reveal_locals`` are only understood by mypy and
447+
don't exist in Python. If you try to run your program, you'll have to
448+
remove any ``reveal_type`` and ``reveal_locals`` calls before you can
449+
run your code. Both are always available and you don't need to import
450+
them.
451+
439452

440453
.. _import-cycles:
441454

docs/source/getting_started.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ Getting started
66
Installation
77
************
88

9-
Mypy requires Python 3.4 or later. Once you've `installed Python 3 <https://www.python.org/downloads/>`_,
9+
Mypy requires Python 3.4 or later to run. Once you've
10+
`installed Python 3 <https://www.python.org/downloads/>`_,
1011
you can install mypy with:
1112

1213
.. code-block:: text
1314
1415
$ python3 -m pip install mypy
1516
17+
Note that even though you need Python 3 to run ``mypy``, type checking
18+
Python 2 code is fully supported, as discussed in :ref:`python2`.
19+
1620
Installing from source
1721
**********************
1822

19-
To install mypy from source, clone the github repository and then run
23+
To install mypy from source, clone the
24+
`mypy repository on GitHub <https://github.com/python/mypy>`_ and then run
2025
``pip install`` locally:
2126

2227
.. code-block:: text

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Welcome to Mypy documentation!
77
==============================
88

9-
Mypy is a static type checker for Python.
9+
Mypy is a static type checker for Python 3 and Python 2.7.
1010

1111
.. toctree::
1212
:maxdepth: 2

docs/source/introduction.rst

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
Introduction
22
============
33

4-
Mypy is a static type checker for Python. If you sprinkle your code
5-
with type annotations, mypy can type check your code and find common bugs.
6-
As mypy is a static analyzer, or a lint-like tool, your code's type
7-
annotations are just hints and don't interfere when running your program.
4+
Mypy is a static type checker for Python 3 and Python 2.7. If you sprinkle
5+
your code with type annotations, mypy can type check your code and find common
6+
bugs. As mypy is a static analyzer, or a lint-like tool, the type
7+
annotations are just hints for mypy and don't interfere when running your program.
88
You run your program with a standard Python interpreter, and the annotations
9-
are treated primarily as comments.
9+
are treated effectively as comments.
1010

11-
Using the Python 3 function annotation syntax (using the PEP 484 notation) or
11+
Using the Python 3 function annotation syntax (using the
12+
`PEP 484 <https://www.python.org/dev/peps/pep-0484/>`_ notation) or
1213
a comment-based annotation syntax for Python 2 code, you will be able to
1314
efficiently annotate your code and use mypy to check the code for common
14-
errors. Mypy has a powerful, easy-to-use, type system with modern features
15-
such as type inference, generics, function types, tuple types and
15+
errors. Mypy has a powerful and easy-to-use type system with modern features
16+
such as type inference, generics, function types, tuple types, and
1617
union types.
1718

1819
As a developer, you decide how to use mypy in your workflow. You can always
1920
escape to dynamic typing as mypy's approach to static typing doesn't restrict
2021
what you can do in your programs. Using mypy will make your programs easier to
21-
debug, maintain, and understand.
22+
understand, debug, and maintain.
2223

2324
This documentation provides a short introduction to mypy. It will help you
2425
get started writing statically typed code. Knowledge of Python and a
2526
statically typed object-oriented language, such as Java, are assumed.
2627

2728
.. note::
2829

29-
Mypy is still experimental. There will be changes
30-
that break backward compatibility.
30+
Mypy is used in production by many companies and projects, but mypy is
31+
officially beta software. There will be occasional changes
32+
that break backward compatibility. The mypy development team tries to
33+
minimize the impact of changes to user code.

docs/source/python2.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ function using the form ``# type: (...) -> rt``, where ``rt`` is the
8989
return type. Note that the return type annotation contains literal
9090
three dots.
9191

92-
Note that when using multi-line comments, you do not need to prefix the
92+
When using multi-line comments, you do not need to prefix the
9393
types of your ``*arg`` and ``**kwarg`` parameters with ``*`` or ``**``.
9494
For example, here is how you would annotate the first example using
95-
multi-line comments.
95+
multi-line comments:
9696

9797
.. code-block:: python
9898
@@ -120,10 +120,11 @@ Additional notes
120120
- The annotation can be on the same line as the function header or on
121121
the following line.
122122

123-
- The type syntax for variables is the same as for Python 3.
123+
- Variables use a comment-based type syntax (explained in
124+
:ref:`explicit-var-types`).
124125

125126
- You don't need to use string literal escapes for forward references
126-
within comments.
127+
within comments (string literal escapes are explained later).
127128

128129
- Mypy uses a separate set of library stub files in `typeshed
129130
<https://github.com/python/typeshed>`_ for Python 2. Library support

0 commit comments

Comments
 (0)