From e2e6bdb8832b5f8d946493faa5b2b6911468b38a Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sun, 15 Dec 2024 16:53:08 +0100 Subject: [PATCH 01/11] Add tp_iter for TypeAliasType --- Lib/test/test_type_aliases.py | 13 ++++++++++++- Objects/typevarobject.c | 13 +++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_type_aliases.py b/Lib/test/test_type_aliases.py index 230bbe646baf28..ee1791bc1d0b9d 100644 --- a/Lib/test/test_type_aliases.py +++ b/Lib/test/test_type_aliases.py @@ -5,7 +5,7 @@ from test.typinganndata import mod_generics_cache from typing import ( - Callable, TypeAliasType, TypeVar, TypeVarTuple, ParamSpec, get_args, + Callable, TypeAliasType, TypeVar, TypeVarTuple, ParamSpec, Unpack, get_args, ) @@ -317,6 +317,17 @@ def test_module(self): self.assertEqual(mod_generics_cache.OldStyle.__module__, mod_generics_cache.__name__) + def test_unpack(self): + type Alias = tuple[int, int] + unpacked = (*Alias,)[0] + self.assertEqual(unpacked, Unpack[Alias]) + + class Foo[*Ts]: + pass + + x = Foo[str, *Alias] + self.assertEqual(x.__args__, (str, Unpack[Alias])) + # All these type aliases are used for pickling tests: T = TypeVar('T') diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 4ed40aa71a595e..50f0836d2676a9 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -385,7 +385,7 @@ caller(void) } static PyObject * -typevartuple_unpack(PyObject *tvt) +unpack(PyObject *self) { PyObject *typing = PyImport_ImportModule("typing"); if (typing == NULL) { @@ -396,7 +396,7 @@ typevartuple_unpack(PyObject *tvt) Py_DECREF(typing); return NULL; } - PyObject *unpacked = PyObject_GetItem(unpack, tvt); + PyObject *unpacked = PyObject_GetItem(unpack, self); Py_DECREF(typing); Py_DECREF(unpack); return unpacked; @@ -431,7 +431,7 @@ unpack_typevartuples(PyObject *params) for (Py_ssize_t i = 0; i < n; i++) { PyObject *param = PyTuple_GET_ITEM(params, i); if (Py_IS_TYPE(param, tp)) { - PyObject *unpacked = typevartuple_unpack(param); + PyObject *unpacked = unpack(param); if (unpacked == NULL) { Py_DECREF(new_params); return NULL; @@ -1505,9 +1505,9 @@ typevartuple_dealloc(PyObject *self) } static PyObject * -typevartuple_iter(PyObject *self) +unpack_iter(PyObject *self) { - PyObject *unpacked = typevartuple_unpack(self); + PyObject *unpacked = unpack(self); if (unpacked == NULL) { return NULL; } @@ -1760,7 +1760,7 @@ PyType_Slot typevartuple_slots[] = { {Py_tp_methods, typevartuple_methods}, {Py_tp_getset, typevartuple_getset}, {Py_tp_new, typevartuple}, - {Py_tp_iter, typevartuple_iter}, + {Py_tp_iter, unpack_iter}, {Py_tp_repr, typevartuple_repr}, {Py_tp_dealloc, typevartuple_dealloc}, {Py_tp_alloc, PyType_GenericAlloc}, @@ -2134,6 +2134,7 @@ PyTypeObject _PyTypeAlias_Type = { .tp_new = typealias_new, .tp_free = PyObject_GC_Del, .tp_traverse = (traverseproc)typealias_traverse, + .tp_iter = unpack_iter, .tp_clear = (inquiry)typealias_clear, .tp_repr = typealias_repr, .tp_as_number = &typealias_as_number, From 982737cfa4582056c63faf8d1362436765efec1a Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sun, 15 Dec 2024 16:57:15 +0100 Subject: [PATCH 02/11] Add news entry --- .../2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst new file mode 100644 index 00000000000000..9c4cd89bd05e9c --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst @@ -0,0 +1,2 @@ +Add the ``tp_iter`` slot to :class:`typing.TypeAliasType` to allow star +unpacking. From e8674d75eb2d0d824e35d68bd676bff24b9dd246 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Mon, 16 Dec 2024 21:01:57 +0100 Subject: [PATCH 03/11] Keep function names consistent --- Objects/typevarobject.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index 50f0836d2676a9..ebe08b93e51076 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -1,4 +1,4 @@ -// TypeVar, TypeVarTuple, and ParamSpec +// TypeVar, TypeVarTuple, TypeAlias and ParamSpec #include "Python.h" #include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK, PyAnnotateFormat #include "pycore_typevarobject.h" @@ -59,6 +59,9 @@ typedef struct { #include "clinic/typevarobject.c.h" +#define typevartuple_iter unpack_iter +#define typealias_iter unpack_iter + /* NoDefault is a marker object to indicate that a parameter has no default. */ static PyObject * @@ -1760,7 +1763,7 @@ PyType_Slot typevartuple_slots[] = { {Py_tp_methods, typevartuple_methods}, {Py_tp_getset, typevartuple_getset}, {Py_tp_new, typevartuple}, - {Py_tp_iter, unpack_iter}, + {Py_tp_iter, typevartuple_iter}, {Py_tp_repr, typevartuple_repr}, {Py_tp_dealloc, typevartuple_dealloc}, {Py_tp_alloc, PyType_GenericAlloc}, @@ -2134,7 +2137,7 @@ PyTypeObject _PyTypeAlias_Type = { .tp_new = typealias_new, .tp_free = PyObject_GC_Del, .tp_traverse = (traverseproc)typealias_traverse, - .tp_iter = unpack_iter, + .tp_iter = typealias_iter, .tp_clear = (inquiry)typealias_clear, .tp_repr = typealias_repr, .tp_as_number = &typealias_as_number, From 4ffeefcf829ad4bbe3e928e4002223cf29fca649 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Mon, 16 Dec 2024 21:04:17 +0100 Subject: [PATCH 04/11] Add whatsnew entry --- Doc/whatsnew/3.14.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 095949242c09d9..94cfa7557d9970 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -857,6 +857,7 @@ typing * Remove :class:`!typing.ByteString`. It had previously raised a :exc:`DeprecationWarning` since Python 3.12. +* :class:`typing.TypeAliasType` now supports star unpacking. urllib ------ From 6674aa120075d246e6c79d537081cbe0aa71bdf8 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Mon, 16 Dec 2024 21:19:09 +0100 Subject: [PATCH 05/11] Update docs --- Doc/library/typing.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 0fee782121b0af..58d4c7c5d149b9 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2292,6 +2292,20 @@ without the dedicated syntax, as documented below. .. versionadded:: 3.14 + .. rubric:: Unpacking + + Type aliases support star unpacking using the ``*Alias`` syntax. + This is equivalent to using the ``Unpack[Alias]`` directly: + + .. doctest:: + + >>> type Alias = tuple[int, str] + >>> type Unpacked = tuple[bool, *Alias] + >>> Unpacked.__value__ + tuple[bool, typing.Unpack[Alias]] + + .. versionadded:: next + Other special directives """""""""""""""""""""""" From d42645f955bdaa34a679fe08dc68cbbc4db1d772 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Mon, 16 Dec 2024 21:23:52 +0100 Subject: [PATCH 06/11] Fix typo --- Doc/library/typing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 58d4c7c5d149b9..3a7d0391c8c0d9 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2295,7 +2295,7 @@ without the dedicated syntax, as documented below. .. rubric:: Unpacking Type aliases support star unpacking using the ``*Alias`` syntax. - This is equivalent to using the ``Unpack[Alias]`` directly: + This is equivalent to using ``Unpack[Alias]`` directly: .. doctest:: From 03744a6205a4bac533726f7161cad16746474103 Mon Sep 17 00:00:00 2001 From: "Tomas R." Date: Mon, 16 Dec 2024 21:29:57 +0100 Subject: [PATCH 07/11] Update Doc/whatsnew/3.14.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/whatsnew/3.14.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 94cfa7557d9970..4f1a5d385afe97 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -857,6 +857,7 @@ typing * Remove :class:`!typing.ByteString`. It had previously raised a :exc:`DeprecationWarning` since Python 3.12. + * :class:`typing.TypeAliasType` now supports star unpacking. urllib From f605921ea04ab846f5b1cf6659942ab7483080be Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Mon, 16 Dec 2024 21:54:41 +0100 Subject: [PATCH 08/11] Fix sphinx errors --- Doc/library/typing.rst | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 3a7d0391c8c0d9..41a6565054aa3c 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2294,17 +2294,17 @@ without the dedicated syntax, as documented below. .. rubric:: Unpacking - Type aliases support star unpacking using the ``*Alias`` syntax. - This is equivalent to using ``Unpack[Alias]`` directly: + Type aliases support star unpacking using the ``*Alias`` syntax. + This is equivalent to using ``Unpack[Alias]`` directly: - .. doctest:: + .. doctest:: - >>> type Alias = tuple[int, str] - >>> type Unpacked = tuple[bool, *Alias] - >>> Unpacked.__value__ - tuple[bool, typing.Unpack[Alias]] + >>> type Alias = tuple[int, str] + >>> type Unpacked = tuple[bool, *Alias] + >>> Unpacked.__value__ + tuple[bool, typing.Unpack[Alias]] - .. versionadded:: next + .. versionadded:: next Other special directives From 186dc48a5f307074658a33c6595d7239652fd690 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Mon, 16 Dec 2024 23:04:29 +0100 Subject: [PATCH 09/11] Update news entry --- .../2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst index 9c4cd89bd05e9c..25b23fd27c0b92 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-15-16-56-26.gh-issue-126085.BvyEXk.rst @@ -1,2 +1 @@ -Add the ``tp_iter`` slot to :class:`typing.TypeAliasType` to allow star -unpacking. +:class:`typing.TypeAliasType` now supports star unpacking. From dcb1b7b26030f6b3935d1a71a1800bad39ef22db Mon Sep 17 00:00:00 2001 From: "Tomas R." Date: Wed, 18 Dec 2024 16:10:59 +0100 Subject: [PATCH 10/11] Update comment in Objects/typevarobject.c Co-authored-by: Jelle Zijlstra --- Objects/typevarobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index ebe08b93e51076..ae07d6a1d8015f 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -1,4 +1,4 @@ -// TypeVar, TypeVarTuple, TypeAlias and ParamSpec +// TypeVar, TypeVarTuple, ParamSpec, and TypeAlias #include "Python.h" #include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK, PyAnnotateFormat #include "pycore_typevarobject.h" From 3597b6641dc5bf72ce6d3eb2d1a82c7d6fbca28c Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Tue, 4 Mar 2025 19:36:57 +0100 Subject: [PATCH 11/11] Remove extra defines --- Objects/typevarobject.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Objects/typevarobject.c b/Objects/typevarobject.c index ae07d6a1d8015f..d8049479775d73 100644 --- a/Objects/typevarobject.c +++ b/Objects/typevarobject.c @@ -59,9 +59,6 @@ typedef struct { #include "clinic/typevarobject.c.h" -#define typevartuple_iter unpack_iter -#define typealias_iter unpack_iter - /* NoDefault is a marker object to indicate that a parameter has no default. */ static PyObject * @@ -1763,7 +1760,7 @@ PyType_Slot typevartuple_slots[] = { {Py_tp_methods, typevartuple_methods}, {Py_tp_getset, typevartuple_getset}, {Py_tp_new, typevartuple}, - {Py_tp_iter, typevartuple_iter}, + {Py_tp_iter, unpack_iter}, {Py_tp_repr, typevartuple_repr}, {Py_tp_dealloc, typevartuple_dealloc}, {Py_tp_alloc, PyType_GenericAlloc}, @@ -2137,7 +2134,7 @@ PyTypeObject _PyTypeAlias_Type = { .tp_new = typealias_new, .tp_free = PyObject_GC_Del, .tp_traverse = (traverseproc)typealias_traverse, - .tp_iter = typealias_iter, + .tp_iter = unpack_iter, .tp_clear = (inquiry)typealias_clear, .tp_repr = typealias_repr, .tp_as_number = &typealias_as_number,