Skip to content

Reimplement Literal on Python <=3.10.0 #148

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
Apr 20, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
objects will now raise a `TypeError` if one of the `Literal` objects being
compared has a mutable parameter. (Using mutable parameters with `Literal` is
not supported by PEP 586 or by any major static type checkers.)
- `Literal` is now reimplemented on all Python versions <= 3.10.0. The
`typing_extensions` version does not suffer from the bug that was fixed in
https://github.com/python/cpython/pull/29334. (The CPython bugfix was
backported to CPython 3.10.1 and 3.9.8, but no earlier.)
- Backport [CPython PR 26067](https://github.com/python/cpython/pull/26067)
(originally by Yurii Karabas), ensuring that `isinstance()` calls on
protocols raise `TypeError` when the protocol is not decorated with
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ Certain objects were changed after they were added to `typing`, and
- `TypeVar` gains two additional parameters, `default=` and `infer_variance=`,
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
in Python 3.12.
- `Literal` does not flatten or deduplicate parameters on Python <3.9.1. The
`typing_extensions` version flattens and deduplicates parameters on all
Python versions.
- `Literal` does not flatten or deduplicate parameters on Python <3.9.1, and a
caching bug was fixed in 3.10.1/3.9.8. The `typing_extensions` version
flattens and deduplicates parameters on all Python versions, and the caching
bug is also fixed on all versions.

There are a few types whose interface was modified between different
versions of typing. For example, `typing.Sequence` was modified to
Expand Down
10 changes: 6 additions & 4 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ def test_literals_inside_other_types(self):
List[Literal[("foo", "bar", "baz")]]

def test_repr(self):
# we backport various bugfixes that were added in 3.9.1
if sys.version_info >= (3, 9, 1):
# we backport various bugfixes that were added in 3.10.1 and earlier
if sys.version_info >= (3, 10, 1):
mod_name = 'typing'
else:
mod_name = 'typing_extensions'
Expand Down Expand Up @@ -662,6 +662,8 @@ def test_equal(self):
self.assertNotEqual(Literal[True], Literal[1])
self.assertNotEqual(Literal[1], Literal[2])
self.assertNotEqual(Literal[1, True], Literal[1])
self.assertNotEqual(Literal[1, True], Literal[1, 1])
self.assertNotEqual(Literal[1, 2], Literal[True, 2])
self.assertEqual(Literal[1], Literal[1])
self.assertEqual(Literal[1, 2], Literal[2, 1])
self.assertEqual(Literal[1, 2, 3], Literal[1, 2, 3, 3])
Expand Down Expand Up @@ -3601,10 +3603,10 @@ def test_typing_extensions_defers_when_possible(self):
'get_type_hints',
'is_typeddict',
}
if sys.version_info < (3, 9, 1):
exclude |= {"Literal"}
if sys.version_info < (3, 10):
exclude |= {'get_args', 'get_origin'}
if sys.version_info < (3, 10, 1):
exclude |= {"Literal"}
if sys.version_info < (3, 11):
exclude |= {'final', 'NamedTuple', 'Any'}
if sys.version_info < (3, 12):
Expand Down
4 changes: 2 additions & 2 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ def IntVar(name):
return typing.TypeVar(name)


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