@@ -237,9 +237,19 @@ dedup_and_flatten_args(PyObject* args)
237
237
PyObject * i_element = PyTuple_GET_ITEM (args , i );
238
238
for (Py_ssize_t j = i + 1 ; j < arg_length ; j ++ ) {
239
239
PyObject * j_element = PyTuple_GET_ITEM (args , j );
240
- if (i_element == j_element ) {
241
- is_duplicate = 1 ;
240
+ int is_ga = Py_TYPE (i_element ) == & Py_GenericAliasType &&
241
+ Py_TYPE (j_element ) == & Py_GenericAliasType ;
242
+ // 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 ;
242
250
}
251
+ if (is_duplicate )
252
+ break ;
243
253
}
244
254
if (!is_duplicate ) {
245
255
Py_INCREF (i_element );
@@ -290,8 +300,8 @@ is_unionable(PyObject *obj)
290
300
type == & _Py_UnionType );
291
301
}
292
302
293
- static PyObject *
294
- type_or ( PyTypeObject * self , PyObject * param )
303
+ PyObject *
304
+ _Py_union_type_or ( PyObject * self , PyObject * param )
295
305
{
296
306
PyObject * tuple = PyTuple_Pack (2 , self , param );
297
307
if (tuple == NULL ) {
@@ -404,7 +414,7 @@ static PyMethodDef union_methods[] = {
404
414
{0 }};
405
415
406
416
static PyNumberMethods union_as_number = {
407
- .nb_or = ( binaryfunc ) type_or , // Add __or__ function
417
+ .nb_or = _Py_union_type_or , // Add __or__ function
408
418
};
409
419
410
420
PyTypeObject _Py_UnionType = {
0 commit comments