@@ -237,9 +237,19 @@ dedup_and_flatten_args(PyObject* args)
237237 PyObject * i_element = PyTuple_GET_ITEM (args , i );
238238 for (Py_ssize_t j = i + 1 ; j < arg_length ; j ++ ) {
239239 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 ;
242250 }
251+ if (is_duplicate )
252+ break ;
243253 }
244254 if (!is_duplicate ) {
245255 Py_INCREF (i_element );
@@ -290,8 +300,8 @@ is_unionable(PyObject *obj)
290300 type == & _Py_UnionType );
291301}
292302
293- static PyObject *
294- type_or ( PyTypeObject * self , PyObject * param )
303+ PyObject *
304+ _Py_union_type_or ( PyObject * self , PyObject * param )
295305{
296306 PyObject * tuple = PyTuple_Pack (2 , self , param );
297307 if (tuple == NULL ) {
@@ -404,7 +414,7 @@ static PyMethodDef union_methods[] = {
404414 {0 }};
405415
406416static PyNumberMethods union_as_number = {
407- .nb_or = ( binaryfunc ) type_or , // Add __or__ function
417+ .nb_or = _Py_union_type_or , // Add __or__ function
408418};
409419
410420PyTypeObject _Py_UnionType = {
0 commit comments