Skip to content

bpo-42345: Add whatsnew and versionchanged for typing.Literal in 3.9 #23386

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

Merged
merged 2 commits into from
Nov 19, 2020
Merged
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
6 changes: 6 additions & 0 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,12 @@ These can be used as types in annotations using ``[]``, each having a unique syn

.. versionadded:: 3.8

.. versionchanged:: 3.9.1
``Literal`` now de-duplicates parameters. Equality comparison of
``Literal`` objects are no longer order dependent. ``Literal`` objects
will now raise a :exc:`TypeError` exception during equality comparisons
if one of their parameters are not :term:`immutable`.

.. data:: ClassVar

Special type construct to mark class variables.
Expand Down
29 changes: 29 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1454,3 +1454,32 @@ Removed
``PyNullImporter_Type``, ``PyCmpWrapper_Type``, ``PySortWrapper_Type``,
``PyNoArgsFunction``.
(Contributed by Pablo Galindo Salgado in :issue:`39372`.)

Notable changes in Python 3.9.1
===============================

typing
------

The behavior of :class:`typing.Literal` was changed to conform with :pep:`586`
and to match the behavior of static type checkers specified in the PEP.

1. ``Literal`` now de-duplicates parameters.
2. Equality comparisons between ``Literal`` objects are now order independent.
3. ``Literal`` comparisons now respect types. For example,
``Literal[0] == Literal[False]`` previously evaluated to ``True``. It is
now ``False``. To support this change, the internally used type cache now
supports differentiating types.
4. ``Literal`` objects will now raise a :exc:`TypeError` exception during
equality comparisons if one of their parameters are not :term:`immutable`.
Note that declaring ``Literal`` with mutable parameters will not throw
an error::

>>> from typing import Literal
>>> Literal[{0}]
>>> Literal[{0}] == Literal[{False}]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'

(Contributed by Yurii Karabas in :issue:`42345`.)