From 2be5552881b9055915e40ffb6419880c90e5e98a Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 6 Sep 2023 15:13:46 +0100 Subject: [PATCH 1/6] Fix ParamSpec ellipsis default for <3.10 --- src/typing_extensions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/typing_extensions.py b/src/typing_extensions.py index b0b1bceb..129b1db0 100644 --- a/src/typing_extensions.py +++ b/src/typing_extensions.py @@ -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 ...: # ellipsis wasn't accepted until 3.11 as a valid call + type_param.__default__ = default + else: + type_param.__default__ = typing._type_check(default, "Default must be a type") else: type_param.__default__ = None From 021512d38c12154f27172d3d0d6114d9a18e7f95 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 6 Sep 2023 15:18:18 +0100 Subject: [PATCH 2/6] Update test_typing_extensions.py --- src/test_typing_extensions.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index e741d699..fcebf131 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -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]]) From d8b8929068d01676409bcbf710f526229207dd31 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 6 Sep 2023 15:20:28 +0100 Subject: [PATCH 3/6] Shorten comment length --- src/typing_extensions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/typing_extensions.py b/src/typing_extensions.py index 129b1db0..97a0d1cd 100644 --- a/src/typing_extensions.py +++ b/src/typing_extensions.py @@ -1277,7 +1277,7 @@ 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: - if isinstance(type_param, ParamSpec) and default is ...: # ellipsis wasn't accepted until 3.11 as a valid call + 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") From 3853b29baae5fa73a853c5048f4bfe2704adcc4e Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 6 Sep 2023 16:18:43 +0000 Subject: [PATCH 4/6] Add docs --- CHANGELOG.md | 2 ++ doc/index.rst | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cd98764..19fb1003 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/doc/index.rst b/doc/index.rst index ea7ff549..c3b55056 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -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. + .. class:: ParamSpecArgs .. class:: ParamSpecKwargs From 7e106869bde18e293704d188c34b8b7cd24f051c Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 6 Sep 2023 19:25:58 +0100 Subject: [PATCH 5/6] Update index.rst --- doc/index.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index c3b55056..71ae6101 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -280,8 +280,8 @@ Special typing primitives .. 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. + Fixed a bug where passing an ellipsis literal (``...``) to ``default`` now + works on Python 3.10 and lower. .. class:: ParamSpecArgs From f93a5e570bf256c0f6ece1fb54b680f5bd037980 Mon Sep 17 00:00:00 2001 From: James Hilton-Balfe Date: Wed, 6 Sep 2023 19:29:05 +0100 Subject: [PATCH 6/6] Update doc/index.rst Co-authored-by: Alex Waygood --- doc/index.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/index.rst b/doc/index.rst index 71ae6101..33492e52 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -280,8 +280,8 @@ Special typing primitives .. versionchanged:: 4.8.0 - Fixed a bug where passing an ellipsis literal (``...``) to ``default`` now - works on Python 3.10 and lower. + Passing an ellipsis literal (``...``) to *default* now works on Python + 3.10 and lower. .. class:: ParamSpecArgs