From e134c43807a94ca5290409c5c5bfa7b5dc97e1a2 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 19 Nov 2020 11:04:41 +0800 Subject: [PATCH 1/7] Add whatsnew for Literal in 3.10 --- Doc/whatsnew/3.10.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 95594413440b85a..b844dbdff180770 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -300,6 +300,27 @@ and :data:`types.NotImplementedType` classes, providing a new set of types readily interpretable by type checkers. (Contributed by Bas van Beek in :issue:`41810`.) +typing +------ + +The behavior of :class:`typing.Literal` was changed. + 1. ``Literal`` now de-duplicates parameters. + 2. Equality comparisons between ``Literal`` objects are now order independent. + Their :func:`hash` results are also order independent. + 3. ``Literal`` objects will now raise a :exc:`TypeError` exception during + equality comparisons if one of their parameters are :term:`immutable` to + conform with :pep:`586`. 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 "", line 1, in + TypeError: unhashable type: 'set' + +(Contributed by Yurii Karabas in :issue:`42345`.) + unittest -------- From a5f43b0f1438c799ed13be4b91224d468d8ddcfe Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 19 Nov 2020 11:16:35 +0800 Subject: [PATCH 2/7] add info about type cache --- Doc/whatsnew/3.10.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index b844dbdff180770..d6c10f8abeacb6c 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -303,11 +303,17 @@ of types readily interpretable by type checkers. typing ------ +The internally used type cache now supports differentiating types. For example, +``Literal[0] == Literal[False]`` previously evaluated to ``True``. It is now +``False``. +(Contributed by Yurii Karabas in :issue:`42345`.) + The behavior of :class:`typing.Literal` was changed. 1. ``Literal`` now de-duplicates parameters. 2. Equality comparisons between ``Literal`` objects are now order independent. Their :func:`hash` results are also order independent. - 3. ``Literal`` objects will now raise a :exc:`TypeError` exception during + 3. ``Literal`` comparisons now respects types. + 4. ``Literal`` objects will now raise a :exc:`TypeError` exception during equality comparisons if one of their parameters are :term:`immutable` to conform with :pep:`586`. Note that declaring ``Literal`` with mutable parameters will not throw an error.:: From 912cbd7d2e5d314fd3a408dbc29b23274d9358e6 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 19 Nov 2020 11:19:52 +0800 Subject: [PATCH 3/7] stray period --- Doc/whatsnew/3.10.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index d6c10f8abeacb6c..53046c246517b9f 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -316,7 +316,7 @@ The behavior of :class:`typing.Literal` was changed. 4. ``Literal`` objects will now raise a :exc:`TypeError` exception during equality comparisons if one of their parameters are :term:`immutable` to conform with :pep:`586`. Note that declaring ``Literal`` with mutable - parameters will not throw an error.:: + parameters will not throw an error:: >>> from typing import Literal >>> Literal[{0}] From 4d7700c42ffd983f65f9d70aac4d80a4322a07f1 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 19 Nov 2020 11:35:20 +0800 Subject: [PATCH 4/7] immutable -> not immutable --- Doc/whatsnew/3.10.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 53046c246517b9f..5d8bb3d724e51d7 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -314,8 +314,8 @@ The behavior of :class:`typing.Literal` was changed. Their :func:`hash` results are also order independent. 3. ``Literal`` comparisons now respects types. 4. ``Literal`` objects will now raise a :exc:`TypeError` exception during - equality comparisons if one of their parameters are :term:`immutable` to - conform with :pep:`586`. Note that declaring ``Literal`` with mutable + equality comparisons if one of their parameters are not :term:`immutable` + to conform with :pep:`586`. Note that declaring ``Literal`` with mutable parameters will not throw an error:: >>> from typing import Literal From 4392517ebec6a6ea2e083224a7dc33aee446d12e Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 19 Nov 2020 11:44:10 +0800 Subject: [PATCH 5/7] add suggestions --- Doc/whatsnew/3.10.rst | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 5d8bb3d724e51d7..6a6d623371524b5 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -303,16 +303,14 @@ of types readily interpretable by type checkers. typing ------ -The internally used type cache now supports differentiating types. For example, -``Literal[0] == Literal[False]`` previously evaluated to ``True``. It is now -``False``. -(Contributed by Yurii Karabas in :issue:`42345`.) - The behavior of :class:`typing.Literal` was changed. 1. ``Literal`` now de-duplicates parameters. 2. Equality comparisons between ``Literal`` objects are now order independent. - Their :func:`hash` results are also order independent. - 3. ``Literal`` comparisons now respects types. + 3. ``Literal`` comparisons now respects types. For example, + ``Literal[0] == Literal[False]`` previously evaluated to ``True``. It is + now ``False``. This matches the behavior of static type checkers as per + :pep:`586`. 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` to conform with :pep:`586`. Note that declaring ``Literal`` with mutable From 75c6ebd9c6fa57e12e3eed37a29b95c6e9bae38f Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 19 Nov 2020 11:47:26 +0800 Subject: [PATCH 6/7] merge repetitive mentions of pep --- Doc/whatsnew/3.10.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 6a6d623371524b5..58b2fa4f1d0fe24 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -303,18 +303,18 @@ of types readily interpretable by type checkers. typing ------ -The behavior of :class:`typing.Literal` was changed. +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 respects types. For example, ``Literal[0] == Literal[False]`` previously evaluated to ``True``. It is - now ``False``. This matches the behavior of static type checkers as per - :pep:`586`. To support this change, the internally used type cache now + 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` - to conform with :pep:`586`. Note that declaring ``Literal`` with mutable - parameters will not throw an error:: + 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}] From 21b4de1a86651375a0cd7339147e0f26915fa8d3 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 19 Nov 2020 12:01:33 +0800 Subject: [PATCH 7/7] de-indent reST --- Doc/whatsnew/3.10.rst | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/Doc/whatsnew/3.10.rst b/Doc/whatsnew/3.10.rst index 58b2fa4f1d0fe24..ad0ec4def0b70ba 100644 --- a/Doc/whatsnew/3.10.rst +++ b/Doc/whatsnew/3.10.rst @@ -305,23 +305,24 @@ 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 respects 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 "", line 1, in - TypeError: unhashable type: 'set' + +1. ``Literal`` now de-duplicates parameters. +2. Equality comparisons between ``Literal`` objects are now order independent. +3. ``Literal`` comparisons now respects 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 "", line 1, in + TypeError: unhashable type: 'set' (Contributed by Yurii Karabas in :issue:`42345`.)