Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e25c67

Browse files
authored
Various documentation updates (#5043)
This includes a bunch of updates to the documentation, including (but not limited to) these: * Remove or update some obsolete things * Make the ordering within some sections more logical * Clarify some explanations * Add some cross references and links
1 parent 89a8e67 commit 4e25c67

7 files changed

Lines changed: 205 additions & 149 deletions

File tree

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/getting_started.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +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
16-
Note that ``mypy`` itself requires Python 3, but you can still check Python 2
17-
code using ``mypy`` as discussed in :ref:`python2`.
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`.
1819

1920
Installing from source
2021
**********************
2122

22-
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
2325
``pip install`` locally:
2426

2527
.. 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)