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 4 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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`` would raise a
:py:exc:`TypeError` on Python versions <3.11.

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

@AlexWaygood AlexWaygood Sep 6, 2023

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
Loading