From 9a06f2c7a5f266d4d627529b275c9b01d7121ef2 Mon Sep 17 00:00:00 2001 From: Anjor Date: Fri, 27 Nov 2026 00:39:25 +0530 Subject: [PATCH 1/2] Remove redundant Literal parameter restrictions and runtime details --- docs/spec/literal.rst | 70 ------------------------------------------- 1 file changed, 70 deletions(-) diff --git a/docs/spec/literal.rst b/docs/spec/literal.rst index 7254632b2..29066b359 100644 --- a/docs/spec/literal.rst +++ b/docs/spec/literal.rst @@ -148,76 +148,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*. - -- 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 """"""""""""""""""""""""""""""""""""""" From 2cac23d8f4e39f733fa108f545de087630e96c0c Mon Sep 17 00:00:00 2001 From: Anjor Date: Fri, 27 Nov 2026 01:48:57 +0530 Subject: [PATCH 2/2] Simplify Literal parameterization specification Remove redundant illegal-parameter and runtime sections while preserving restrictions on valid Literal parameters. --- docs/spec/literal.rst | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/docs/spec/literal.rst b/docs/spec/literal.rst index 29066b359..100de78ff 100644 --- a/docs/spec/literal.rst +++ b/docs/spec/literal.rst @@ -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::