Skip to content

bpo-28556: Don't simplify unions at runtime #6841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 3 additions & 4 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -955,16 +955,15 @@ The module defines the following classes, functions and decorators:

Union[int, str] == Union[str, int]

* When a class and its subclass are present, the latter is skipped, e.g.::

Union[int, object] == object

* You cannot subclass or instantiate a union.

* You cannot write ``Union[X][Y]``.

* You can use ``Optional[X]`` as a shorthand for ``Union[X, None]``.

.. versionchanged:: 3.7
Don't remove explicit subclasses from unions at runtime.

.. data:: Optional

Optional type.
Expand Down
24 changes: 12 additions & 12 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,11 @@ def test_union_any(self):
def test_union_object(self):
u = Union[object]
self.assertEqual(u, object)
u = Union[int, object]
self.assertEqual(u, object)
u = Union[object, int]
self.assertEqual(u, object)
u1 = Union[int, object]
u2 = Union[object, int]
self.assertEqual(u1, u2)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe also assert that it isn't object?

self.assertNotEqual(u1, object)
self.assertNotEqual(u2, object)

def test_unordered(self):
u1 = Union[int, float]
Expand All @@ -267,13 +268,11 @@ def test_single_class_disappears(self):
t = Union[Employee]
self.assertIs(t, Employee)

def test_base_class_disappears(self):
u = Union[Employee, Manager, int]
self.assertEqual(u, Union[int, Employee])
u = Union[Manager, int, Employee]
self.assertEqual(u, Union[int, Employee])
def test_base_class_kept(self):
u = Union[Employee, Manager]
self.assertIs(u, Employee)
self.assertNotEqual(u, Employee)
self.assertIn(Employee, u.__args__)
self.assertIn(Manager, u.__args__)

def test_union_union(self):
u = Union[int, float]
Expand Down Expand Up @@ -317,7 +316,8 @@ def test_cannot_instantiate(self):
def test_union_generalization(self):
self.assertFalse(Union[str, typing.Iterable[int]] == str)
self.assertFalse(Union[str, typing.Iterable[int]] == typing.Iterable[int])
self.assertTrue(Union[str, typing.Iterable] == typing.Iterable)
Copy link
Member

Choose a reason for hiding this comment

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

How about a more positive test of the expected members?

self.assertIn(str, Union[str, typing.Iterable[int]].__args__)
self.assertIn(typing.Iterable[int], Union[str, typing.Iterable[int]].__args__)

def test_union_compare_other(self):
self.assertNotEqual(Union, object)
Expand Down Expand Up @@ -917,7 +917,7 @@ def test_extended_generic_rules_eq(self):
self.assertEqual(Union[T, U][int, Union[int, str]], Union[int, str])
class Base: ...
class Derived(Base): ...
self.assertEqual(Union[T, Base][Derived], Base)
self.assertEqual(Union[T, Base][Union[Base, Derived]], Union[Base, Derived])
with self.assertRaises(TypeError):
Union[T, int][1]

Expand Down
32 changes: 3 additions & 29 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ def _check_generic(cls, parameters):


def _remove_dups_flatten(parameters):
"""An internal helper for Union creation and substitution: flatten Union's
among parameters, then remove duplicates and strict subclasses.
"""An internal helper for Union creation and substitution: flatten Unions
among parameters, then remove duplicates.
"""
# Flatten out Union[Union[...], ...].
params = []
Expand All @@ -224,20 +224,7 @@ def _remove_dups_flatten(parameters):
all_params.remove(t)
params = new_params
assert not all_params, all_params
# Weed out subclasses.
# E.g. Union[int, Employee, Manager] == Union[int, Employee].
# If object is present it will be sole survivor among proper classes.
# Never discard type variables.
# (In particular, Union[str, AnyStr] != AnyStr.)
all_params = set(params)
for t1 in params:
if not isinstance(t1, type):
continue
if any((isinstance(t2, type) or
isinstance(t2, _GenericAlias) and t2._special) and issubclass(t1, t2)
for t2 in all_params - {t1}):
all_params.remove(t1)
return tuple(t for t in params if t in all_params)
return tuple(params)


_cleanups = []
Expand Down Expand Up @@ -436,19 +423,6 @@ class Starship:

Union[int, str] == Union[str, int]

- When two arguments have a subclass relationship, the least
derived argument is kept, e.g.::

class Employee: pass
class Manager(Employee): pass
Union[int, Employee, Manager] == Union[int, Employee]
Union[Manager, int, Employee] == Union[int, Employee]
Union[Employee, Manager] == Employee

- Similar for object::

Union[int, object] == object

- You cannot subclass or instantiate a union.
- You can use Optional[X] as a shorthand for Union[X, None].
""")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Do not simplify arguments to `typing.Union`. Now `Union[Manager, Employee]`
is not simplified to `Employee` at runtime. Such simplification previously
caused several bugs and limited possibilities for introspection.