Skip to content

Commit 6121331

Browse files
committed
revisited error_code{s,_list,_list2}.rst, extending_mypy.rst, faq.rst, final_attrs.rst
Signed-off-by: Oleg Höfling <[email protected]>
1 parent 2288371 commit 6121331

File tree

5 files changed

+37
-39
lines changed

5 files changed

+37
-39
lines changed

docs/source/error_code_list.rst

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ definition in an active scope, such as an assignment, function
8383
definition or an import. This can catch missing definitions, missing
8484
imports, and typos.
8585

86-
This example accidentally calls ``sort()`` instead of ``sorted()``:
86+
This example accidentally calls ``sort()`` instead of :py:func:`sorted`:
8787

8888
.. code-block:: python
8989
@@ -181,7 +181,7 @@ This example incorrectly uses the function ``log`` as a type:
181181
for x in objs:
182182
f(x)
183183
184-
You can use ``Callable`` as the type for callable objects:
184+
You can use :py:data:`~typing.Callable` as the type for callable objects:
185185

186186
.. code-block:: python
187187
@@ -415,11 +415,11 @@ Example:
415415
# Error: Dict entry 0 has incompatible type "str": "str"; expected "str": "int" [dict-item]
416416
d: Dict[str, int] = {'key': 'value'}
417417
418-
Check TypedDict items [typeddict-item]
419-
--------------------------------------
418+
Check ``TypedDict`` items [typeddict-item]
419+
------------------------------------------
420420

421-
When constructing a TypedDict object, mypy checks that each key and value is compatible
422-
with the TypedDict type that is inferred from the surrounding context.
421+
When constructing a ``TypedDict`` object, mypy checks that each key and value is compatible
422+
with the ``TypedDict`` type that is inferred from the surrounding context.
423423

424424
Example:
425425

@@ -542,8 +542,7 @@ Check instantiation of abstract classes [abstract]
542542

543543
Mypy generates an error if you try to instantiate an abstract base
544544
class (ABC). An abtract base class is a class with at least one
545-
abstract method or attribute. (See also :doc:`Python
546-
abc module documentation <python:library/abc>`)
545+
abstract method or attribute. (See also :py:mod:`abc` module documentation)
547546

548547
Sometimes a class is made accidentally abstract, often due to an
549548
unimplemented abstract method. In a case like this you need to provide
@@ -569,10 +568,10 @@ Example:
569568
# Error: Cannot instantiate abstract class 'Thing' with abstract attribute 'save' [abstract]
570569
t = Thing()
571570
572-
Check the target of NewType [valid-newtype]
573-
-------------------------------------------
571+
Check the target of ``NewType`` [valid-newtype]
572+
-----------------------------------------------
574573

575-
The target of a ``NewType`` definition must be a class type. It can't
574+
The target of a :py:func:`NewType <typing.NewType>` definition must be a class type. It can't
576575
be a union type, ``Any``, or various other special types.
577576

578577
You can also get this error if the target has been imported from a
@@ -593,13 +592,13 @@ To work around the issue, you can either give mypy access to the sources
593592
for ``acme`` or create a stub file for the module. See :ref:`ignore-missing-imports`
594593
for more information.
595594

596-
Check the return type of __exit__ [exit-return]
597-
-----------------------------------------------
595+
Check the return type of ``__exit__`` [exit-return]
596+
---------------------------------------------------
598597

599-
If mypy can determine that ``__exit__`` always returns ``False``, mypy
598+
If mypy can determine that :py:meth:`__exit__ <object.__exit__>` always returns ``False``, mypy
600599
checks that the return type is *not* ``bool``. The boolean value of
601600
the return type affects which lines mypy thinks are reachable after a
602-
``with`` statement, since any ``__exit__`` method that can return
601+
``with`` statement, since any :py:meth:`__exit__ <object.__exit__>` method that can return
603602
``True`` may swallow exceptions. An imprecise return type can result
604603
in mysterious errors reported near ``with`` statements.
605604

docs/source/error_code_list2.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ Example:
135135
...
136136
137137
138-
Check that function does not return Any value [no-any-return]
139-
-------------------------------------------------------------
138+
Check that function does not return ``Any`` value [no-any-return]
139+
-----------------------------------------------------------------
140140

141141
If you use ``--warn-return-any``, mypy generates an error if you return a
142142
value with an ``Any`` type in a function that is annotated to return a
@@ -155,8 +155,8 @@ Example:
155155
# Error: Returning Any from function declared to return "str" [no-any-return]
156156
return fields(x)[0]
157157
158-
Check that types have no Any components due to missing imports [no-any-unimported]
159-
----------------------------------------------------------------------------------
158+
Check that types have no ``Any`` components due to missing imports [no-any-unimported]
159+
--------------------------------------------------------------------------------------
160160

161161
If you use ``--disallow-any-unimported``, mypy generates an error if a component of
162162
a type becomes ``Any`` because mypy couldn't resolve an import. These "stealth"

docs/source/extending_mypy.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ what normally would have been the command line arguments to mypy.
1414

1515
Function ``run`` returns a ``Tuple[str, str, int]``, namely
1616
``(<normal_report>, <error_report>, <exit_status>)``, in which ``<normal_report>``
17-
is what mypy normally writes to ``sys.stdout``, ``<error_report>`` is what mypy
18-
normally writes to ``sys.stderr`` and ``exit_status`` is the exit status mypy normally
17+
is what mypy normally writes to :py:data:`sys.stdout`, ``<error_report>`` is what mypy
18+
normally writes to :py:data:`sys.stderr` and ``exit_status`` is the exit status mypy normally
1919
returns to the operating system.
2020

2121
A trivial example of using the api is the following
@@ -98,7 +98,7 @@ High-level overview
9898
*******************
9999

100100
Every entry point function should accept a single string argument
101-
that is a full mypy version and return a subclass of ``mypy.plugins.Plugin``:
101+
that is a full mypy version and return a subclass of ``mypy.plugin.Plugin``:
102102

103103
.. code-block:: python
104104
@@ -174,7 +174,7 @@ For example:
174174
instead of module level functions.
175175

176176
**get_method_signature_hook()** is used to adjust the signature of a method.
177-
This includes special Python methods except ``__init__()`` and ``__new__()``.
177+
This includes special Python methods except :py:meth:`~object.__init__` and :py:meth:`~object.__new__`.
178178
For example in this code:
179179

180180
.. code-block:: python
@@ -185,12 +185,12 @@ For example in this code:
185185
x[0] = 42
186186
187187
mypy will call ``get_method_signature_hook("ctypes.Array.__setitem__")``
188-
so that the plugin can mimic the ``ctypes`` auto-convert behavior.
188+
so that the plugin can mimic the :py:mod:`ctypes` auto-convert behavior.
189189

190-
**get_attribute_hook** overrides instance member field lookups and property
190+
**get_attribute_hook()** overrides instance member field lookups and property
191191
access (not assignments, and not method calls). This hook is only called for
192-
fields which already exist on the class. *Exception:* if ``__getattr__`` or
193-
``__getattribute__`` is a method on the class, the hook is called for all
192+
fields which already exist on the class. *Exception:* if :py:meth:`__getattr__ <object.__getattr__>` or
193+
:py:meth:`__getattribute__ <object.__getattribute__>` is a method on the class, the hook is called for all
194194
fields which do not refer to methods.
195195

196196
**get_class_decorator_hook()** can be used to update class definition for

docs/source/faq.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,16 @@ Structural subtyping can be thought of as "static duck typing".
110110
Some argue that structural subtyping is better suited for languages with duck
111111
typing such as Python. Mypy however primarily uses nominal subtyping,
112112
leaving structural subtyping mostly opt-in (except for built-in protocols
113-
such as ``Iterable`` that always support structural subtyping). Here are some
113+
such as :py:class:`~typing.Iterable` that always support structural subtyping). Here are some
114114
reasons why:
115115

116116
1. It is easy to generate short and informative error messages when
117117
using a nominal type system. This is especially important when
118118
using type inference.
119119

120-
2. Python provides built-in support for nominal ``isinstance()`` tests and
120+
2. Python provides built-in support for nominal :py:func:`isinstance` tests and
121121
they are widely used in programs. Only limited support for structural
122-
``isinstance()`` is available, and it's less type safe than
123-
nominal type tests.
122+
:py:func:`isinstance` is available, and it's less type safe than nominal type tests.
124123

125124
3. Many programmers are already familiar with static, nominal subtyping and it
126125
has been successfully used in languages such as Java, C++ and
@@ -164,7 +163,7 @@ monkey patching of methods.
164163
How is mypy different from Cython?
165164
**********************************
166165

167-
`Cython :doc:<cython:index>` is a variant of Python that supports
166+
:doc:`Cython <cython:index>` is a variant of Python that supports
168167
compilation to CPython C modules. It can give major speedups to
169168
certain classes of programs compared to CPython, and it provides
170169
static typing (though this is different from mypy). Mypy differs in

docs/source/final_attrs.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ from being overridden in a subclass:
5454
class ListView(Window):
5555
BORDER_WIDTH = 3 # Error: can't override a final attribute
5656
57-
You can use ``@property`` to make an attribute read-only, but unlike ``Final``,
57+
You can use :py:class:`@property <property>` to make an attribute read-only, but unlike ``Final``,
5858
it doesn't work with module attributes, and it doesn't prevent overriding in
5959
subclasses.
6060

@@ -83,11 +83,11 @@ You can use ``Final`` in one of these forms:
8383

8484
* Finally, you can write ``self.id: Final = 1`` (also optionally with
8585
a type in square brackets). This is allowed *only* in
86-
``__init__`` methods, so that the final instance attribute is
86+
:py:meth:`__init__ <object.__init__>` methods, so that the final instance attribute is
8787
assigned only once when an instance is created.
8888

89-
Details of using Final
90-
**********************
89+
Details of using ``Final``
90+
**************************
9191

9292
These are the two main rules for defining a final name:
9393

@@ -98,7 +98,7 @@ These are the two main rules for defining a final name:
9898
* There must be *exactly one* assignment to a final name.
9999

100100
A final attribute declared in a class body without an initializer must
101-
be initialized in the ``__init__`` method (you can skip the
101+
be initialized in the :py:meth:`__init__ <object.__init__>` method (you can skip the
102102
initializer in stub files):
103103

104104
.. code-block:: python
@@ -121,9 +121,9 @@ annotations. Using it in any other position is an error. In particular,
121121
def fun(x: Final[List[int]]) -> None: # Error!
122122
...
123123
124-
``Final`` and ``ClassVar`` should not be used together. Mypy will infer
124+
``Final`` and :py:data:`~typing.ClassVar` should not be used together. Mypy will infer
125125
the scope of a final declaration automatically depending on whether it was
126-
initialized in the class body or in ``__init__``.
126+
initialized in the class body or in :py:meth:`__init__ <object.__init__>`.
127127

128128
A final attribute can't be overridden by a subclass (even with another
129129
explicit final declaration). Note however that a final attribute can

0 commit comments

Comments
 (0)