Repository navigation
Simplify Literal specification by removing redundant sections #2395
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
Open
Anjor99
wants to merge
2
commits into
python:main
Choose a base branch
from
Anjor99:remove-redundant-literal-sections
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+11
−74
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -84,8 +84,8 @@ However, Literal **must** be parameterized with at least one type. | |
| Types like ``Literal[]`` or ``Literal`` are illegal. | ||
|
|
||
|
|
||
| Legal and illegal parameterizations | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| Legal parameterizations | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
|
||
| This section describes what exactly constitutes a legal ``Literal[...]`` type: | ||
| what values may and may not be used as parameters. | ||
|
|
@@ -95,8 +95,8 @@ expressions, and nothing else. | |
|
|
||
| .. _literal-legal-parameters: | ||
|
|
||
| Legal parameters for ``Literal`` at type check time | ||
| """"""""""""""""""""""""""""""""""""""""""""""""""" | ||
| Legal parameters for ``Literal`` | ||
| """""""""""""""""""""""""""""""" | ||
|
|
||
| ``Literal`` may be parameterized with literal ``int``, ``str``, ``bytes``, | ||
| and ``bool`` objects, instances of ``enum.Enum`` subclasses, and ``None``. So for example, all of | ||
|
|
@@ -117,10 +117,17 @@ the following would be legal:: | |
| Literal[Color.RED] | ||
| Literal[None] | ||
|
|
||
| The only exceptions to this rule are the unary ``-`` (minus) and unary | ||
| ``+`` (plus) for ints: types like ``Literal[-5]`` and ``Literal[+1]`` | ||
| are *accepted*. | ||
|
|
||
| **Note:** Since the type ``None`` is inhabited by just a single | ||
| value, the types ``None`` and ``Literal[None]`` are exactly equivalent. | ||
| Type checkers may simplify ``Literal[None]`` into just ``None``. | ||
|
|
||
| Arbitrary expressions like ``Literal[3 + 4]`` or | ||
| ``Literal["foo".replace("o", "b")]`` are not allowed. | ||
|
|
||
| ``Literal`` may also be parameterized by other literal types, or type aliases | ||
| to other literal types. For example, the following is legal:: | ||
|
|
||
|
|
@@ -148,76 +155,6 @@ This should be exactly equivalent to the following type:: | |
|
|
||
| Literal[1, 2, 3, "foo", 5] | None | ||
|
|
||
| Illegal parameters for ``Literal`` at type check time | ||
| """"""""""""""""""""""""""""""""""""""""""""""""""""" | ||
|
|
||
| The following parameters are intentionally disallowed by design: | ||
|
|
||
| - Arbitrary expressions like ``Literal[3 + 4]`` or | ||
| ``Literal["foo".replace("o", "b")]``. | ||
|
|
||
| - Rationale: Literal types are meant to be a | ||
| minimal extension to the typing ecosystem and requiring type | ||
| checkers to interpret potentially expressions inside types adds too | ||
| much complexity. | ||
|
|
||
| - As a consequence, complex numbers like ``Literal[4 + 3j]`` and | ||
| ``Literal[-4 + 2j]`` are also prohibited. For consistency, literals like | ||
| ``Literal[4j]`` that contain just a single complex number are also | ||
| prohibited. | ||
|
|
||
| - The only exceptions to this rule are the unary ``-`` (minus) and unary ``+`` (plus) for ints: types | ||
| like ``Literal[-5]`` and ``Literal[+1]`` are *accepted*. | ||
|
Comment on lines
-169
to
-170
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 isn't mentioned anywhere else in the spec, so it should stay (or moved elsewhere) |
||
|
|
||
| - Tuples containing valid literal types like ``Literal[(1, "foo", "bar")]``. | ||
| The user could always express this type as | ||
| ``tuple[Literal[1], Literal["foo"], Literal["bar"]]`` instead. Also, | ||
| tuples are likely to be confused with the ``Literal[1, 2, 3]`` | ||
| shortcut. | ||
|
|
||
| - Mutable literal data structures like dict literals, list literals, or | ||
| set literals: literals are always implicitly final and immutable. So, | ||
| ``Literal[{"a": "b", "c": "d"}]`` is illegal. | ||
|
|
||
| - Any other types: for example, ``Literal[Path]``, or | ||
| ``Literal[some_object_instance]`` are illegal. This includes typevars: if | ||
| ``T`` is a typevar, ``Literal[T]`` is not allowed. Typevars can vary over | ||
| only types, never over values. | ||
|
|
||
| The following are provisionally disallowed for simplicity. We can consider | ||
| allowing them in the future. | ||
|
|
||
| - Floats: e.g. ``Literal[3.14]``. Representing Literals of infinity or NaN | ||
| in a clean way is tricky; real-world APIs are unlikely to vary their | ||
| behavior based on a float parameter. | ||
|
|
||
| - Any: e.g. ``Literal[Any]``. ``Any`` is a type, and ``Literal[...]`` is | ||
| meant to contain values only. It is also unclear what ``Literal[Any]`` | ||
| would actually semantically mean. | ||
|
|
||
| Parameters at runtime | ||
| """"""""""""""""""""" | ||
|
|
||
| Although the set of parameters ``Literal[...]`` may contain at type check time | ||
| is very small, the actual implementation of ``typing.Literal`` will not perform | ||
| any checks at runtime. For example:: | ||
|
|
||
| def my_function(x: Literal[1 + 2]) -> int: | ||
| return x * 3 | ||
|
|
||
| x: Literal = 3 | ||
| y: Literal[my_function] = my_function | ||
|
|
||
| The type checker should reject this program: all three uses of | ||
| ``Literal`` are *invalid* according to this spec. However, Python itself | ||
| should execute this program with no errors. | ||
|
|
||
| This is partly to help us preserve flexibility in case we want to expand the | ||
| scope of what ``Literal`` can be used for in the future, and partly because | ||
| it is not possible to detect all illegal parameters at runtime to begin with. | ||
| For example, it is impossible to distinguish between ``Literal[1 + 2]`` and | ||
| ``Literal[3]`` at runtime. | ||
|
|
||
| Literals, enums, and forward references | ||
| """"""""""""""""""""""""""""""""""""""" | ||
|
|
||
|
|
||
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.
The conformance test quotes this, so I'm not sure if getting rid of it like is wise:
typing/conformance/tests/literals_parameterizations.py
Line 40 in 07c1001