Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ For conversations that are more suitable to a chat platform, you can use one of

## Repository Content

This GitHub repository is used for several things:
This GitHub repository serves multiple purposes related to Python's static type system:
Copy link
Collaborator

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.



- The documentation at [typing.python.org](https://typing.python.org/)
is maintained in the [docs directory](./docs). This includes the
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]/>`_

Expand Down
5 changes: 5 additions & 0 deletions docs/spec/index.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Specification for the Python type system

========================================

This section provides the official specification for Python’s static type system.
It defines the rules, terminology, and behaviors that type checkers and typing-related tools are expected to follow.


.. toctree::
:maxdepth: 2
:caption: Contents:
Expand Down
23 changes: 22 additions & 1 deletion docs/spec/literal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Literals
``Literal``
-----------

(Originally specified in :pep:`586`.)
(Originally specified in :pep:`586`, which introduced ``Literal`` types.)


Core Semantics
Expand All @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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``.

Expand All @@ -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.
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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,
and that the clarification about inference precision is redundant in a spec context.

I’ll revise this PR to remove the example and spec clarifications, and I’ll
follow up with a separate, focused PR to add the example to the appropriate
guide section instead.

Thanks again for taking the time to review.



This "inheriting" behavior is identical to how we
:ref:`handle NewTypes <newtype>`.

Expand Down