summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorIvan Levkivskyi <levkivskyi@gmail.com>2019-05-26 08:37:48 (GMT)
committerGitHub <noreply@github.com>2019-05-26 08:37:48 (GMT)
commitb891c465bb7d38a597c5c2ad547d7b19194f4dad (patch)
tree1b250227315edde01664d700c509baa3a9661ca9 /Doc/library
parentf367242d10ef36db38133a39ab7627f63099cba4 (diff)
downloadcpython-b891c465bb7d38a597c5c2ad547d7b19194f4dad.zip
cpython-b891c465bb7d38a597c5c2ad547d7b19194f4dad.tar.gz
cpython-b891c465bb7d38a597c5c2ad547d7b19194f4dad.tar.bz2
bpo-37046: PEP 586: Add Literal to typing module (#13572)
The implementation is straightforward and essentially is just copied from `typing_extensions`.
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/typing.rst22
1 files changed, 22 insertions, 0 deletions
diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index 8362f1d..e64fecb 100644
--- a/Doc/library/typing.rst
+++ b/Doc/library/typing.rst
@@ -1103,6 +1103,28 @@ The module defines the following classes, functions and decorators:
``Callable[..., Any]``, and in turn to
:class:`collections.abc.Callable`.
+.. data:: Literal
+
+ A type that can be used to indicate to type checkers that the
+ corresponding variable or function parameter has a value equivalent to
+ the provided literal (or one of several literals). For example::
+
+ def validate_simple(data: Any) -> Literal[True]: # always returns True
+ ...
+
+ MODE = Literal['r', 'rb', 'w', 'wb']
+ def open_helper(file: str, mode: MODE) -> str:
+ ...
+
+ open_helper('/some/path', 'r') # Passes type check
+ open_helper('/other/path', 'typo') # Error in type checker
+
+ ``Literal[...]`` cannot be subclassed. At runtime, an arbitrary value
+ is allowed as type argument to ``Literal[...]``, but type checkers may
+ impose restrictions. See :pep:`586` for more details about literal types.
+
+ .. versionadded:: 3.8
+
.. data:: ClassVar
Special type construct to mark class variables.