Skip to content

Commit fb37b2e

Browse files
authored
Reimplement Literal on Python <=3.10.0 (#148)
1 parent 8bff0a3 commit fb37b2e

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
objects will now raise a `TypeError` if one of the `Literal` objects being
1414
compared has a mutable parameter. (Using mutable parameters with `Literal` is
1515
not supported by PEP 586 or by any major static type checkers.)
16+
- `Literal` is now reimplemented on all Python versions <= 3.10.0. The
17+
`typing_extensions` version does not suffer from the bug that was fixed in
18+
https://github.com/python/cpython/pull/29334. (The CPython bugfix was
19+
backported to CPython 3.10.1 and 3.9.8, but no earlier.)
1620
- Backport [CPython PR 26067](https://github.com/python/cpython/pull/26067)
1721
(originally by Yurii Karabas), ensuring that `isinstance()` calls on
1822
protocols raise `TypeError` when the protocol is not decorated with

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,10 @@ Certain objects were changed after they were added to `typing`, and
143143
- `TypeVar` gains two additional parameters, `default=` and `infer_variance=`,
144144
in the draft PEPs [695](https://peps.python.org/pep-0695/) and [696](https://peps.python.org/pep-0696/), which are being considered for inclusion
145145
in Python 3.12.
146-
- `Literal` does not flatten or deduplicate parameters on Python <3.9.1. The
147-
`typing_extensions` version flattens and deduplicates parameters on all
148-
Python versions.
146+
- `Literal` does not flatten or deduplicate parameters on Python <3.9.1, and a
147+
caching bug was fixed in 3.10.1/3.9.8. The `typing_extensions` version
148+
flattens and deduplicates parameters on all Python versions, and the caching
149+
bug is also fixed on all versions.
149150

150151
There are a few types whose interface was modified between different
151152
versions of typing. For example, `typing.Sequence` was modified to

src/test_typing_extensions.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,8 @@ def test_literals_inside_other_types(self):
615615
List[Literal[("foo", "bar", "baz")]]
616616

617617
def test_repr(self):
618-
# we backport various bugfixes that were added in 3.9.1
619-
if sys.version_info >= (3, 9, 1):
618+
# we backport various bugfixes that were added in 3.10.1 and earlier
619+
if sys.version_info >= (3, 10, 1):
620620
mod_name = 'typing'
621621
else:
622622
mod_name = 'typing_extensions'
@@ -662,6 +662,8 @@ def test_equal(self):
662662
self.assertNotEqual(Literal[True], Literal[1])
663663
self.assertNotEqual(Literal[1], Literal[2])
664664
self.assertNotEqual(Literal[1, True], Literal[1])
665+
self.assertNotEqual(Literal[1, True], Literal[1, 1])
666+
self.assertNotEqual(Literal[1, 2], Literal[True, 2])
665667
self.assertEqual(Literal[1], Literal[1])
666668
self.assertEqual(Literal[1, 2], Literal[2, 1])
667669
self.assertEqual(Literal[1, 2, 3], Literal[1, 2, 3, 3])
@@ -3601,10 +3603,10 @@ def test_typing_extensions_defers_when_possible(self):
36013603
'get_type_hints',
36023604
'is_typeddict',
36033605
}
3604-
if sys.version_info < (3, 9, 1):
3605-
exclude |= {"Literal"}
36063606
if sys.version_info < (3, 10):
36073607
exclude |= {'get_args', 'get_origin'}
3608+
if sys.version_info < (3, 10, 1):
3609+
exclude |= {"Literal"}
36083610
if sys.version_info < (3, 11):
36093611
exclude |= {'final', 'NamedTuple', 'Any'}
36103612
if sys.version_info < (3, 12):

src/typing_extensions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ def IntVar(name):
261261
return typing.TypeVar(name)
262262

263263

264-
# Various Literal bugs were fixed in 3.9.1, but not backported earlier than that
265-
if sys.version_info >= (3, 9, 1):
264+
# A Literal bug was fixed in 3.11.0, 3.10.1 and 3.9.8
265+
if sys.version_info >= (3, 10, 1):
266266
Literal = typing.Literal
267267
else:
268268
def _flatten_literal_params(parameters):

0 commit comments

Comments
 (0)