-
Notifications
You must be signed in to change notification settings - Fork 281
Add cross-references and clarifications to Literal type specification #2130
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
Changes from all commits
b3786ee
684f40e
e685406
06cb232
8dcf0fe
bedc17f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,7 +69,7 @@ Discussions and Support | |
| ======================= | ||
|
|
||
| * `User help forum <https://github.com/python/typing/discussions>`_ | ||
| * `User chat on Gitter <http://gitter.im/python/typing>`_ | ||
| * `User chat on Gitter <https://gitter.im/python/typing>`_ | ||
| * `Developer forum <https://discuss.python.org/c/typing/32>`_ | ||
| * `Developer mailing list (archived) <https://mail.python.org/archives/list/[email protected]/>`_ | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ Literals | |
| ``Literal`` | ||
| ----------- | ||
|
|
||
| (Originally specified in :pep:`586`.) | ||
| (Originally specified in :pep:`586`, which introduced ``Literal`` types.) | ||
|
|
||
|
|
||
| Core Semantics | ||
|
|
@@ -24,6 +24,24 @@ concrete value. For example, if we define some variable ``foo`` to have | |
| type ``Literal[3]``, we are declaring that ``foo`` must be exactly equal | ||
| to ``3`` and no other value. | ||
|
|
||
| Example | ||
|
Collaborator
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. This example looks like something that should go in the guides, not the spec. |
||
| ^^^^^^^ | ||
|
|
||
| Literal types are most useful when a function accepts only a fixed set of | ||
| allowed constant values. Type checkers can then verify that callers provide | ||
| only those values. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from typing import Literal | ||
|
|
||
| def set_mode(mode: Literal["fast", "slow"]) -> None: | ||
| print(f"Running in {mode} mode") | ||
|
|
||
| set_mode("fast") # OK | ||
| set_mode("other") # Type checker error | ||
|
|
||
|
|
||
| Given some value ``v`` that is a member of type ``T``, the type ``Literal[v]`` | ||
| is a subtype of ``T``. For example, ``Literal[3]`` is a subtype of ``int``. | ||
|
|
||
|
|
@@ -32,6 +50,9 @@ literal type. So, if we have some variable ``foo`` of type ``Literal[3]`` | |
| it’s safe to do things like ``foo + 5`` since ``foo`` inherits ``int``’s | ||
| ``__add__`` method. The resulting type of ``foo + 5`` is ``int``. | ||
|
|
||
| The specification does not mandate the level of precision required for such inference, and type checkers may choose to infer a more precise ``Literal`` result where possible. | ||
|
Collaborator
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. In general, if the spec doesn't mandate behavior, it's assumed that type checkers are free to choose their own behavior, so I don't think this is necessary to state here.
Author
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. Thanks for the detailed feedback — that makes sense. I agree that the example fits better in the guides rather than the specification, I’ll revise this PR to remove the example and spec clarifications, and I’ll Thanks again for taking the time to review. |
||
|
|
||
|
|
||
| This "inheriting" behavior is identical to how we | ||
| :ref:`handle NewTypes <newtype>`. | ||
|
|
||
|
|
||
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.
I don't think this is a useful change.