Skip to content

Commit 8db0387

Browse files
committed
Document Python 2 annotation syntax
1 parent 5e9c1a6 commit 8db0387

File tree

5 files changed

+79
-13
lines changed

5 files changed

+79
-13
lines changed

docs/source/basics.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ Function signatures
1010

1111
A function without a type signature is dynamically typed. You can
1212
declare the signature of a function using the Python 3 annotation
13-
syntax. This makes the function statically typed (the type checker
14-
reports type errors within the function). A function without a
15-
type annotation is dynamically typed, and identical to ordinary
16-
Python:
13+
syntax (Python 2 is discussed later in :ref:`python2`.) This makes the
14+
function statically typed (the type checker reports type errors within
15+
the function). A function without a type annotation is dynamically
16+
typed, and identical to ordinary Python:
1717

1818
.. code-block:: python
1919

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
# General information about the project.
4848
project = u'Mypy'
49-
copyright = u'2014, Jukka Lehtosalo'
49+
copyright = u'2016, Jukka Lehtosalo'
5050

5151
# The version info for the project you're documenting, acts as replacement for
5252
# |version| and |release|, also used in various other places throughout the

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Welcome to Mypy documentation!
1212
introduction
1313
basics
1414
builtin_types
15+
python2
1516
type_inference_and_annotations
1617
kinds_of_types
1718
class_basics

docs/source/introduction.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ Introduction
33

44
Mypy is a static type checker for Python. If you sprinkle your code
55
with type annotations using the Python 3 function annotation syntax
6-
(using the PEP 484 notation), mypy can type check you code and find
7-
common bugs. Mypy is a static analyzer, or a lint-like tool: type
8-
annotations are just hints and are not enforced when running your
9-
program. You run your program with a standard Python interpreter, and
10-
the annotations are treated basically as comments.
6+
(using the PEP 484 notation) or a comment-based annotation syntax for
7+
Python 2 code, mypy can type check you code and find common bugs. Mypy
8+
is a static analyzer, or a lint-like tool: type annotations are just
9+
hints and are not enforced when running your program. You run your
10+
program with a standard Python interpreter, and the annotations are
11+
treated basically as comments.
1112

1213
Mypy has a powerful but easy-to-use type system with modern features
1314
such as type inference, generics, function types, tuple types and
@@ -17,9 +18,8 @@ programs, but it will make your programs easier to debug, maintain and
1718
understand.
1819

1920
This document is a short introduction to mypy. It will get you started
20-
writing statically typed code. Knowledge of Python 3.x and some kind
21-
of a statically typed object-oriented language such as Java are
22-
assumed.
21+
writing statically typed code. Knowledge of Python and some kind of a
22+
statically typed object-oriented language such as Java are assumed.
2323

2424
.. note::
2525
Mypy is still experimental. There will be changes

docs/source/python2.rst

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
.. _python2:
2+
3+
Type checking Python 2 code
4+
===========================
5+
6+
For code that needs to be Python 2.7 compatible, function type
7+
annotations are given in comments, since the function annotation
8+
syntax was introduced in Python 3. The comment-based syntax is
9+
specified in `PEP 484 <https://www.python.org/dev/peps/pep-0484>`_.
10+
11+
Run mypy in Python 2 mode by using the ``--py2`` option::
12+
13+
$ mypy --py2 program.py
14+
15+
To run your program, you must have the ``typing`` module in your module
16+
search path. Use ``pip install typing`` to install the module (this also
17+
works for Python 3).
18+
19+
The example below illustrates Python 2 function type annotation
20+
syntax. This is also valid in Python 3 mode:
21+
22+
.. code-block:: python
23+
24+
from typing import List
25+
26+
def hello(): # type: () -> None
27+
print 'hello'
28+
29+
class Example:
30+
def method(self, lst, opt=0, *args, **kwargs):
31+
# type: (List[str], int, *str, **bool) -> int
32+
...
33+
34+
Here are more specifics:
35+
36+
- You should include types for arguments with default values in the
37+
annotation. The ``opt`` argument of ``method`` above is an example
38+
of this.
39+
40+
- For ``*args`` and ``**kwargs`` the type should be prefixed with
41+
``*`` or ``**``, respectively. Again, the above example illustrates
42+
this.
43+
44+
- The type syntax for variables is the same as for Python 3.
45+
46+
- Things like ``Any`` must be imported from ``typing``, even if they
47+
are only used in comments.
48+
49+
- You don't provide an annotation for the ``self``/``cls`` variable of
50+
methods.
51+
52+
- The annotation can be on the same line as the function header or on
53+
the following line.
54+
55+
- You don't need to use string literal escapes for forward references
56+
within comments.
57+
58+
- Mypy uses a separate set of library stub files in `typeshed
59+
<http://github.com/python/typeshed>`_ for Python 2. Library support
60+
may vary between Python 2 and Python 3.
61+
62+
.. note::
63+
64+
Currently there's no support for splitting an annotation to multiple
65+
lines. This will likely change in the future.

0 commit comments

Comments
 (0)