blob: 54cdedae4d92c988bfe16fba46a1720e3fbe137a [file] [log] [blame]
Maggie Moss1b4552c2020-09-09 13:23:24 -07001// types.Union -- used to represent e.g. Union[int, str], int | str
2#include "Python.h"
Miss Islington (bot)08561342021-07-03 06:33:16 -07003#include "pycore_object.h" // _PyObject_GC_TRACK/UNTRACK
Maggie Moss1b4552c2020-09-09 13:23:24 -07004#include "pycore_unionobject.h"
5#include "structmember.h"
6
7
Miss Islington (bot)03aad302021-07-17 14:10:21 -07008static PyObject *make_union(PyObject *);
9
10
Maggie Moss1b4552c2020-09-09 13:23:24 -070011typedef struct {
12 PyObject_HEAD
13 PyObject *args;
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +030014 PyObject *parameters;
Maggie Moss1b4552c2020-09-09 13:23:24 -070015} unionobject;
16
17static void
18unionobject_dealloc(PyObject *self)
19{
20 unionobject *alias = (unionobject *)self;
21
Miss Islington (bot)08561342021-07-03 06:33:16 -070022 _PyObject_GC_UNTRACK(self);
23
Maggie Moss1b4552c2020-09-09 13:23:24 -070024 Py_XDECREF(alias->args);
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +030025 Py_XDECREF(alias->parameters);
Neil Schemenauer0564aaf2020-10-27 11:55:52 -070026 Py_TYPE(self)->tp_free(self);
Maggie Moss1b4552c2020-09-09 13:23:24 -070027}
28
Miss Islington (bot)08561342021-07-03 06:33:16 -070029static int
30union_traverse(PyObject *self, visitproc visit, void *arg)
31{
32 unionobject *alias = (unionobject *)self;
33 Py_VISIT(alias->args);
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +030034 Py_VISIT(alias->parameters);
Miss Islington (bot)08561342021-07-03 06:33:16 -070035 return 0;
36}
37
Maggie Moss1b4552c2020-09-09 13:23:24 -070038static Py_hash_t
39union_hash(PyObject *self)
40{
41 unionobject *alias = (unionobject *)self;
Miss Islington (bot)70598802021-07-16 02:02:59 -070042 PyObject *args = PyFrozenSet_New(alias->args);
43 if (args == NULL) {
44 return (Py_hash_t)-1;
Maggie Moss1b4552c2020-09-09 13:23:24 -070045 }
Miss Islington (bot)70598802021-07-16 02:02:59 -070046 Py_hash_t hash = PyObject_Hash(args);
47 Py_DECREF(args);
48 return hash;
Maggie Moss1b4552c2020-09-09 13:23:24 -070049}
50
51static int
Miss Islington (bot)03aad302021-07-17 14:10:21 -070052is_generic_alias_in_args(PyObject *args)
53{
Maggie Moss1b4552c2020-09-09 13:23:24 -070054 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
55 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
56 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
Miss Islington (bot)03aad302021-07-17 14:10:21 -070057 if (_PyGenericAlias_Check(arg)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -070058 return 0;
59 }
60 }
61 return 1;
62}
63
64static PyObject *
65union_instancecheck(PyObject *self, PyObject *instance)
66{
67 unionobject *alias = (unionobject *) self;
68 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
69 if (!is_generic_alias_in_args(alias->args)) {
70 PyErr_SetString(PyExc_TypeError,
71 "isinstance() argument 2 cannot contain a parameterized generic");
72 return NULL;
73 }
74 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
75 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
Miss Islington (bot)b42eee72021-07-13 21:55:45 -070076 if (PyType_Check(arg)) {
77 int res = PyObject_IsInstance(instance, arg);
78 if (res < 0) {
79 return NULL;
80 }
81 if (res) {
82 Py_RETURN_TRUE;
83 }
Maggie Moss1b4552c2020-09-09 13:23:24 -070084 }
85 }
86 Py_RETURN_FALSE;
87}
88
89static PyObject *
90union_subclasscheck(PyObject *self, PyObject *instance)
91{
92 if (!PyType_Check(instance)) {
93 PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
94 return NULL;
95 }
96 unionobject *alias = (unionobject *)self;
97 if (!is_generic_alias_in_args(alias->args)) {
98 PyErr_SetString(PyExc_TypeError,
99 "issubclass() argument 2 cannot contain a parameterized generic");
100 return NULL;
101 }
102 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
103 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
104 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
Miss Islington (bot)b42eee72021-07-13 21:55:45 -0700105 if (PyType_Check(arg)) {
106 int res = PyObject_IsSubclass(instance, arg);
107 if (res < 0) {
108 return NULL;
109 }
110 if (res) {
111 Py_RETURN_TRUE;
112 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700113 }
114 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700115 Py_RETURN_FALSE;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700116}
117
118static int
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700119is_typing_module(PyObject *obj)
120{
121 _Py_IDENTIFIER(__module__);
122 PyObject *module;
123 if (_PyObject_LookupAttrId(obj, &PyId___module__, &module) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700124 return -1;
125 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700126 int is_typing = (module != NULL &&
127 PyUnicode_Check(module) &&
128 _PyUnicode_EqualToASCIIString(module, "typing"));
129 Py_XDECREF(module);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700130 return is_typing;
131}
132
133static int
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700134is_typing_name(PyObject *obj, const char *name)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700135{
136 PyTypeObject *type = Py_TYPE(obj);
137 if (strcmp(type->tp_name, name) != 0) {
138 return 0;
139 }
Miss Islington (bot)cc1a47c2021-07-15 00:25:22 -0700140 return is_typing_module((PyObject *)type);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700141}
142
143static PyObject *
144union_richcompare(PyObject *a, PyObject *b, int op)
145{
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700146 if (!_PyUnion_Check(b) || (op != Py_EQ && op != Py_NE)) {
147 Py_RETURN_NOTIMPLEMENTED;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700148 }
149
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700150 PyObject *a_set = PySet_New(((unionobject*)a)->args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700151 if (a_set == NULL) {
152 return NULL;
153 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700154 PyObject *b_set = PySet_New(((unionobject*)b)->args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700155 if (b_set == NULL) {
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300156 Py_DECREF(a_set);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700157 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700158 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700159 PyObject *result = PyObject_RichCompare(a_set, b_set, op);
160 Py_DECREF(b_set);
161 Py_DECREF(a_set);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700162 return result;
163}
164
165static PyObject*
166flatten_args(PyObject* args)
167{
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200168 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
169 Py_ssize_t total_args = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700170 // Get number of total args once it's flattened.
171 for (Py_ssize_t i = 0; i < arg_length; i++) {
172 PyObject *arg = PyTuple_GET_ITEM(args, i);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700173 if (_PyUnion_Check(arg)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700174 total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args);
175 } else {
176 total_args++;
177 }
178 }
179 // Create new tuple of flattened args.
180 PyObject *flattened_args = PyTuple_New(total_args);
181 if (flattened_args == NULL) {
182 return NULL;
183 }
184 Py_ssize_t pos = 0;
185 for (Py_ssize_t i = 0; i < arg_length; i++) {
186 PyObject *arg = PyTuple_GET_ITEM(args, i);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700187 if (_PyUnion_Check(arg)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700188 PyObject* nested_args = ((unionobject*)arg)->args;
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200189 Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args);
190 for (Py_ssize_t j = 0; j < nested_arg_length; j++) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700191 PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j);
192 Py_INCREF(nested_arg);
193 PyTuple_SET_ITEM(flattened_args, pos, nested_arg);
194 pos++;
195 }
196 } else {
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300197 if (arg == Py_None) {
198 arg = (PyObject *)&_PyNone_Type;
199 }
Maggie Moss1b4552c2020-09-09 13:23:24 -0700200 Py_INCREF(arg);
201 PyTuple_SET_ITEM(flattened_args, pos, arg);
202 pos++;
203 }
204 }
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700205 assert(pos == total_args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700206 return flattened_args;
207}
208
209static PyObject*
210dedup_and_flatten_args(PyObject* args)
211{
212 args = flatten_args(args);
213 if (args == NULL) {
214 return NULL;
215 }
216 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
217 PyObject *new_args = PyTuple_New(arg_length);
218 if (new_args == NULL) {
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700219 Py_DECREF(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700220 return NULL;
221 }
222 // Add unique elements to an array.
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200223 Py_ssize_t added_items = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700224 for (Py_ssize_t i = 0; i < arg_length; i++) {
225 int is_duplicate = 0;
226 PyObject* i_element = PyTuple_GET_ITEM(args, i);
Serhiy Storchaka80844d12021-07-16 16:42:04 +0300227 for (Py_ssize_t j = 0; j < added_items; j++) {
228 PyObject* j_element = PyTuple_GET_ITEM(new_args, j);
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700229 int is_ga = _PyGenericAlias_Check(i_element) &&
230 _PyGenericAlias_Check(j_element);
kj4eb41d02020-11-09 12:00:13 +0800231 // RichCompare to also deduplicate GenericAlias types (slower)
232 is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ)
233 : i_element == j_element;
234 // Should only happen if RichCompare fails
235 if (is_duplicate < 0) {
236 Py_DECREF(args);
237 Py_DECREF(new_args);
238 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700239 }
kj4eb41d02020-11-09 12:00:13 +0800240 if (is_duplicate)
241 break;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700242 }
243 if (!is_duplicate) {
244 Py_INCREF(i_element);
245 PyTuple_SET_ITEM(new_args, added_items, i_element);
246 added_items++;
247 }
248 }
249 Py_DECREF(args);
250 _PyTuple_Resize(&new_args, added_items);
251 return new_args;
252}
253
254static int
255is_typevar(PyObject *obj)
256{
257 return is_typing_name(obj, "TypeVar");
258}
259
260static int
261is_special_form(PyObject *obj)
262{
263 return is_typing_name(obj, "_SpecialForm");
264}
265
266static int
267is_new_type(PyObject *obj)
268{
269 PyTypeObject *type = Py_TYPE(obj);
270 if (type != &PyFunction_Type) {
271 return 0;
272 }
273 return is_typing_module(obj);
274}
275
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700276// Emulates short-circuiting behavior of the ``||`` operator
277// while also checking negative values.
278#define CHECK_RES(res) { \
279 int result = res; \
280 if (result) { \
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700281 return result; \
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700282 } \
283}
284
285// Returns 1 on true, 0 on false, and -1 on error.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700286static int
287is_unionable(PyObject *obj)
288{
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700289 if (obj == Py_None ||
290 PyType_Check(obj) ||
291 _PyGenericAlias_Check(obj) ||
292 _PyUnion_Check(obj))
293 {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700294 return 1;
295 }
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700296 CHECK_RES(is_typevar(obj));
297 CHECK_RES(is_new_type(obj));
298 CHECK_RES(is_special_form(obj));
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700299 return 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700300}
301
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100302static int
303is_args_unionable(PyObject *args)
304{
305 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
306 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
307 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
308 int is_arg_unionable = is_unionable(arg);
309 if (is_arg_unionable <= 0) {
310 if (is_arg_unionable == 0) {
311 PyErr_Format(PyExc_TypeError,
312 "Each union argument must be a type, got %.100R", arg);
313 }
314 return 0;
315 }
316 }
317 return 1;
318}
319
kj4eb41d02020-11-09 12:00:13 +0800320PyObject *
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700321_Py_union_type_or(PyObject* self, PyObject* other)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700322{
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700323 int r = is_unionable(self);
324 if (r > 0) {
325 r = is_unionable(other);
326 }
327 if (r < 0) {
328 return NULL;
329 }
330 if (!r) {
331 Py_RETURN_NOTIMPLEMENTED;
332 }
333
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700334 PyObject *tuple = PyTuple_Pack(2, self, other);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700335 if (tuple == NULL) {
336 return NULL;
337 }
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700338
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700339 PyObject *new_union = make_union(tuple);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700340 Py_DECREF(tuple);
341 return new_union;
342}
343
344static int
345union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
346{
347 _Py_IDENTIFIER(__module__);
348 _Py_IDENTIFIER(__qualname__);
349 _Py_IDENTIFIER(__origin__);
350 _Py_IDENTIFIER(__args__);
351 PyObject *qualname = NULL;
352 PyObject *module = NULL;
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300353 PyObject *tmp;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700354 PyObject *r = NULL;
355 int err;
356
Serhiy Storchaka6dec5252021-07-15 10:15:14 +0300357 if (p == (PyObject *)&_PyNone_Type) {
358 return _PyUnicodeWriter_WriteASCIIString(writer, "None", 4);
359 }
360
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300361 if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700362 goto exit;
363 }
364
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300365 if (tmp) {
366 Py_DECREF(tmp);
367 if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700368 goto exit;
369 }
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300370 if (tmp) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700371 // It looks like a GenericAlias
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300372 Py_DECREF(tmp);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700373 goto use_repr;
374 }
375 }
376
377 if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
378 goto exit;
379 }
380 if (qualname == NULL) {
381 goto use_repr;
382 }
383 if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
384 goto exit;
385 }
386 if (module == NULL || module == Py_None) {
387 goto use_repr;
388 }
389
390 // Looks like a class
391 if (PyUnicode_Check(module) &&
392 _PyUnicode_EqualToASCIIString(module, "builtins"))
393 {
394 // builtins don't need a module name
395 r = PyObject_Str(qualname);
396 goto exit;
397 }
398 else {
399 r = PyUnicode_FromFormat("%S.%S", module, qualname);
400 goto exit;
401 }
402
403use_repr:
404 r = PyObject_Repr(p);
405exit:
406 Py_XDECREF(qualname);
407 Py_XDECREF(module);
408 if (r == NULL) {
409 return -1;
410 }
411 err = _PyUnicodeWriter_WriteStr(writer, r);
412 Py_DECREF(r);
413 return err;
414}
415
416static PyObject *
417union_repr(PyObject *self)
418{
419 unionobject *alias = (unionobject *)self;
420 Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
421
422 _PyUnicodeWriter writer;
423 _PyUnicodeWriter_Init(&writer);
424 for (Py_ssize_t i = 0; i < len; i++) {
425 if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
426 goto error;
427 }
428 PyObject *p = PyTuple_GET_ITEM(alias->args, i);
429 if (union_repr_item(&writer, p) < 0) {
430 goto error;
431 }
432 }
433 return _PyUnicodeWriter_Finish(&writer);
434error:
435 _PyUnicodeWriter_Dealloc(&writer);
436 return NULL;
437}
438
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100439static PyObject *
440union_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
441{
442 unionobject *alias = (unionobject *)self;
443 PyObject* from_args = PyObject_GetAttrString(self, "_from_args");
444 if (from_args == NULL) {
445 return NULL;
446 }
447
448 return Py_BuildValue("N(O)", from_args, alias->args);
449}
450
Maggie Moss1b4552c2020-09-09 13:23:24 -0700451static PyMemberDef union_members[] = {
452 {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
453 {0}
454};
455
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100456static PyObject *
457union_from_args(PyObject *cls, PyObject *args)
458{
459 if (!PyTuple_CheckExact(args)) {
460 _PyArg_BadArgument("Union._from_args", "argument 'args'", "tuple", args);
461 return NULL;
462 }
463 if (!PyTuple_GET_SIZE(args)) {
464 PyErr_SetString(PyExc_ValueError, "args must be not empty");
465 return NULL;
466 }
467
468 if (is_args_unionable(args) <= 0) {
469 return NULL;
470 }
471
472 return make_union(args);
473}
474
Maggie Moss1b4552c2020-09-09 13:23:24 -0700475static PyMethodDef union_methods[] = {
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100476 {"_from_args", union_from_args, METH_O | METH_CLASS},
Maggie Moss1b4552c2020-09-09 13:23:24 -0700477 {"__instancecheck__", union_instancecheck, METH_O},
478 {"__subclasscheck__", union_subclasscheck, METH_O},
Pablo Galindo Salgado9356d1e2021-07-24 15:08:53 +0100479 {"__reduce__", union_reduce, METH_NOARGS},
Maggie Moss1b4552c2020-09-09 13:23:24 -0700480 {0}};
481
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300482
483static PyObject *
484union_getitem(PyObject *self, PyObject *item)
485{
486 unionobject *alias = (unionobject *)self;
487 // Populate __parameters__ if needed.
488 if (alias->parameters == NULL) {
489 alias->parameters = _Py_make_parameters(alias->args);
490 if (alias->parameters == NULL) {
491 return NULL;
492 }
493 }
494
495 PyObject *newargs = _Py_subs_parameters(self, alias->args, alias->parameters, item);
496 if (newargs == NULL) {
497 return NULL;
498 }
499
Miss Islington (bot)21db59f2021-07-22 15:18:49 -0700500 PyObject *res;
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700501 Py_ssize_t nargs = PyTuple_GET_SIZE(newargs);
Miss Islington (bot)21db59f2021-07-22 15:18:49 -0700502 if (nargs == 0) {
503 res = make_union(newargs);
504 }
505 else {
506 res = PyTuple_GET_ITEM(newargs, 0);
507 Py_INCREF(res);
508 for (Py_ssize_t iarg = 1; iarg < nargs; iarg++) {
509 PyObject *arg = PyTuple_GET_ITEM(newargs, iarg);
510 Py_SETREF(res, PyNumber_Or(res, arg));
511 if (res == NULL) {
512 break;
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700513 }
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700514 }
515 }
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300516 Py_DECREF(newargs);
517 return res;
518}
519
520static PyMappingMethods union_as_mapping = {
521 .mp_subscript = union_getitem,
522};
523
524static PyObject *
525union_parameters(PyObject *self, void *Py_UNUSED(unused))
526{
527 unionobject *alias = (unionobject *)self;
528 if (alias->parameters == NULL) {
529 alias->parameters = _Py_make_parameters(alias->args);
530 if (alias->parameters == NULL) {
531 return NULL;
532 }
533 }
534 Py_INCREF(alias->parameters);
535 return alias->parameters;
536}
537
538static PyGetSetDef union_properties[] = {
539 {"__parameters__", union_parameters, (setter)NULL, "Type variables in the types.Union.", NULL},
540 {0}
541};
542
Maggie Moss1b4552c2020-09-09 13:23:24 -0700543static PyNumberMethods union_as_number = {
kj4eb41d02020-11-09 12:00:13 +0800544 .nb_or = _Py_union_type_or, // Add __or__ function
Maggie Moss1b4552c2020-09-09 13:23:24 -0700545};
546
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700547PyTypeObject _PyUnion_Type = {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700548 PyVarObject_HEAD_INIT(&PyType_Type, 0)
549 .tp_name = "types.Union",
550 .tp_doc = "Represent a PEP 604 union type\n"
551 "\n"
552 "E.g. for int | str",
553 .tp_basicsize = sizeof(unionobject),
554 .tp_dealloc = unionobject_dealloc,
555 .tp_alloc = PyType_GenericAlloc,
Miss Islington (bot)08561342021-07-03 06:33:16 -0700556 .tp_free = PyObject_GC_Del,
557 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
558 .tp_traverse = union_traverse,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700559 .tp_hash = union_hash,
560 .tp_getattro = PyObject_GenericGetAttr,
561 .tp_members = union_members,
562 .tp_methods = union_methods,
563 .tp_richcompare = union_richcompare,
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300564 .tp_as_mapping = &union_as_mapping,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700565 .tp_as_number = &union_as_number,
566 .tp_repr = union_repr,
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300567 .tp_getset = union_properties,
Maggie Moss1b4552c2020-09-09 13:23:24 -0700568};
569
Miss Islington (bot)03aad302021-07-17 14:10:21 -0700570static PyObject *
571make_union(PyObject *args)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700572{
573 assert(PyTuple_CheckExact(args));
574
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300575 args = dedup_and_flatten_args(args);
576 if (args == NULL) {
577 return NULL;
578 }
579 if (PyTuple_GET_SIZE(args) == 1) {
580 PyObject *result1 = PyTuple_GET_ITEM(args, 0);
581 Py_INCREF(result1);
582 Py_DECREF(args);
583 return result1;
584 }
585
Miss Islington (bot)85b58292021-07-18 04:59:25 -0700586 unionobject *result = PyObject_GC_New(unionobject, &_PyUnion_Type);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700587 if (result == NULL) {
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300588 Py_DECREF(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700589 return NULL;
590 }
591
Serhiy Storchaka2d055ce2021-07-17 22:14:57 +0300592 result->parameters = NULL;
Serhiy Storchakac3007ab2021-07-16 14:48:20 +0300593 result->args = args;
Miss Islington (bot)000b9e82021-07-03 13:51:10 -0700594 _PyObject_GC_TRACK(result);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700595 return (PyObject*)result;
596}