Skip to content

Commit dfb94c1

Browse files
authored
Various doc updates (#3314)
* flexible Callable * --cache-dir=/dev/null * --quick mode * Refresh usage message * ClassVar
1 parent d432240 commit dfb94c1

File tree

4 files changed

+184
-10
lines changed

4 files changed

+184
-10
lines changed

docs/source/command_line.rst

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ flag (or its long form ``--help``)::
1414
[--disallow-untyped-calls] [--disallow-untyped-defs]
1515
[--check-untyped-defs] [--disallow-subclassing-any]
1616
[--warn-incomplete-stub] [--warn-redundant-casts]
17-
[--warn-no-return] [--warn-unused-ignores] [--show-error-context]
18-
[-i] [--cache-dir DIR] [--strict-optional]
19-
[--strict-optional-whitelist [GLOB [GLOB ...]]] [--strict]
17+
[--no-warn-no-return] [--warn-return-any] [--warn-unused-ignores]
18+
[--show-error-context] [-i] [--quick-and-dirty] [--cache-dir DIR]
19+
[--strict-optional]
20+
[--strict-optional-whitelist [GLOB [GLOB ...]]]
2021
[--junit-xml JUNIT_XML] [--pdb] [--show-traceback] [--stats]
2122
[--inferstats] [--custom-typing MODULE]
2223
[--custom-typeshed-dir DIR] [--scripts-are-modules]
2324
[--config-file CONFIG_FILE] [--show-column-numbers]
24-
[--find-occurrences CLASS.MEMBER]
25+
[--find-occurrences CLASS.MEMBER] [--strict] [--strict-boolean]
2526
[--cobertura-xml-report DIR] [--html-report DIR]
2627
[--linecount-report DIR] [--linecoverage-report DIR]
2728
[--memory-xml-report DIR] [--old-html-report DIR]
@@ -298,10 +299,31 @@ Here are some more useful flags:
298299
the base class even though that may not actually be the case. This
299300
flag makes mypy raise an error instead.
300301

301-
- ``--incremental`` is an experimental option that enables incremental
302-
type checking. When enabled, mypy caches results from previous runs
302+
.. _incremental:
303+
304+
- ``--incremental`` is an experimental option that enables a module
305+
cache. When enabled, mypy caches results from previous runs
303306
to speed up type checking. Incremental mode can help when most parts
304-
of your program haven't changed since the previous mypy run.
307+
of your program haven't changed since the previous mypy run. A
308+
companion flag is ``--cache-dir DIR``, which specifies where the
309+
cache files are written. By default this is ``.mypy_cache`` in the
310+
current directory. While the cache is only read in incremental
311+
mode, it is written even in non-incremental mode, in order to "warm"
312+
the cache. To disable writing the cache, use
313+
``--cache-dir=/dev/null`` (UNIX) or ``--cache-dir=nul`` (Windows).
314+
Cache files belonging to a different mypy version are ignored.
315+
316+
.. _quick:
317+
318+
- ``--quick-and-dirty`` is an experimental, unsafe variant of
319+
:ref:`incremental mode <incremental>`. Quick mode is faster than
320+
regular incremental mode, because it only re-checks modules that
321+
were modified since their cache file was last written (regular
322+
incremental mode also re-checks all modules that depend on one or
323+
more modules that were re-checked). Quick mode is unsafe because it
324+
may miss problems caused by a change in a dependency. Quick mode
325+
updates the cache, but regular incremental mode ignores cache files
326+
written by quick mode.
305327

306328
- ``--python-version X.Y`` will make mypy typecheck your code as if it were
307329
run under Python version X.Y. Without this option, mypy will default to using

docs/source/config_file.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,17 @@ The following global flags may only be set in the global section
9393
- ``dump_inference_stats`` (Boolean, default False) dumps stats about
9494
type inference.
9595

96-
- ``incremental`` (Boolean, default False) enables the experimental
97-
module cache.
96+
- ``incremental`` (Boolean, default False) enables :ref:`incremental
97+
mode <incremental>`.
9898

9999
- ``cache_dir`` (string, default ``.mypy_cache``) stores module cache
100-
info in the given folder in incremental mode.
100+
info in the given folder in :ref:`incremental mode <incremental>`.
101+
The cache is only read in incremental mode, but it is always written
102+
unless the value is set to ``/dev/null`` (UNIX) or ``nul``
103+
(Windows).
104+
105+
- ``quick_and_dirty`` (Boolean, default False) enables :ref:`quick
106+
mode <quick>`.
101107

102108
- ``show_error_context`` (Boolean, default False) shows
103109
context notes before errors.

docs/source/kinds_of_types.rst

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,130 @@ using bidirectional type inference:
188188
If you want to give the argument or return value types explicitly, use
189189
an ordinary, perhaps nested function definition.
190190

191+
Extended Callable types
192+
***********************
193+
194+
As an experimental mypy extension, you can specify ``Callable`` types
195+
that support keyword arguments, optional arguments, and more. Where
196+
you specify the arguments of a Callable, you can choose to supply just
197+
the type of a nameless positional argument, or an "argument specifier"
198+
representing a more complicated form of argument. This allows one to
199+
more closely emulate the full range of possibilities given by the
200+
``def`` statement in Python.
201+
202+
As an example, here's a complicated function definition and the
203+
corresponding ``Callable``:
204+
205+
.. code-block:: python
206+
207+
from typing import Callable
208+
from mypy_extensions import (Arg, DefaultArg, NamedArg,
209+
DefaultNamedArg, VarArg, KwArg)
210+
211+
def func(__a: int, # This convention is for nameless arguments
212+
b: int,
213+
c: int = 0,
214+
*args: int,
215+
d: int,
216+
e: int = 0,
217+
**kwargs: int) -> int:
218+
...
219+
220+
F = Callable[[int, # Or Arg(int)
221+
Arg(int, 'b'),
222+
DefaultArg(int, 'c'),
223+
VarArg(int),
224+
NamedArg(int, 'd'),
225+
DefaultNamedArg(int, 'e'),
226+
KwArg(int)],
227+
int]
228+
229+
f: F = func
230+
231+
Argument specifiers are special function calls that can specify the
232+
following aspects of an argument:
233+
234+
- its type (the only thing that the basic format supports)
235+
236+
- its name (if it has one)
237+
238+
- whether it may be omitted
239+
240+
- whether it may or must be passed using a keyword
241+
242+
- whether it is a ``*args`` argument (representing the remaining
243+
positional arguments)
244+
245+
- whether it is a ``**kwargs`` argument (representing the remaining
246+
keyword arguments)
247+
248+
The following functions are available in ``mypy_extensions`` for this
249+
purpose:
250+
251+
.. code-block:: python
252+
253+
def Arg(type=Any, name=None):
254+
# A normal, mandatory, positional argument.
255+
# If the name is specified it may be passed as a keyword.
256+
257+
def DefaultArg(type=Any, name=None):
258+
# An optional positional argument (i.e. with a default value).
259+
# If the name is specified it may be passed as a keyword.
260+
261+
def NamedArg(type=Any, name=None):
262+
# A mandatory keyword-only argument.
263+
264+
def DefaultNamedArg(type=Any, name=None):
265+
# An optional keyword-only argument (i.e. with a default value).
266+
267+
def VarArg(type=Any):
268+
# A *args-style variadic positional argument.
269+
# A single VarArg() specifier represents all remaining
270+
# positional arguments.
271+
272+
def KwArg(type=Any):
273+
# A **kwargs-style variadic keyword argument.
274+
# A single KwArg() specifier represents all remaining
275+
# keyword arguments.
276+
277+
In all cases, the ``type`` argument defaults to ``Any``, and if the
278+
``name`` argument is omitted the argument has no name (the name is
279+
required for ``NamedArg`` and ``DefaultNamedArg``). A basic
280+
``Callable`` such as
281+
282+
.. code-block:: python
283+
284+
MyFunc = Callable[[int, str, int], float]
285+
286+
is equivalent to the following:
287+
288+
.. code-block:: python
289+
290+
MyFunc = Callable[[Arg(int), Arg(str), Arg(int)], float]
291+
292+
A ``Callable`` with unspecified argument types, such as
293+
294+
.. code-block:: python
295+
296+
MyOtherFunc = Callable[..., int]
297+
298+
is (roughly) equivalent to
299+
300+
.. code-block:: python
301+
302+
MyOtherFunc = Callable[[VarArg(), KwArg()], int]
303+
304+
.. note::
305+
306+
This feature is experimental. Details of the implementation may
307+
change and there may be unknown limitations. **IMPORTANT:**
308+
Each of the functions above currently just returns its ``type``
309+
argument, so the information contained in the argument specifiers
310+
is not available at runtime. This limitation is necessary for
311+
backwards compatibility with the existing ``typing.py`` module as
312+
present in the Python 3.5+ standard library and distributed via
313+
PyPI.
314+
191315
.. _union-types:
192316

193317
Union types

docs/source/python36.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ now have type annotations using either of the two forms:
1616

1717
.. code-block:: python
1818
19+
from typing import Optional
1920
foo: Optional[int]
2021
bar: List[str] = []
2122
@@ -26,6 +27,27 @@ Mypy fully supports this syntax, interpreting them as equivalent to
2627
foo = None # type: Optional[int]
2728
bar = [] # type: List[str]
2829
30+
An additional feature defined in PEP 526 is also supported: you can
31+
mark names intended to be used as class variables with ``ClassVar``.
32+
In a pinch you can also use ClassVar in ``# type`` comments.
33+
Example:
34+
35+
.. code-block:: python
36+
37+
from typing import ClassVar
38+
39+
class C:
40+
x: int # instance variable
41+
y: ClassVar[int] # class variable
42+
z = None # type: ClassVar[int]
43+
44+
def foo(self) -> None:
45+
self.x = 0 # OK
46+
self.y = 0 # Error: Cannot assign to class variable "y" via instance
47+
48+
C.y = 0 # This is OK
49+
50+
2951
Literal string formatting (`PEP 498 <https://www.python.org/dev/peps/pep-0498>`_)
3052
---------------------------------------------------------------------------------
3153

0 commit comments

Comments
 (0)