Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-44652: Preserve natural order of args in the union type.
  • Loading branch information
serhiy-storchaka committed Jul 16, 2021
commit c540b34fc4580bb6284aeccc07b5de2beb82195d
39 changes: 34 additions & 5 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ class Example:

class Forward: ...

def clear_typing_caches():
for f in typing._cleanups:
f()


class TypesTests(unittest.TestCase):

def test_truth_values(self):
Expand Down Expand Up @@ -706,11 +711,35 @@ def test_or_type_operator_with_TypeVar(self):
assert str | TV == typing.Union[str, TV]

def test_union_args(self):
self.assertEqual((int | str).__args__, (int, str))
self.assertEqual(((int | str) | list).__args__, (int, str, list))
self.assertEqual((int | (str | list)).__args__, (int, str, list))
self.assertEqual((int | None).__args__, (int, type(None)))
self.assertEqual((int | type(None)).__args__, (int, type(None)))
def check(arg, expected):
clear_typing_caches()
self.assertEqual(arg.__args__, expected)

check(int | str, (int, str))
check((int | str) | list, (int, str, list))
check(int | (str | list), (int, str, list))
check((int | str) | int, (int, str))
check(int | (str | int), (int, str))
check((int | str) | (str | int), (int, str))
check(typing.Union[int, str] | list, (int, str, list))
check(int | typing.Union[str, list], (int, str, list))
check((int | str) | (list | int), (int, str, list))
check((int | str) | typing.Union[list, int], (int, str, list))
check(typing.Union[int, str] | (list | int), (int, str, list))
check((str | int) | (int | list), (str, int, list))
check((str | int) | typing.Union[int, list], (str, int, list))
check(typing.Union[str, int] | (int | list), (str, int, list))
return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I forgot to remove it.

check(int | type(None), (int, type(None)))
check(type(None) | int, (type(None), int))

args = (int, list[int], typing.List[int],
typing.Tuple[int, int], typing.Callable[[int], int],
typing.Hashable, typing.TypeVar('T'))
for x in args:
with self.subTest(x):
check(x | None, (x, type(None)))
check(None | x, (type(None), x))

def test_union_parameter_chaining(self):
T = typing.TypeVar("T")
Expand Down
8 changes: 4 additions & 4 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,8 +988,8 @@ def __hash__(self):
def __or__(self, right):
return Union[self, right]

def __ror__(self, right):
return Union[self, right]
def __ror__(self, left):
return Union[left, self]

@_tp_cache
def __getitem__(self, params):
Expand Down Expand Up @@ -1099,8 +1099,8 @@ def __reduce__(self):
def __or__(self, right):
return Union[self, right]

def __ror__(self, right):
return Union[self, right]
def __ror__(self, left):
return Union[left, self]

class _CallableGenericAlias(_GenericAlias, _root=True):
def __repr__(self):
Expand Down
4 changes: 2 additions & 2 deletions Objects/unionobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ dedup_and_flatten_args(PyObject* args)
for (Py_ssize_t i = 0; i < arg_length; i++) {
int is_duplicate = 0;
PyObject* i_element = PyTuple_GET_ITEM(args, i);
for (Py_ssize_t j = i + 1; j < arg_length; j++) {
PyObject* j_element = PyTuple_GET_ITEM(args, j);
for (Py_ssize_t j = 0; j < added_items; j++) {

@Fidget-Spinner Fidget-Spinner Jul 16, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed this should be faster too (for a small number of unique members :).

PyObject* j_element = PyTuple_GET_ITEM(new_args, j);
int is_ga = PyObject_TypeCheck(i_element, &Py_GenericAliasType) &&
PyObject_TypeCheck(j_element, &Py_GenericAliasType);
// RichCompare to also deduplicate GenericAlias types (slower)
Expand Down