Skip to content

Commit 1051ca4

Browse files
bpo-42345: Add whatsnew and versionchanged for typing.Literal in 3.9 (GH-23386)
* Whatsnew entry in 3.9 same as the one in 3.10. * versionchanged for typing.Literal docs Needs backport to 3.9. (cherry picked from commit e1dc0db) Co-authored-by: kj <[email protected]>
1 parent 2acd9d0 commit 1051ca4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

Doc/library/typing.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,12 @@ These can be used as types in annotations using ``[]``, each having a unique syn
655655

656656
.. versionadded:: 3.8
657657

658+
.. versionchanged:: 3.9.1
659+
``Literal`` now de-duplicates parameters. Equality comparison of
660+
``Literal`` objects are no longer order dependent. ``Literal`` objects
661+
will now raise a :exc:`TypeError` exception during equality comparisons
662+
if one of their parameters are not :term:`immutable`.
663+
658664
.. data:: ClassVar
659665

660666
Special type construct to mark class variables.

Doc/whatsnew/3.9.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,3 +1454,32 @@ Removed
14541454
``PyNullImporter_Type``, ``PyCmpWrapper_Type``, ``PySortWrapper_Type``,
14551455
``PyNoArgsFunction``.
14561456
(Contributed by Pablo Galindo Salgado in :issue:`39372`.)
1457+
1458+
Notable changes in Python 3.9.1
1459+
===============================
1460+
1461+
typing
1462+
------
1463+
1464+
The behavior of :class:`typing.Literal` was changed to conform with :pep:`586`
1465+
and to match the behavior of static type checkers specified in the PEP.
1466+
1467+
1. ``Literal`` now de-duplicates parameters.
1468+
2. Equality comparisons between ``Literal`` objects are now order independent.
1469+
3. ``Literal`` comparisons now respect types. For example,
1470+
``Literal[0] == Literal[False]`` previously evaluated to ``True``. It is
1471+
now ``False``. To support this change, the internally used type cache now
1472+
supports differentiating types.
1473+
4. ``Literal`` objects will now raise a :exc:`TypeError` exception during
1474+
equality comparisons if one of their parameters are not :term:`immutable`.
1475+
Note that declaring ``Literal`` with mutable parameters will not throw
1476+
an error::
1477+
1478+
>>> from typing import Literal
1479+
>>> Literal[{0}]
1480+
>>> Literal[{0}] == Literal[{False}]
1481+
Traceback (most recent call last):
1482+
File "<stdin>", line 1, in <module>
1483+
TypeError: unhashable type: 'set'
1484+
1485+
(Contributed by Yurii Karabas in :issue:`42345`.)

0 commit comments

Comments
 (0)