blob: 05350363eed63f0cfdd33a543a77d85625be294e [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
287static int
288is_unionable(PyObject *obj)
289{
290 if (obj == Py_None) {
291 return 1;
292 }
293 PyTypeObject *type = Py_TYPE(obj);
294 return (
295 is_typevar(obj) ||
296 is_new_type(obj) ||
297 is_special_form(obj) ||
298 PyType_Check(obj) ||
kj463c7d32020-12-14 02:38:24 +0800299 PyObject_TypeCheck(obj, &Py_GenericAliasType) ||
Maggie Moss1b4552c2020-09-09 13:23:24 -0700300 type == &_Py_UnionType);
301}
302
kj4eb41d02020-11-09 12:00:13 +0800303PyObject *
304_Py_union_type_or(PyObject* self, PyObject* param)
Maggie Moss1b4552c2020-09-09 13:23:24 -0700305{
306 PyObject *tuple = PyTuple_Pack(2, self, param);
307 if (tuple == NULL) {
308 return NULL;
309 }
310 PyObject *new_union = _Py_Union(tuple);
311 Py_DECREF(tuple);
312 return new_union;
313}
314
315static int
316union_repr_item(_PyUnicodeWriter *writer, PyObject *p)
317{
318 _Py_IDENTIFIER(__module__);
319 _Py_IDENTIFIER(__qualname__);
320 _Py_IDENTIFIER(__origin__);
321 _Py_IDENTIFIER(__args__);
322 PyObject *qualname = NULL;
323 PyObject *module = NULL;
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300324 PyObject *tmp;
Maggie Moss1b4552c2020-09-09 13:23:24 -0700325 PyObject *r = NULL;
326 int err;
327
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300328 if (_PyObject_LookupAttrId(p, &PyId___origin__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700329 goto exit;
330 }
331
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300332 if (tmp) {
333 Py_DECREF(tmp);
334 if (_PyObject_LookupAttrId(p, &PyId___args__, &tmp) < 0) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700335 goto exit;
336 }
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300337 if (tmp) {
Maggie Moss1b4552c2020-09-09 13:23:24 -0700338 // It looks like a GenericAlias
Serhiy Storchaka98c44332020-10-10 22:23:42 +0300339 Py_DECREF(tmp);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700340 goto use_repr;
341 }
342 }
343
344 if (_PyObject_LookupAttrId(p, &PyId___qualname__, &qualname) < 0) {
345 goto exit;
346 }
347 if (qualname == NULL) {
348 goto use_repr;
349 }
350 if (_PyObject_LookupAttrId(p, &PyId___module__, &module) < 0) {
351 goto exit;
352 }
353 if (module == NULL || module == Py_None) {
354 goto use_repr;
355 }
356
357 // Looks like a class
358 if (PyUnicode_Check(module) &&
359 _PyUnicode_EqualToASCIIString(module, "builtins"))
360 {
361 // builtins don't need a module name
362 r = PyObject_Str(qualname);
363 goto exit;
364 }
365 else {
366 r = PyUnicode_FromFormat("%S.%S", module, qualname);
367 goto exit;
368 }
369
370use_repr:
371 r = PyObject_Repr(p);
372exit:
373 Py_XDECREF(qualname);
374 Py_XDECREF(module);
375 if (r == NULL) {
376 return -1;
377 }
378 err = _PyUnicodeWriter_WriteStr(writer, r);
379 Py_DECREF(r);
380 return err;
381}
382
383static PyObject *
384union_repr(PyObject *self)
385{
386 unionobject *alias = (unionobject *)self;
387 Py_ssize_t len = PyTuple_GET_SIZE(alias->args);
388
389 _PyUnicodeWriter writer;
390 _PyUnicodeWriter_Init(&writer);
391 for (Py_ssize_t i = 0; i < len; i++) {
392 if (i > 0 && _PyUnicodeWriter_WriteASCIIString(&writer, " | ", 3) < 0) {
393 goto error;
394 }
395 PyObject *p = PyTuple_GET_ITEM(alias->args, i);
396 if (union_repr_item(&writer, p) < 0) {
397 goto error;
398 }
399 }
400 return _PyUnicodeWriter_Finish(&writer);
401error:
402 _PyUnicodeWriter_Dealloc(&writer);
403 return NULL;
404}
405
406static PyMemberDef union_members[] = {
407 {"__args__", T_OBJECT, offsetof(unionobject, args), READONLY},
408 {0}
409};
410
411static PyMethodDef union_methods[] = {
412 {"__instancecheck__", union_instancecheck, METH_O},
413 {"__subclasscheck__", union_subclasscheck, METH_O},
414 {0}};
415
416static PyNumberMethods union_as_number = {
kj4eb41d02020-11-09 12:00:13 +0800417 .nb_or = _Py_union_type_or, // Add __or__ function
Maggie Moss1b4552c2020-09-09 13:23:24 -0700418};
419
420PyTypeObject _Py_UnionType = {
421 PyVarObject_HEAD_INIT(&PyType_Type, 0)
422 .tp_name = "types.Union",
423 .tp_doc = "Represent a PEP 604 union type\n"
424 "\n"
425 "E.g. for int | str",
426 .tp_basicsize = sizeof(unionobject),
427 .tp_dealloc = unionobject_dealloc,
428 .tp_alloc = PyType_GenericAlloc,
429 .tp_free = PyObject_Del,
430 .tp_flags = Py_TPFLAGS_DEFAULT,
431 .tp_hash = union_hash,
432 .tp_getattro = PyObject_GenericGetAttr,
433 .tp_members = union_members,
434 .tp_methods = union_methods,
435 .tp_richcompare = union_richcompare,
436 .tp_as_number = &union_as_number,
437 .tp_repr = union_repr,
438};
439
440PyObject *
441_Py_Union(PyObject *args)
442{
443 assert(PyTuple_CheckExact(args));
444
445 unionobject* result = NULL;
446
447 // Check arguments are unionable.
Victor Stinnerd67de0a2020-09-23 23:25:54 +0200448 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
Maggie Moss1b4552c2020-09-09 13:23:24 -0700449 for (Py_ssize_t iarg = 0; iarg < nargs; iarg++) {
450 PyObject *arg = PyTuple_GET_ITEM(args, iarg);
451 if (arg == NULL) {
452 return NULL;
453 }
454 int is_arg_unionable = is_unionable(arg);
455 if (is_arg_unionable < 0) {
456 return NULL;
457 }
458 if (!is_arg_unionable) {
459 Py_INCREF(Py_NotImplemented);
460 return Py_NotImplemented;
461 }
462 }
463
464 result = PyObject_New(unionobject, &_Py_UnionType);
465 if (result == NULL) {
466 return NULL;
467 }
468
469 result->args = dedup_and_flatten_args(args);
470 if (result->args == NULL) {
471 Py_DECREF(result);
472 return NULL;
473 }
474 return (PyObject*)result;
475}