Skip to content

Follow up for #283 (response to comments) including change in Union[X, Any] #288

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 3 commits into from
Sep 29, 2016
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
17 changes: 7 additions & 10 deletions python2/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,13 @@ def test_subclass_error(self):
def test_union_any(self):
u = Union[Any]
self.assertEqual(u, Any)
u = Union[int, Any]
self.assertEqual(u, Any)
u = Union[Any, int]
self.assertEqual(u, Any)
u1 = Union[int, Any]
u2 = Union[Any, int]
u3 = Union[Any, object]
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.

Why not keep the test but change the asserts to test for the new behavior?

self.assertNotEqual(u1, Any)
self.assertNotEqual(u2, Any)
self.assertNotEqual(u3, Any)

def test_union_object(self):
u = Union[object]
Expand All @@ -214,12 +217,6 @@ def test_union_object(self):
u = Union[object, int]
self.assertEqual(u, object)

def test_union_any_object(self):
u = Union[object, Any]
self.assertEqual(u, Any)
u = Union[Any, object]
self.assertEqual(u, Any)

def test_unordered(self):
u1 = Union[int, float]
u2 = Union[float, int]
Expand Down
35 changes: 17 additions & 18 deletions python2/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __new__(cls, *args, **kwds):
isinstance(args[1], tuple)):
# Close enough.
raise TypeError("Cannot subclass %r" % cls)
return object.__new__(cls)
return super(_TypingBase, cls).__new__(cls)

# Things that are not classes also need these.
def _eval_type(self, globalns, localns):
Expand All @@ -168,7 +168,11 @@ def __call__(self, *args, **kwds):


class _FinalTypingBase(_TypingBase):
"""Mix-in class to prevent instantiation."""
"""Mix-in class to prevent instantiation.

Prevents instantiation unless _root=True is given in class call.
It is used to create pseudo-singleton instances Any, Union, Tuple, etc.
"""

__slots__ = ()

Expand Down Expand Up @@ -256,7 +260,7 @@ def __init__(self, name, type_var, impl_type, type_checker):
assert isinstance(name, basestring), repr(name)
assert isinstance(impl_type, type), repr(impl_type)
assert not isinstance(impl_type, TypingMeta), repr(impl_type)
assert isinstance(type_var, (type, _TypingBase))
assert isinstance(type_var, (type, _TypingBase)), repr(type_var)
self.name = name
self.type_var = type_var
self.impl_type = impl_type
Expand Down Expand Up @@ -427,9 +431,13 @@ def __new__(cls, name, bases, namespace):
class _Any(_FinalTypingBase):
"""Special type indicating an unconstrained type.

- Any object is an instance of Any.
- Any class is a subclass of Any.
- As a special case, Any and object are subclasses of each other.
- Any is compatible with every type.
- Any assumed to have all methods.
- All values assumed to be instances of Any.

Note that all the above statements are true from the point of view of
static type checkers. At runtime, Any should not be used with instance
or class checks.
"""
__metaclass__ = AnyMeta
__slots__ = ()
Expand Down Expand Up @@ -598,16 +606,10 @@ class Manager(Employee): pass
Union[Manager, int, Employee] == Union[int, Employee]
Union[Employee, Manager] == Employee
Copy link
Member

Choose a reason for hiding this comment

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

I'd keep this even though it's not a special case -- it's a nice edge case that confirms the rule.


- Corollary: if Any is present it is the sole survivor, e.g.::

Union[int, Any] == Any

- Similar for object::

Union[int, object] == object

- To cut a tie: Union[object, Any] == Union[Any, object] == Any.

- You cannot subclass or instantiate a union.

- You cannot write Union[X][Y] (what would it mean?).
Expand Down Expand Up @@ -646,14 +648,11 @@ def __new__(cls, parameters=None, *args, **kwds):
assert not all_params, all_params
# Weed out subclasses.
# E.g. Union[int, Employee, Manager] == Union[int, Employee].
# If Any or object is present it will be the sole survivor.
# If both Any and object are present, Any wins.
# Never discard type variables, except against Any.
# 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 t1 is Any:
return Any
if not isinstance(t1, type):
continue
if any(isinstance(t2, type) and issubclass(t1, t2)
Expand Down Expand Up @@ -726,7 +725,7 @@ def __new__(cls, name, bases, namespace):
class _Optional(_FinalTypingBase):
"""Optional type.

Optional[X] is equivalent to Union[X, type(None)].
Optional[X] is equivalent to Union[X, None].
"""

__metaclass__ = OptionalMeta
Expand Down
17 changes: 7 additions & 10 deletions src/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,13 @@ def test_subclass_error(self):
def test_union_any(self):
u = Union[Any]
self.assertEqual(u, Any)
u = Union[int, Any]
self.assertEqual(u, Any)
u = Union[Any, int]
self.assertEqual(u, Any)
u1 = Union[int, Any]
u2 = Union[Any, int]
u3 = Union[Any, object]
self.assertEqual(u1, u2)
self.assertNotEqual(u1, Any)
self.assertNotEqual(u2, Any)
self.assertNotEqual(u3, Any)

def test_union_object(self):
u = Union[object]
Expand All @@ -215,12 +218,6 @@ def test_union_object(self):
u = Union[object, int]
self.assertEqual(u, object)

def test_union_any_object(self):
u = Union[object, Any]
self.assertEqual(u, Any)
u = Union[Any, object]
self.assertEqual(u, Any)

def test_unordered(self):
u1 = Union[int, float]
u2 = Union[float, int]
Expand Down
38 changes: 18 additions & 20 deletions src/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ class _TypingBase(metaclass=TypingMeta, _root=True):

__slots__ = ()


def __init__(self, *args, **kwds):
pass

Expand All @@ -158,7 +157,7 @@ def __new__(cls, *args, **kwds):
isinstance(args[1], tuple)):
# Close enough.
raise TypeError("Cannot subclass %r" % cls)
return object.__new__(cls)
return super().__new__(cls)

# Things that are not classes also need these.
def _eval_type(self, globalns, localns):
Expand All @@ -177,7 +176,11 @@ def __call__(self, *args, **kwds):


class _FinalTypingBase(_TypingBase, _root=True):
"""Mix-in class to prevent instantiation."""
"""Mix-in class to prevent instantiation.

Prevents instantiation unless _root=True is given in class call.
It is used to create pseudo-singleton instances Any, Union, Tuple, etc.
"""

__slots__ = ()

Expand Down Expand Up @@ -273,7 +276,7 @@ def __init__(self, name, type_var, impl_type, type_checker):
assert isinstance(name, str), repr(name)
assert isinstance(impl_type, type), repr(impl_type)
assert not isinstance(impl_type, TypingMeta), repr(impl_type)
assert isinstance(type_var, (type, _TypingBase))
assert isinstance(type_var, (type, _TypingBase)), repr(type_var)
self.name = name
self.type_var = type_var
self.impl_type = impl_type
Expand Down Expand Up @@ -375,9 +378,13 @@ def _type_repr(obj):
class _Any(_FinalTypingBase, _root=True):
"""Special type indicating an unconstrained type.

- Any object is an instance of Any.
- Any class is a subclass of Any.
- As a special case, Any and object are subclasses of each other.
- Any is compatible with every type.
- Any assumed to have all methods.
- All values assumed to be instances of Any.

Note that all the above statements are true from the point of view of
static type checkers. At runtime, Any should not be used with instance
or class checks.
"""

__slots__ = ()
Expand Down Expand Up @@ -502,7 +509,7 @@ def inner(*args, **kwds):
try:
return cached(*args, **kwds)
except TypeError:
pass # Do not duplicate real errors.
pass # All real errors (not unhashable args) are raised below.
return func(*args, **kwds)
return inner

Expand Down Expand Up @@ -542,16 +549,10 @@ class Manager(Employee): pass
Union[Manager, int, Employee] == Union[int, Employee]
Union[Employee, Manager] == Employee

- Corollary: if Any is present it is the sole survivor, e.g.::

Union[int, Any] == Any

- Similar for object::

Union[int, object] == object

- To cut a tie: Union[object, Any] == Union[Any, object] == Any.

- You cannot subclass or instantiate a union.

- You cannot write Union[X][Y] (what would it mean?).
Expand Down Expand Up @@ -589,14 +590,11 @@ def __new__(cls, parameters=None, *args, _root=False):
assert not all_params, all_params
# Weed out subclasses.
# E.g. Union[int, Employee, Manager] == Union[int, Employee].
# If Any or object is present it will be the sole survivor.
# If both Any and object are present, Any wins.
# Never discard type variables, except against Any.
# 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 t1 is Any:
return Any
if not isinstance(t1, type):
continue
if any(isinstance(t2, type) and issubclass(t1, t2)
Expand Down Expand Up @@ -662,7 +660,7 @@ def __subclasscheck__(self, cls):
class _Optional(_FinalTypingBase, _root=True):
"""Optional type.

Optional[X] is equivalent to Union[X, type(None)].
Optional[X] is equivalent to Union[X, None].
"""

__slots__ = ()
Expand Down