blob: a66d61524dcfc7b03fe40746181363e5f83c2407 [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"
3#include "pycore_unionobject.h"
4#include "structmember.h"
5
6
7typedef struct {
8 PyObject_HEAD
9 PyObject *args;
10} unionobject;
11
12static void
13unionobject_dealloc(PyObject *self)
14{
15 unionobject *alias = (unionobject *)self;
16
17 Py_XDECREF(alias->args);
Neil Schemenauer0564aaf2020-10-27 11:55:52 -070018 Py_TYPE(self)->tp_free(self);
Maggie Moss1b4552c2020-09-09 13:23:24 -070019}
20
21static Py_hash_t
22union_hash(PyObject *self)
23{
24 unionobject *alias = (unionobject *)self;
25 Py_hash_t h1 = PyObject_Hash(alias->args);
26 if (h1 == -1) {
27 return -1;
28 }
29 return h1;
30}
31
32static int
33is_generic_alias_in_args(PyObject *args) {
34 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
35 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
36 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
Ken Jin49cd68f2021-01-03 00:19:15 +080037 if (PyObject_TypeCheck(arg, &Py_GenericAliasType)) {
Maggie Moss1b4552c2020-09-09 13:23:24 -070038 return 0;
39 }
40 }
41 return 1;
42}
43
44static PyObject *
45union_instancecheck(PyObject *self, PyObject *instance)
46{
47 unionobject *alias = (unionobject *) self;
48 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
49 if (!is_generic_alias_in_args(alias->args)) {
50 PyErr_SetString(PyExc_TypeError,
51 "isinstance() argument 2 cannot contain a parameterized generic");
52 return NULL;
53 }
54 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
55 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
56 if (arg == Py_None) {
57 arg = (PyObject *)&_PyNone_Type;
58 }
59 if (PyType_Check(arg) && PyObject_IsInstance(instance, arg) != 0) {
60 Py_RETURN_TRUE;
61 }
62 }
63 Py_RETURN_FALSE;
64}
65
66static PyObject *
67union_subclasscheck(PyObject *self, PyObject *instance)
68{
69 if (!PyType_Check(instance)) {
70 PyErr_SetString(PyExc_TypeError, "issubclass() arg 1 must be a class");
71 return NULL;
72 }
73 unionobject *alias = (unionobject *)self;
74 if (!is_generic_alias_in_args(alias->args)) {
75 PyErr_SetString(PyExc_TypeError,
76 "issubclass() argument 2 cannot contain a parameterized generic");
77 return NULL;
78 }
79 Py_ssize_t nargs = PyTuple_GET_SIZE(alias->args);
80 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
81 PyObject *arg = PyTuple_GET_ITEM(alias->args, iarg);
82 if (PyType_Check(arg) && (PyType_IsSubtype((PyTypeObject *)instance, (PyTypeObject *)arg) != 0)) {
83 Py_RETURN_TRUE;
84 }
85 }
86 Py_RETURN_FALSE;
87}
88
89static int
90is_typing_module(PyObject *obj) {
91 PyObject *module = PyObject_GetAttrString(obj, "__module__");
92 if (module == NULL) {
93 return -1;
94 }
95 int is_typing = PyUnicode_Check(module) && _PyUnicode_EqualToASCIIString(module, "typing");
96 Py_DECREF(module);
97 return is_typing;
98}
99
100static int
101is_typing_name(PyObject *obj, char *name)
102{
103 PyTypeObject *type = Py_TYPE(obj);
104 if (strcmp(type->tp_name, name) != 0) {
105 return 0;
106 }
107 return is_typing_module(obj);
108}
109
110static PyObject *
111union_richcompare(PyObject *a, PyObject *b, int op)
112{
113 PyObject *result = NULL;
114 if (op != Py_EQ && op != Py_NE) {
115 result = Py_NotImplemented;
116 Py_INCREF(result);
117 return result;
118 }
119
120 PyTypeObject *type = Py_TYPE(b);
121
122 PyObject* a_set = PySet_New(((unionobject*)a)->args);
123 if (a_set == NULL) {
124 return NULL;
125 }
126 PyObject* b_set = PySet_New(NULL);
127 if (b_set == NULL) {
128 goto exit;
129 }
130
131 // Populate b_set with the data from the right object
132 int is_typing_union = is_typing_name(b, "_UnionGenericAlias");
133 if (is_typing_union < 0) {
134 goto exit;
135 }
136 if (is_typing_union) {
137 PyObject *b_args = PyObject_GetAttrString(b, "__args__");
138 if (b_args == NULL) {
139 goto exit;
140 }
141 if (!PyTuple_CheckExact(b_args)) {
142 Py_DECREF(b_args);
143 PyErr_SetString(PyExc_TypeError, "__args__ argument of typing.Union object is not a tuple");
144 goto exit;
145 }
146 Py_ssize_t b_arg_length = PyTuple_GET_SIZE(b_args);
147 for (Py_ssize_t i = 0; i < b_arg_length; i++) {
148 PyObject* arg = PyTuple_GET_ITEM(b_args, i);
149 if (arg == (PyObject *)&_PyNone_Type) {
150 arg = Py_None;
151 }
152 if (PySet_Add(b_set, arg) == -1) {
153 Py_DECREF(b_args);
154 goto exit;
155 }
156 }
157 Py_DECREF(b_args);
158 } else if (type == &_Py_UnionType) {
159 PyObject* args = ((unionobject*) b)->args;
160 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
161 for (Py_ssize_t i = 0; i < arg_length; i++) {
162 PyObject* arg = PyTuple_GET_ITEM(args, i);
163 if (PySet_Add(b_set, arg) == -1) {
164 goto exit;
165 }
166 }
167 } else {
168 if (PySet_Add(b_set, b) == -1) {
169 goto exit;
170 }
171 }
172 result = PyObject_RichCompare(a_set, b_set, op);
173exit:
174 Py_XDECREF(a_set);
175 Py_XDECREF(b_set);
176 return result;
177}
178
179static PyObject*
180flatten_args(PyObject* args)
181{
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200182 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
183 Py_ssize_t total_args = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700184 // Get number of total args once it's flattened.
185 for (Py_ssize_t i = 0; i < arg_length; i++) {
186 PyObject *arg = PyTuple_GET_ITEM(args, i);
187 PyTypeObject* arg_type = Py_TYPE(arg);
188 if (arg_type == &_Py_UnionType) {
189 total_args += PyTuple_GET_SIZE(((unionobject*) arg)->args);
190 } else {
191 total_args++;
192 }
193 }
194 // Create new tuple of flattened args.
195 PyObject *flattened_args = PyTuple_New(total_args);
196 if (flattened_args == NULL) {
197 return NULL;
198 }
199 Py_ssize_t pos = 0;
200 for (Py_ssize_t i = 0; i < arg_length; i++) {
201 PyObject *arg = PyTuple_GET_ITEM(args, i);
202 PyTypeObject* arg_type = Py_TYPE(arg);
203 if (arg_type == &_Py_UnionType) {
204 PyObject* nested_args = ((unionobject*)arg)->args;
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200205 Py_ssize_t nested_arg_length = PyTuple_GET_SIZE(nested_args);
206 for (Py_ssize_t j = 0; j < nested_arg_length; j++) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700207 PyObject* nested_arg = PyTuple_GET_ITEM(nested_args, j);
208 Py_INCREF(nested_arg);
209 PyTuple_SET_ITEM(flattened_args, pos, nested_arg);
210 pos++;
211 }
212 } else {
213 Py_INCREF(arg);
214 PyTuple_SET_ITEM(flattened_args, pos, arg);
215 pos++;
216 }
217 }
218 return flattened_args;
219}
220
221static PyObject*
222dedup_and_flatten_args(PyObject* args)
223{
224 args = flatten_args(args);
225 if (args == NULL) {
226 return NULL;
227 }
228 Py_ssize_t arg_length = PyTuple_GET_SIZE(args);
229 PyObject *new_args = PyTuple_New(arg_length);
230 if (new_args == NULL) {
231 return NULL;
232 }
233 // Add unique elements to an array.
Victor Stinnerd73cf7c2020-09-26 12:48:41 +0200234 Py_ssize_t added_items = 0;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700235 for (Py_ssize_t i = 0; i < arg_length; i++) {
236 int is_duplicate = 0;
237 PyObject* i_element = PyTuple_GET_ITEM(args, i);
238 for (Py_ssize_t j = i + 1; j < arg_length; j++) {
239 PyObject* j_element = PyTuple_GET_ITEM(args, j);
kj463c7d32020-12-14 02:38:24 +0800240 int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) &&
241 PyObject_TypeCheck(j_element, &Py_GenericAliasType);
kj4eb41d02020-11-09 12:00:13 +0800242 // RichCompare to also deduplicate GenericAlias types (slower)
243 is_duplicate = is_ga ? PyObject_RichCompareBool(i_element, j_element, Py_EQ)
244 : i_element == j_element;
245 // Should only happen if RichCompare fails
246 if (is_duplicate < 0) {
247 Py_DECREF(args);
248 Py_DECREF(new_args);
249 return NULL;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700250 }
kj4eb41d02020-11-09 12:00:13 +0800251 if (is_duplicate)
252 break;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700253 }
254 if (!is_duplicate) {
255 Py_INCREF(i_element);
256 PyTuple_SET_ITEM(new_args, added_items, i_element);
257 added_items++;
258 }
259 }
260 Py_DECREF(args);
261 _PyTuple_Resize(&new_args, added_items);
262 return new_args;
263}
264
265static int
266is_typevar(PyObject *obj)
267{
268 return is_typing_name(obj, "TypeVar");
269}
270
271static int
272is_special_form(PyObject *obj)
273{
274 return is_typing_name(obj, "_SpecialForm");
275}
276
277static int
278is_new_type(PyObject *obj)
279{
280 PyTypeObject *type = Py_TYPE(obj);
281 if (type != &PyFunction_Type) {
282 return 0;
283 }
284 return is_typing_module(obj);
285}
286
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700287// Emulates short-circuiting behavior of the ``||`` operator
288// while also checking negative values.
289#define CHECK_RES(res) { \
290 int result = res; \
291 if (result) { \
292 return result; \
293 } \
294}
295
296// Returns 1 on true, 0 on false, and -1 on error.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700297static int
298is_unionable(PyObject *obj)
299{
300 if (obj == Py_None) {
301 return 1;
302 }
303 PyTypeObject *type = Py_TYPE(obj);
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700304 CHECK_RES(is_typevar(obj));
305 CHECK_RES(is_new_type(obj));
306 CHECK_RES(is_special_form(obj));
Maggie Moss1b4552c2020-09-09 13:23:24 -0700307 return (
Miss Islington (bot)7e6cad72021-06-23 02:38:49 -0700308 // The following checks never fail.
Maggie Moss1b4552c2020-09-09 13:23:24 -0700309 PyType_Check(obj) ||
kj463c7d32020-12-14 02:38:24 +0800310 PyObject_TypeCheck(obj, &Py_GenericAliasType) ||
Maggie Moss1b4552c2020-09-09 13:23:24 -0700311 type == &_Py_UnionType);
312}
313
kj4eb41d02020-11-09 12:00:13 +0800314PyObject *
315_Py_union_type_or(PyObject* self, PyObject* param)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700316{
317 PyObject *tuple = PyTuple_Pack(2, self, param);
318 if (tuple == NULL) {
319 return NULL;
320 }
321 PyObject *new_union = _Py_Union(tuple);
322 Py_DECREF(tuple);
323 return new_union;
324}
325
326static int
327union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
328{
329 _Py_IDENTIFIER(__module__);
330 _Py_IDENTIFIER(__qualname__);
331 _Py_IDENTIFIER(__origin__);
332 _Py_IDENTIFIER(__args__);
333 PyObject *qualname = NULL;
334 PyObject *module = NULL;
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300335 PyObject *tmp;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700336 PyObject *r = NULL;
337 int err;
338
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300339 if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700340 goto exit;
341 }
342
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300343 if (tmp) {
344 Py_DECREF(tmp);
345 if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700346 goto exit;
347 }
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300348 if (tmp) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700349 // It looks like a GenericAlias
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300350 Py_DECREF(tmp);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700351 goto use_repr;
352 }
353 }
354
355 if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
356 goto exit;
357 }
358 if (qualname == NULL) {
359 goto use_repr;
360 }
361 if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
362 goto exit;
363 }
364 if (module == NULL || module == Py_None) {
365 goto use_repr;
366 }
367
368 // Looks like a class
369 if (PyUnicode_Check(module) &&
370 _PyUnicode_EqualToASCIIString(module, "builtins"))
371 {
372 // builtins don't need a module name
373 r = PyObject_Str(qualname);
374 goto exit;
375 }
376 else {
377 r = PyUnicode_FromFormat("%S.%S", module, qualname);
378 goto exit;
379 }
380
381use_repr:
382 r = PyObject_Repr(p);
383exit:
384 Py_XDECREF(qualname);
385 Py_XDECREF(module);
386 if (r == NULL) {
387 return -1;
388 }
389 err = _PyUnicodeWriter_WriteStr(writer, r);
390 Py_DECREF(r);
391 return err;
392}
393
394static PyObject *
395union_repr(PyObject *self)
396{
397 unionobject *alias = (unionobject *)self;
398 Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
399
400 _PyUnicodeWriter writer;
401 _PyUnicodeWriter_Init(&writer);
402 for (Py_ssize_t i = 0; i < len; i++) {
403 if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
404 goto error;
405 }
406 PyObject *p = PyTuple_GET_ITEM(alias->args, i);
407 if (union_repr_item(&writer, p) < 0) {
408 goto error;
409 }
410 }
411 return _PyUnicodeWriter_Finish(&writer);
412error:
413 _PyUnicodeWriter_Dealloc(&writer);
414 return NULL;
415}
416
417static PyMemberDef union_members[] = {
418 {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
419 {0}
420};
421
422static PyMethodDef union_methods[] = {
423 {"__instancecheck__", union_instancecheck, METH_O},
424 {"__subclasscheck__", union_subclasscheck, METH_O},
425 {0}};
426
427static PyNumberMethods union_as_number = {
kj4eb41d02020-11-09 12:00:13 +0800428 .nb_or = _Py_union_type_or, // Add __or__ function
Maggie Moss1b4552c2020-09-09 13:23:24 -0700429};
430
431PyTypeObject _Py_UnionType = {
432 PyVarObject_HEAD_INIT(&PyType_Type, 0)
433 .tp_name = "types.Union",
434 .tp_doc = "Represent a PEP 604 union type\n"
435 "\n"
436 "E.g. for int | str",
437 .tp_basicsize = sizeof(unionobject),
438 .tp_dealloc = unionobject_dealloc,
439 .tp_alloc = PyType_GenericAlloc,
440 .tp_free = PyObject_Del,
441 .tp_flags = Py_TPFLAGS_DEFAULT,
442 .tp_hash = union_hash,
443 .tp_getattro = PyObject_GenericGetAttr,
444 .tp_members = union_members,
445 .tp_methods = union_methods,
446 .tp_richcompare = union_richcompare,
447 .tp_as_number = &union_as_number,
448 .tp_repr = union_repr,
449};
450
451PyObject *
452_Py_Union(PyObject *args)
453{
454 assert(PyTuple_CheckExact(args));
455
456 unionobject* result = NULL;
457
458 // Check arguments are unionable.
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200459 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700460 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
461 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
462 if (arg == NULL) {
463 return NULL;
464 }
465 int is_arg_unionable = is_unionable(arg);
466 if (is_arg_unionable < 0) {
467 return NULL;
468 }
469 if (!is_arg_unionable) {
470 Py_INCREF(Py_NotImplemented);
471 return Py_NotImplemented;
472 }
473 }
474
475 result = PyObject_New(unionobject, &_Py_UnionType);
476 if (result == NULL) {
477 return NULL;
478 }
479
480 result->args = dedup_and_flatten_args(args);
481 if (result->args == NULL) {
482 Py_DECREF(result);
483 return NULL;
484 }
485 return (PyObject*)result;
486}