Skip to content

Commit f8d7304

Browse files
committed
Use ... to format code within normal text
1 parent a3cf5c9 commit f8d7304

15 files changed

+134
-125
lines changed

docs/source/additional_features.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ including the following:
66

77
- inheritance between generic classes
88
- compatibility and subtyping of generic types, including covariance of generic types
9-
- super()
9+
- ``super()``

docs/source/basics.rst

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ reports type errors within the function):
2323
def greeting(name: str) -> str:
2424
return 'Hello, {}'.format(name)
2525
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
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
2828
results in a type check error:
2929

3030
.. code-block:: python
@@ -38,7 +38,7 @@ The typing module
3838
*****************
3939

4040
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
41+
if it imports the module ``typing``. Here is a complete statically typed
4242
example from the previous section:
4343

4444
.. code-block:: python
@@ -48,9 +48,9 @@ example from the previous section:
4848
def greeting(name: str) -> str:
4949
return 'Hello, {}'.format(name)
5050
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):
51+
The ``typing`` module contains many definitions that are useful in
52+
statically typed code. You can also use ``from ... import`` to import
53+
them (we'll explain Iterable later in this document):
5454

5555
.. code-block:: python
5656
@@ -60,11 +60,11 @@ statically typed code. You can also use from ... import to import them
6060
for name in names:
6161
print('Hello, {}'.format(name))
6262
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
63+
For brevity, we often omit the ``typing`` import in code examples, but
64+
you should always include it in modules that contain statically typed
6565
code.
6666

67-
You can still have dynamically typed functions in modules that import typing:
67+
You can still have dynamically typed functions in modules that import ``typing``:
6868

6969
.. code-block:: python
7070

docs/source/builtin_types.rst

+12-12
Original file line numberDiff line numberDiff line change
@@ -17,18 +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
21-
are defined in the typing module.
20+
The type ``Any`` and type constructors ``List``, ``Dict``,
21+
``Iterable`` and ``Sequence`` are defined in the ``typing`` module.
2222

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.
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.
2828

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
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
3434
collection type objects do not support indexing.

docs/source/casts.rst

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

1010
.. code-block:: python
1111
@@ -23,10 +23,14 @@ casts being checked at runtime. Use an assertion if you want to
2323
perform an actual runtime check. Casts are used to silence spurious
2424
type checker warnings.
2525

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.
26+
You don't need a cast for expressions with type ``Any``, of when
27+
assigning to a variable with type ``Any``, as was explained earlier.
2828

29-
You can cast to a dynamically typed value by just calling Any:
29+
Any() as a cast
30+
***************
31+
32+
You can cast to a dynamically typed value by just calling ``Any``;
33+
this is equivalent to ``cast(Any, ...)`` but shorter:
3034

3135
.. code-block:: python
3236

docs/source/class_basics.rst

+13-12
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ initialized within the class. Mypy infers the types of attributes:
1919
a.x = 2 # OK
2020
a.y = 3 # Error: A has no attribute y
2121
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
22+
This is a bit like each class having an implicitly defined
23+
``__slots__`` attribute. In Python semantics this is only enforced
24+
during type checking: at runtime we use standard Python semantics. You
25+
can selectively define a class as *dynamic*; dynamic classes have
2626
Python-like compile-time semantics, and they allow you to assign to
2727
arbitrary attributes anywhere in a program without the type checker
2828
complaining:
@@ -45,8 +45,8 @@ use dynamic classes when you really need them.
4545

4646
Dynamic classes are not implemented in the current mypy version.
4747

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

5151
.. code-block:: python
5252
@@ -73,7 +73,7 @@ in a method:
7373
self.y = 0 # type: Any # OK
7474
7575
You can only define an instance variable within a method if you assign
76-
to it explicitly using self:
76+
to it explicitly using ``self``:
7777

7878
.. code-block:: python
7979
@@ -110,8 +110,8 @@ override has a compatible signature:
110110
.. note::
111111

112112
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'.
113+
example, you could override the return type ``object`` with a subtype
114+
such as ``int``.
115115

116116
You can also override a statically typed method with a dynamically
117117
typed one. This allows dynamically typed code to override methods
@@ -141,9 +141,10 @@ Abstract base classes and multiple inheritance
141141
**********************************************
142142

143143
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.
144+
several built-in abstract base classes types (for example,
145+
``Sequence``, ``Iterable`` and ``Iterator``). You can define abstract
146+
base classes using the ``abc.ABCMeta`` metaclass and the
147+
``abc.abstractmethod`` function decorator.
147148

148149
.. code-block:: python
149150

docs/source/common_issues.rst

+16-15
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ can make your code difficult to understand.
4242
Second, each name within a function only has a single type. You can
4343
reuse for loop indices etc., but if you want to use a variable with
4444
multiple types within a single function, you may need to declare it
45-
with the Any type.
45+
with the ``Any`` type.
4646

4747
.. code-block:: python
4848
@@ -77,8 +77,8 @@ above example:
7777
...
7878
shape = Triangle() # OK
7979
80-
Fourth, if you use isinstance tests or other kinds of runtime type
81-
tests, you may have to add casts (this is similar to instanceof tests
80+
Fourth, if you use ``isinstance()`` tests or other kinds of runtime type
81+
tests, you may have to add casts (this is similar to ``instanceof`` tests
8282
in Java):
8383

8484
.. code-block:: python
@@ -89,18 +89,19 @@ in Java):
8989
n += 1 # o += 1 would be an error
9090
...
9191
92-
Note that the object type used in the above example is similar to
93-
Object in Java: it only supports operations defined for all objects,
94-
such as equality and isinstance(). The type Any, in contrast, supports
95-
all operations, even if they may fail at runtime. The cast above would
96-
have been unnecessary if the type of o was Any.
97-
98-
Some consider casual use of isinstance tests a sign of bad programming
99-
style. Often a method override or an overloaded function is a cleaner
100-
way of implementing functionality that depends on the runtime types of
101-
values. However, use whatever techniques that work for you. Sometimes
102-
isinstance tests *are* the cleanest way of implementing a piece of
103-
functionality.
92+
Note that the ``object`` type used in the above example is similar to
93+
``Object`` in Java: it only supports operations defined for all
94+
objects, such as equality and ``isinstance()``. The type ``Any``, in
95+
contrast, supports all operations, even if they may fail at
96+
runtime. The cast above would have been unnecessary if the type of
97+
``o`` was ``Any``.
98+
99+
Some consider casual use of ``isinstance()`` tests a sign of bad
100+
programming style. Often a method override or an overloaded function
101+
is a cleaner way of implementing functionality that depends on the
102+
runtime types of values. However, use whatever techniques that work
103+
for you. Sometimes isinstance tests *are* the cleanest way of
104+
implementing a piece of functionality.
104105

105106
Type inference in mypy is designed to work well in common cases, to be
106107
predictable and to let the type checker give useful error

docs/source/conf.py

+2
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,5 @@
262262

263263
# If true, do not generate a @detailmenu in the "Top" node's menu.
264264
#texinfo_no_detailmenu = False
265+
266+
rst_prolog = '.. |...| unicode:: U+2026 .. ellipsis\n'

docs/source/dynamic_typing.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ explicit return type are dynamically typed (operations are checked at
66
runtime). Code outside functions is statically typed by default, and
77
types of variables are inferred. This does usually the right thing,
88
but you can also make any variable dynamically typed by defining it
9-
explicitly with the type Any:
9+
explicitly with the type ``Any``:
1010

1111
.. code-block:: python
1212
@@ -17,8 +17,8 @@ explicitly with the type Any:
1717
s = 'x' # Type check error
1818
d = 'x' # OK
1919
20-
Alternatively, you can use the Undefined construct to define
21-
dynamically typed variables, as Any can be used anywhere any other
20+
Alternatively, you can use the ``Undefined`` construct to define
21+
dynamically typed variables, as ``Any`` can be used anywhere any other
2222
type is valid:
2323

2424
.. code-block:: python
@@ -29,7 +29,7 @@ type is valid:
2929
d = 1 # OK
3030
d = 'x' # OK
3131
32-
Additionally, if you don't import the typing module in a file, all
32+
Additionally, if you don't import the ``typing`` module in a file, all
3333
code outside functions will be dynamically typed by default, and the
3434
file is not type checked at all. This mode makes it easy to include
3535
existing Python code that is not trivially compatible with static
@@ -38,4 +38,4 @@ typing.
3838
.. note::
3939

4040
The current mypy version type checks all modules, even those that
41-
don't import typing. This will change in a future version.
41+
don't import ``typing``. This will change in a future version.

docs/source/faq.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ dynamic or 'patchable').
164164
How is mypy different from PyPy?
165165
********************************
166166

167-
*This answer relates to PyPy as a Python implementation. See also the
168-
answer related to RPython below.*
167+
*This answer relates to PyPy as a Python implementation. See also the answer related to RPython below.*
169168

170169
Mypy and PyPy are orthogonal. Mypy does static type checking, i.e. it
171170
is basically a linter, but static typing has no runtime effect,

docs/source/function_overloading.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ programmer.
3939

4040
As generic type variables are erased at runtime, an overloaded
4141
function cannot dispatch based on a generic type argument,
42-
e.g. List[int] versus List[str].
42+
e.g. ``List[int]`` versus ``List[str]``.

docs/source/generics.rst

+19-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Defining generic classes
66

77
The built-in collection classes are generic classes. Generic types
88
have one or more type parameters, which can be arbitrary types. For
9-
example, Dict]int, str] has the type parameters int and str, and
10-
List[int] has a type parameter int.
9+
example, ``Dict[int, str]`` has the type parameters ``int`` and
10+
``str``, and ``List[int]`` has a type parameter ``int``.
1111

1212
Programs can also define new generic classes. Here is a very simple
1313
generic class that represents a stack:
@@ -31,10 +31,10 @@ generic class that represents a stack:
3131
def empty(self) -> bool:
3232
return not self.items
3333
34-
The Stack class can be used to represent a stack of any type:
35-
Stack[int], Stack[Tuple[int, str]], etc.
34+
The ``Stack`` class can be used to represent a stack of any type:
35+
``Stack[int]``, ``Stack[Tuple[int, str]]``, etc.
3636

37-
Using Stack is similar to built-in container types:
37+
Using ``Stack`` is similar to built-in container types:
3838

3939
.. code-block:: python
4040
@@ -54,31 +54,31 @@ Type inference works for user-defined generic types as well:
5454
Generic class internals
5555
***********************
5656

57-
You may wonder what happens at runtime when you index Stack. Actually,
58-
indexing Stack just returns Stack:
57+
You may wonder what happens at runtime when you index
58+
``Stack``. Actually, indexing ``Stack`` just returns ``Stack``:
5959

6060
>>> print(Stack)
6161
<class '__main__.Stack'>
6262
>>> print(Stack[int])
6363
<class '__main__.Stack'>
6464

65-
Note that built-in types list, dict and so on do not support indexing
66-
in Python. This is why we have the aliases List, Dict and so on in the
67-
typing module. Indexing these aliases just gives you the target class
68-
in Python, similar to Stack:
65+
Note that built-in types ``list``, ``dict`` and so on do not support
66+
indexing in Python. This is why we have the aliases ``List``, ``Dict``
67+
and so on in the ``typing`` module. Indexing these aliases just gives
68+
you the target class in Python, similar to ``Stack``:
6969

7070
>>> from typing import List
7171
>>> List[int]
7272
<class 'list'>
7373

7474
The above examples illustrate that type variables are erased at
75-
runtime when running in a Python VM. Generic Stack or list instances
76-
are just ordinary Python objects, and they have no extra runtime
77-
overhead or magic due to being generic, other than a metaclass that
78-
overloads the indexing operator. If you worry about the overhead
79-
introduced by the type indexing operation when constructing instances,
80-
you can often rewrite such code using a # type annotation, which has
81-
no runtime impact:
75+
runtime. Generic ``Stack`` or ``list`` instances are just ordinary
76+
Python objects, and they have no extra runtime overhead or magic due
77+
to being generic, other than a metaclass that overloads the indexing
78+
operator. If you worry about the overhead introduced by the type
79+
indexing operation when constructing instances, you can usually
80+
rewrite such code using a ``# type:`` annotation, which has no runtime
81+
impact:
8282

8383
.. code-block:: python
8484
@@ -118,7 +118,7 @@ return type is derived from the sequence item type. For example:
118118
s = first('foo') # s has type str.
119119
n = first([1, 2, 3]) # n has type int.
120120
121-
Note also that a single definition of a type variable (such as T
121+
Note also that a single definition of a type variable (such as ``T``
122122
above) can be used in multiple generic functions or classes. In this
123123
example we use the same type variable in two generic functions:
124124

0 commit comments

Comments
 (0)