Skip to content
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

Fix ParamSpec ellipsis default for <3.10 #279

Merged
merged 6 commits into from Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@
called on a concrete subclass of a generic class. Patch by Alex Waygood
(backporting https://github.com/python/cpython/pull/107584, by James
Hilton-Balfe).
- Fix bug where `ParamSpec(default=...)` would raise a `TypeError` on Python
versions <3.11. Patch by James Hilton-Balfe

# Release 4.7.1 (July 2, 2023)

Expand Down
5 changes: 5 additions & 0 deletions doc/index.rst
Expand Up @@ -278,6 +278,11 @@ Special typing primitives

The implementation was changed for compatibility with Python 3.12.

.. versionchanged:: 4.8.0

Fixed a bug where passing an ellipsis literal (``...``) to ``default`` now
works on Python 3.10 and lower.
Gobot1234 marked this conversation as resolved.
Show resolved Hide resolved

.. class:: ParamSpecArgs

.. class:: ParamSpecKwargs
Expand Down
3 changes: 3 additions & 0 deletions src/test_typing_extensions.py
Expand Up @@ -5593,6 +5593,9 @@ def test_paramspec(self):
class A(Generic[P]): ...
Alias = typing.Callable[P, None]

P_default = ParamSpec('P_default', default=...)
self.assertIs(P_default.__default__, ...)

def test_typevartuple(self):
Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]])
Expand Down
5 changes: 4 additions & 1 deletion src/typing_extensions.py
Expand Up @@ -1277,7 +1277,10 @@ def _set_default(type_param, default):
type_param.__default__ = tuple((typing._type_check(d, "Default must be a type")
for d in default))
elif default != _marker:
type_param.__default__ = typing._type_check(default, "Default must be a type")
if isinstance(type_param, ParamSpec) and default is ...: # ... not valid <3.11
type_param.__default__ = default
else:
type_param.__default__ = typing._type_check(default, "Default must be a type")
Comment on lines +1280 to +1283
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, so the issue is that typing._type_check changed in Python 3.11 so that it would accept an ellipsis, but the function didn't allow it on previous versions of CPython

else:
type_param.__default__ = None

Expand Down