Skip to content

Commit a3cf5c9

Browse files
committed
Wrap long lines
1 parent 3ae57de commit a3cf5c9

14 files changed

+499
-145
lines changed

docs/source/additional_features.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Additional features
22
-------------------
33

4-
Several mypy features are not currently covered by this tutorial, including the following:
4+
Several mypy features are not currently covered by this tutorial,
5+
including the following:
56

67
- inheritance between generic classes
78
- compatibility and subtyping of generic types, including covariance of generic types

docs/source/basics.rst

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Basics
44
Function signatures
55
*******************
66

7-
A function without a type signature is dynamically typed. You can declare the signature of a function using the Python 3 annotation syntax This makes the function statically typed (the type checker reports type errors within the function):
7+
A function without a type signature is dynamically typed. You can
8+
declare the signature of a function using the Python 3 annotation
9+
syntax This makes the function statically typed (the type checker
10+
reports type errors within the function):
811

912
.. code-block:: python
1013
@@ -20,7 +23,9 @@ A function without a type signature is dynamically typed. You can declare the si
2023
def greeting(name: str) -> str:
2124
return 'Hello, {}'.format(name)
2225
23-
A None return type indicates a function that does not explicitly return a value. Using a None result in a statically typed context results in a type check error:
26+
A None return type indicates a function that does not explicitly
27+
return a value. Using a None result in a statically typed context
28+
results in a type check error:
2429

2530
.. code-block:: python
2631
@@ -32,7 +37,9 @@ A None return type indicates a function that does not explicitly return a value.
3237
The typing module
3338
*****************
3439

35-
We cheated a bit in the above examples: a module is type checked only if it imports the module typing. Here is a complete statically typed example from the previous section:
40+
We cheated a bit in the above examples: a module is type checked only
41+
if it imports the module typing. Here is a complete statically typed
42+
example from the previous section:
3643

3744
.. code-block:: python
3845
@@ -41,7 +48,9 @@ We cheated a bit in the above examples: a module is type checked only if it impo
4148
def greeting(name: str) -> str:
4249
return 'Hello, {}'.format(name)
4350
44-
The typing module contains many definitions that are useful in statically typed code. You can also use from ... import to import them (we'll explain Iterable later in this document):
51+
The typing module contains many definitions that are useful in
52+
statically typed code. You can also use from ... import to import them
53+
(we'll explain Iterable later in this document):
4554

4655
.. code-block:: python
4756
@@ -51,7 +60,9 @@ The typing module contains many definitions that are useful in statically typed
5160
for name in names:
5261
print('Hello, {}'.format(name))
5362
54-
For brevity, we often omit the typing import in code examples, but you should always include it in modules that contain statically typed code.
63+
For brevity, we often omit the typing import in code examples, but you
64+
should always include it in modules that contain statically typed
65+
code.
5566

5667
You can still have dynamically typed functions in modules that import typing:
5768

@@ -65,27 +76,43 @@ You can still have dynamically typed functions in modules that import typing:
6576
def g() -> None:
6677
1 + 'x' # Type check error (statically typed)
6778
68-
Mixing dynamic and static typing within a single file is often useful. For example, if you are migrating existing Python code to static typing, it may be easiest to do this incrementally, such as by migrating a few functions at a time. Also, when prototyping a new feature, you may decide to first implement the relevant code using dynamic typing and only add type signatures later, when the code is more stable.
79+
Mixing dynamic and static typing within a single file is often
80+
useful. For example, if you are migrating existing Python code to
81+
static typing, it may be easiest to do this incrementally, such as by
82+
migrating a few functions at a time. Also, when prototyping a new
83+
feature, you may decide to first implement the relevant code using
84+
dynamic typing and only add type signatures later, when the code is
85+
more stable.
6986

7087
.. note::
7188

72-
Currently the type checker checks the top levels and annotated functions of all modules, even those that don't import typing. However, you should not rely on this, as this will change in the future.
89+
Currently the type checker checks the top levels and annotated
90+
functions of all modules, even those that don't import
91+
typing. However, you should not rely on this, as this will change
92+
in the future.
7393

7494
Type checking and running programs
7595
**********************************
7696

77-
You can type check a program by using the mypy tool, which is basically a linter — it checks you program for errors without actually running it::
97+
You can type check a program by using the mypy tool, which is
98+
basically a linter — it checks you program for errors without actually
99+
running it::
78100

79101
$ mypy program.py
80102

81-
You can always run a mypy program as a Python program, without type checking, even it it has type errors::
103+
You can always run a mypy program as a Python program, without type
104+
checking, even it it has type errors::
82105

83106
$ python3 program.py
84107

85-
All errors reported by mypy are essentially warnings that you are free to ignore, if you so wish.
108+
All errors reported by mypy are essentially warnings that you are free
109+
to ignore, if you so wish.
86110

87-
The `README <https://github.com/JukkaL/mypy/blob/master/README.md>`_ explains how to download and install mypy.
111+
The `README <https://github.com/JukkaL/mypy/blob/master/README.md>`_
112+
explains how to download and install mypy.
88113

89114
.. note::
90115

91-
Depending on how mypy is configured, you may have to explicitly use the Python interpreter to run mypy. The mypy tool is an ordinary mypy (and so also Python) program.
116+
Depending on how mypy is configured, you may have to explicitly use
117+
the Python interpreter to run mypy. The mypy tool is an ordinary
118+
mypy (and so also Python) program.

docs/source/builtin_types.rst

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ These are examples of some of the most common built-in types:
1717
Sequence[bool] # sequence of booleans
1818
Any # dynamically typed value
1919
20-
The type Any and type constructors List, Dict, Iterable and Sequence are defined in the typing module.
20+
The type Any and type constructors List, Dict, Iterable and Sequence
21+
are defined in the typing module.
2122

22-
The type Dict is a *generic* class, signified by type arguments within [...]. For example, Dict[int, str] is a dictionary from integers to strings and and Dict[Any, Any] is a dictionary of dynamically typed (arbitrary) values and keys. List is another generic class. Dict and List are aliases for the built-ins dict and list, respectively.
23+
The type Dict is a *generic* class, signified by type arguments within
24+
[...]. For example, Dict[int, str] is a dictionary from integers to
25+
strings and and Dict[Any, Any] is a dictionary of dynamically typed
26+
(arbitrary) values and keys. List is another generic class. Dict and
27+
List are aliases for the built-ins dict and list, respectively.
2328

24-
Iterable and Sequence are generic abstract base classes that correspond to Python protocols. For example, a str object is valid when Iterable[str] or Sequence[str] is expected. Note that even though they are similar to abstract base classes defined in abc.collections (formerly collections), they are not identical, since the built-in collection type objects do not support indexing.
29+
Iterable and Sequence are generic abstract base classes that
30+
correspond to Python protocols. For example, a str object is valid
31+
when Iterable[str] or Sequence[str] is expected. Note that even though
32+
they are similar to abstract base classes defined in abc.collections
33+
(formerly collections), they are not identical, since the built-in
34+
collection type objects do not support indexing.

docs/source/casts.rst

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

4-
Mypy supports type casts that are usually used to coerce a statically typed value to a subtype. Unlike languages such as Java or C#, however, mypy casts are only used as hints for the type checker when using Python semantics, and they have no runtime effect. Use the function cast to perform a cast:
4+
Mypy supports type casts that are usually used to coerce a statically
5+
typed value to a subtype. Unlike languages such as Java or C#,
6+
however, mypy casts are only used as hints for the type checker when
7+
using Python semantics, and they have no runtime effect. Use the
8+
function cast to perform a cast:
59

610
.. code-block:: python
711
@@ -11,9 +15,16 @@ Mypy supports type casts that are usually used to coerce a statically typed valu
1115
x = cast(List[int], o) # OK
1216
y = cast(List[str], o) # OK (cast performs no actual runtime check)
1317
14-
Supporting runtime checking of casts such as the above when using Python semantics would require emulating reified generics and this would be difficult to do and would likely degrade performance and make code more difficult to read. You should not rely in your programs on casts being checked at runtime. Use an assertion if you want to perform an actual runtime check. Casts are used to silence spurious type checker warnings.
18+
Supporting runtime checking of casts such as the above when using
19+
Python semantics would require emulating reified generics and this
20+
would be difficult to do and would likely degrade performance and make
21+
code more difficult to read. You should not rely in your programs on
22+
casts being checked at runtime. Use an assertion if you want to
23+
perform an actual runtime check. Casts are used to silence spurious
24+
type checker warnings.
1525

16-
You don't need a cast for expressions with type Any, of when assigning to a variable with type Any, as was explained earlier.
26+
You don't need a cast for expressions with type Any, of when assigning
27+
to a variable with type Any, as was explained earlier.
1728

1829
You can cast to a dynamically typed value by just calling Any:
1930

docs/source/class_basics.rst

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Class basics
44
Instance and class attributes
55
*****************************
66

7-
Mypy type checker detects if you are trying to access a missing attribute, which is a very common programming error. For this to work correctly, instance and class attributes must be defined or initialized within the class. Mypy infers the types of attributes:
7+
Mypy type checker detects if you are trying to access a missing
8+
attribute, which is a very common programming error. For this to work
9+
correctly, instance and class attributes must be defined or
10+
initialized within the class. Mypy infers the types of attributes:
811

912
.. code-block:: python
1013
@@ -16,7 +19,13 @@ Mypy type checker detects if you are trying to access a missing attribute, which
1619
a.x = 2 # OK
1720
a.y = 3 # Error: A has no attribute y
1821
19-
This is a bit like each class having an implicitly defined __slots__ attribute. In Python semantics this is only enforced during type checking: at runtime we use standard Python semantics. You can selectively define a class as *dynamic*; dynamic classes have Python-like compile-time semantics, and they allow you to assign to arbitrary attributes anywhere in a program without the type checker complaining:
22+
This is a bit like each class having an implicitly defined __slots__
23+
attribute. In Python semantics this is only enforced during type
24+
checking: at runtime we use standard Python semantics. You can
25+
selectively define a class as *dynamic*; dynamic classes have
26+
Python-like compile-time semantics, and they allow you to assign to
27+
arbitrary attributes anywhere in a program without the type checker
28+
complaining:
2029

2130
.. code-block:: python
2231
@@ -28,13 +37,16 @@ This is a bit like each class having an implicitly defined __slots__ attribute.
2837
a = A()
2938
a.x = 2 # OK, no need to define x explicitly.
3039
31-
Mypy also lets you read arbitrary attributes of dynamic class instances. This limits type checking effectiveness, so you should only use dynamic classes when you really need them.
40+
Mypy also lets you read arbitrary attributes of dynamic class
41+
instances. This limits type checking effectiveness, so you should only
42+
use dynamic classes when you really need them.
3243

3344
.. note::
3445

3546
Dynamic classes are not implemented in the current mypy version.
3647

37-
You can declare variables in the class body explicitly using Undefined or a type comment:
48+
You can declare variables in the class body explicitly using Undefined
49+
or a type comment:
3850

3951
.. code-block:: python
4052
@@ -45,9 +57,11 @@ You can declare variables in the class body explicitly using Undefined or a type
4557
a = A()
4658
a.x = [1] # OK
4759
48-
As in Python, a variable defined in the class body can used as a class or an instance variable.
60+
As in Python, a variable defined in the class body can used as a class
61+
or an instance variable.
4962

50-
Similarly, you can give explicit types to instance variables defined in a method:
63+
Similarly, you can give explicit types to instance variables defined
64+
in a method:
5165

5266
.. code-block:: python
5367
@@ -58,7 +72,8 @@ Similarly, you can give explicit types to instance variables defined in a method
5872
def f(self) -> None:
5973
self.y = 0 # type: Any # OK
6074
61-
You can only define an instance variable within a method if you assign to it explicitly using self:
75+
You can only define an instance variable within a method if you assign
76+
to it explicitly using self:
6277

6378
.. code-block:: python
6479
@@ -71,7 +86,8 @@ You can only define an instance variable within a method if you assign to it exp
7186
Overriding statically typed methods
7287
***********************************
7388

74-
When overriding a statically typed method, mypy checks that the override has a compatible signature:
89+
When overriding a statically typed method, mypy checks that the
90+
override has a compatible signature:
7591

7692
.. code-block:: python
7793
@@ -93,11 +109,18 @@ When overriding a statically typed method, mypy checks that the override has a c
93109
94110
.. note::
95111

96-
You can also vary return types **covariantly** in overriding. For example, you could override the return type 'object' with a subtype such as 'int'.
112+
You can also vary return types **covariantly** in overriding. For
113+
example, you could override the return type 'object' with a subtype
114+
such as 'int'.
97115

98-
You can also override a statically typed method with a dynamically typed one. This allows dynamically typed code to override methods defined in library classes without worrying about their type signatures, similar to Python.
116+
You can also override a statically typed method with a dynamically
117+
typed one. This allows dynamically typed code to override methods
118+
defined in library classes without worrying about their type
119+
signatures, similar to Python.
99120

100-
There is no runtime enforcement that the method override returns a value that is compatible with the original return type, since types are erased in the Python semantics:
121+
There is no runtime enforcement that the method override returns a
122+
value that is compatible with the original return type, since types
123+
are erased in the Python semantics:
101124

102125
.. code-block:: python
103126
@@ -117,7 +140,10 @@ There is no runtime enforcement that the method override returns a value that is
117140
Abstract base classes and multiple inheritance
118141
**********************************************
119142

120-
Mypy uses Python abstract base classes for protocol types. There are several built-in abstract base classes types (for example, Sequence, Iterable and Iterator). You can define abstract base classes using the abc.ABCMeta metaclass and the abc.abstractmethod function decorator.
143+
Mypy uses Python abstract base classes for protocol types. There are
144+
several built-in abstract base classes types (for example, Sequence,
145+
Iterable and Iterator). You can define abstract base classes using the
146+
abc.ABCMeta metaclass and the abc.abstractmethod function decorator.
121147

122148
.. code-block:: python
123149
@@ -139,10 +165,15 @@ Mypy uses Python abstract base classes for protocol types. There are several bui
139165
a = A() # Error: A is abstract
140166
b = B() # OK
141167
142-
Unlike most Python code, abstract base classes are likely to play a significant role in many complex mypy programs.
168+
Unlike most Python code, abstract base classes are likely to play a
169+
significant role in many complex mypy programs.
143170
144-
A class can inherit any number of classes, both abstract and concrete. As with normal overrides, a dynamically typed method can implement a statically typed abstract method defined in an abstract base class.
171+
A class can inherit any number of classes, both abstract and
172+
concrete. As with normal overrides, a dynamically typed method can
173+
implement a statically typed abstract method defined in an abstract
174+
base class.
145175
146176
.. note::
147177
148-
There are also plans to support more Python-style "duck typing" in the type system. The details are still open.
178+
There are also plans to support more Python-style "duck typing" in
179+
the type system. The details are still open.

0 commit comments

Comments
 (0)