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

bpo-43224: Implement PEP 646 changes to genericaliasobject.c #31019

Merged
merged 19 commits into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
72 changes: 72 additions & 0 deletions Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ class MyList(list):
self.assertEqual(repr(list[str]), 'list[str]')
self.assertEqual(repr(list[()]), 'list[()]')
self.assertEqual(repr(tuple[int, ...]), 'tuple[int, ...]')
x1 = tuple[
tuple( # Effectively the same as starring; TODO
tuple[int]
)
]
self.assertEqual(repr(x1), 'tuple[*tuple[int]]')
x2 = tuple[
tuple( # Ditto TODO
tuple[int, str]
)
]
self.assertEqual(repr(x2), 'tuple[*tuple[int, str]]')
x3 = tuple[
tuple( # Ditto TODO
tuple[int, ...]
)
]
self.assertEqual(repr(x3), 'tuple[*tuple[int, ...]]')
self.assertTrue(repr(MyList[int]).endswith('.BaseTest.test_repr.<locals>.MyList[int]'))
self.assertEqual(repr(list[str]()), '[]') # instances should keep their normal repr

Expand All @@ -175,6 +193,7 @@ def test_exposed_type(self):

def test_parameters(self):
from typing import List, Dict, Callable

D0 = dict[str, int]
self.assertEqual(D0.__args__, (str, int))
self.assertEqual(D0.__parameters__, ())
Expand All @@ -190,6 +209,7 @@ def test_parameters(self):
D2b = dict[T, T]
self.assertEqual(D2b.__args__, (T, T))
self.assertEqual(D2b.__parameters__, (T,))

L0 = list[str]
self.assertEqual(L0.__args__, (str,))
self.assertEqual(L0.__parameters__, ())
Expand All @@ -212,6 +232,45 @@ def test_parameters(self):
self.assertEqual(L5.__args__, (Callable[[K, V], K],))
self.assertEqual(L5.__parameters__, (K, V))

T1 = tuple[
tuple( # Ditto TODO
tuple[int]
)
]
self.assertEqual(
T1.__args__,
tuple( # Ditto TODO
tuple[int]
)
)
self.assertEqual(T1.__parameters__, ())

T2 = tuple[
tuple( # Ditto TODO
tuple[T]
)
]
self.assertEqual(
T2.__args__,
tuple( # Ditto TODO
tuple[T]
)
)
self.assertEqual(T2.__parameters__, (T,))

T4 = tuple[
tuple( # Ditto TODO
tuple[int, str]
)
]
self.assertEqual(
T4.__args__,
tuple( # Ditto TODO
tuple[int, str]
)
)
self.assertEqual(T4.__parameters__, ())

def test_parameter_chaining(self):
from typing import List, Dict, Union, Callable
self.assertEqual(list[T][int], list[int])
Expand Down Expand Up @@ -242,6 +301,19 @@ def test_parameter_chaining(self):
def test_equality(self):
self.assertEqual(list[int], list[int])
self.assertEqual(dict[str, int], dict[str, int])
self.assertEqual((*tuple[int],)[0], (*tuple[int],)[0])
self.assertEqual(
tuple[
tuple( # Effectively the same as starring; TODO
tuple[int]
)
],
tuple[
tuple( # Ditto TODO
tuple[int]
)
]
)
self.assertNotEqual(dict[str, int], dict[str, str])
self.assertNotEqual(list, list[int])
self.assertNotEqual(list[int], list)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow unpacking types.GenericAlias objects, e.g. ``*tuple[int, str]``.
56 changes: 56 additions & 0 deletions Objects/genericaliasobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
#include "pycore_unionobject.h" // _Py_union_type_or, _PyGenericAlias_Check
#include "structmember.h" // PyMemberDef

#include <stdbool.h>

typedef struct {
PyObject_HEAD
PyObject *origin;
PyObject *args;
PyObject *parameters;
PyObject* weakreflist;
// Whether we're a starred type, e.g. *tuple[int].
// Only supported for `tuple`.
bool starred;
} gaobject;

typedef struct {
PyObject_HEAD
PyObject *obj; /* Set to NULL when iterator is exhausted */
} gaiterobject;

static void
ga_dealloc(PyObject *self)
{
Expand Down Expand Up @@ -124,6 +134,11 @@ ga_repr(PyObject *self)
_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer);

if (alias->starred) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, "*", 1) < 0) {
goto error;
}
}
if (ga_repr_item(&writer, alias->origin) < 0) {
goto error;
}
Expand Down Expand Up @@ -626,6 +641,46 @@ static PyNumberMethods ga_as_number = {
.nb_or = _Py_union_type_or, // Add __or__ function
};

static PyObject *
ga_iternext(gaiterobject *gi) {
if (gi->obj == NULL) {
mrahtz marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
}
gaobject *alias = (gaobject *)gi->obj;
PyObject *starred_tuple = Py_GenericAlias(alias->origin, alias->args);
mrahtz marked this conversation as resolved.
Show resolved Hide resolved
if (starred_tuple == NULL) {
return NULL;
}
((gaobject *)starred_tuple)->starred = true;
Py_SETREF(gi->obj, NULL);
mrahtz marked this conversation as resolved.
Show resolved Hide resolved
return starred_tuple;
}

static void
ga_iter_dealloc(gaiterobject *gi) {
PyObject_GC_UnTrack(gi);
PyObject_GC_Del(gi);
mrahtz marked this conversation as resolved.
Show resolved Hide resolved
}

static PyTypeObject Py_GenericAliasIterType = {
mrahtz marked this conversation as resolved.
Show resolved Hide resolved
PyVarObject_HEAD_INIT(&PyType_Type, 0)
.tp_name = "generic_alias_iter",
.tp_basicsize = sizeof(gaiterobject),
.tp_iternext = (iternextfunc)ga_iternext,
.tp_dealloc = (destructor)ga_iter_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
};

static PyObject *
ga_iter(PyObject *self) {
gaiterobject *gi = PyObject_GC_New(gaiterobject, &Py_GenericAliasIterType);
if (gi == NULL)
mrahtz marked this conversation as resolved.
Show resolved Hide resolved
return NULL;
gi->obj = Py_NewRef(self);
PyObject_GC_Track(gi);
return (PyObject *)gi;
}

// TODO:
// - argument clinic?
// - __doc__?
Expand Down Expand Up @@ -654,6 +709,7 @@ PyTypeObject Py_GenericAliasType = {
.tp_new = ga_new,
.tp_free = PyObject_GC_Del,
.tp_getset = ga_properties,
.tp_iter = (getiterfunc)ga_iter,
};

PyObject *
Expand Down