-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Various doc updates #3314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Various doc updates #3314
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3148825
Docs for flexible Callable
19b533b
Document --cache-dir=/dev/null
52de25d
Document quick mode
4064a3a
Update usage message to the most recent one
8101de7
Add some docs and an example for ClassVar.
c186459
Address code review
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,6 +188,130 @@ using bidirectional type inference: | |
If you want to give the argument or return value types explicitly, use | ||
an ordinary, perhaps nested function definition. | ||
|
||
Extended Callable types | ||
*********************** | ||
|
||
As an experimental mypy extension, you can specify ``Callable`` types | ||
that support keyword arguments, optional arguments, and more. Where | ||
you specify the arguments of a Callable, you can choose to supply just | ||
the type of a nameless positional argument, or an "argument specifier" | ||
representing a more complicated form of argument. This allows one to | ||
more closely emulate the full range of possibilities given by the | ||
``def`` statement in Python. | ||
|
||
As an example, here's a complicated function definition and the | ||
corresponding ``Callable``: | ||
|
||
.. code-block:: python | ||
|
||
from typing import Callable | ||
from mypy_extensions import (Arg, DefaultArg, NamedArg, | ||
DefaultNamedArg, VarArg, KwArg) | ||
|
||
def func(__a: int, # This convention is for nameless arguments | ||
b: int, | ||
c: int = 0, | ||
*args: int, | ||
d: int, | ||
e: int = 0, | ||
**kwargs: int) -> int: | ||
... | ||
|
||
F = Callable[[int, # Or Arg(int) | ||
Arg(int, 'b'), | ||
DefaultArg(int, 'c'), | ||
VarArg(int), | ||
NamedArg(int, 'd'), | ||
DefaultNamedArg(int, 'e'), | ||
KwArg(int)], | ||
int] | ||
|
||
f: F = func | ||
|
||
Argument specifiers are special function calls that can specify the | ||
following aspects of an argument: | ||
|
||
- its type (the only thing that the basic format supports) | ||
|
||
- its name (if it has one) | ||
|
||
- whether it may be omitted | ||
|
||
- whether it may or must be passed using a keyword | ||
|
||
- whether it is a ``*args`` argument (representing the remaining | ||
positional arguments) | ||
|
||
- whether it is a ``**kwargs`` argument (representing the remaining | ||
keyword arguments) | ||
|
||
The following functions are available in ``mypy_extensions`` for this | ||
purpose: | ||
|
||
.. code-block:: python | ||
|
||
def Arg(type=Any, name=None): | ||
# A normal, mandatory, positional argument. | ||
# If the name is specified it may be passed as a keyword. | ||
|
||
def DefaultArg(type=Any, name=None): | ||
# An optional positional argument (i.e. with a default value). | ||
# If the name is specified it may be passed as a keyword. | ||
|
||
def NamedArg(type=Any, name=None): | ||
# A mandatory keyword-only argument. | ||
|
||
def DefaultNamedArg(type=Any, name=None): | ||
# An optional keyword-only argument (i.e. with a default value). | ||
|
||
def VarArg(type=Any): | ||
# A *args-style variadic positional argument. | ||
# A single VarArg() specifier represents all remaining | ||
# positional arguments. | ||
|
||
def KwArg(type=Any): | ||
# A **kwargs-style variadic keyword argument. | ||
# A single KwArg() specifier represents all remaining | ||
# keyword arguments. | ||
|
||
In all cases, the ``type`` argument defaults to ``Any``, and if the | ||
``name`` argument is omitted the argument has no name (the name is | ||
required for ``NamedArg`` and ``DefaultNamedArg``). A basic | ||
``Callable`` such as | ||
|
||
.. code-block:: python | ||
|
||
MyFunc = Callable[[int, str, int], float] | ||
|
||
is equivalent to the following: | ||
|
||
.. code-block:: python | ||
|
||
MyFunc = Callable[[Arg(int), Arg(str), Arg(int)], float] | ||
|
||
A ``Callable`` with unspecified argument types, such as | ||
|
||
.. code-block:: python | ||
|
||
MyOtherFunc = Callable[..., int] | ||
|
||
is (roughly) equivalent to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like "roughly" here 😈 |
||
|
||
.. code-block:: python | ||
|
||
MyOtherFunc = Callable[[VarArg(), KwArg()], int] | ||
|
||
.. note:: | ||
|
||
This feature is experimental. Details of the implementation may | ||
change and there may be unknown limitations. **IMPORTANT:** | ||
Each of the functions above currently just returns its ``type`` | ||
argument, so the information contained in the argument specifiers | ||
is not available at runtime. This limitation is necessary for | ||
backwards compatibility with the existing ``typing.py`` module as | ||
present in the Python 3.5+ standard library and distributed via | ||
PyPI. | ||
|
||
.. _union-types: | ||
|
||
Union types | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This equivalency is near, but not entirely true. A
Callable[..., int]
getsis_ellipsis_args
set toTrue
, unlikeCallable[[VarArg(), KwArg()], int]
. A Callable with ellipsis args is not only considered a subtype of all Callables with compatible return types (which it naturally is), but also a supertype of all Callables with compatible return types (which is weird and special). This is the only other wart in the type system I know of that's comparable toAny
.