Package horizons :: Package ext :: Module typing :: Class Union
[hide private]
[frames] | no frames]

Class Union

object --+    
         |    
     Final --+
             |
            Union

Union type; Union[X, Y] means either X or Y.

To define a union, use e.g. Union[int, str].  Details:

- The arguments must be types and there must be at least one.

- None as an argument is a special case and is replaced by
  type(None).

- Unions of unions are flattened, e.g.::

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

- Unions of a single argument vanish, e.g.::

    Union[int] == int  # The constructor actually returns int

- Redundant arguments are skipped, e.g.::

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

- When comparing unions, the argument order is ignored, e.g.::

    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

- 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?).

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

Nested Classes [hide private]
  __metaclass__
Metaclass for Union.
Instance Methods [hide private]
    Inherited from object
 
__delattr__(...)
x.__delattr__('name') <==> del x.name
 
__format__(...)
default object formatter
 
__getattribute__(...)
x.__getattribute__('name') <==> x.name
 
__hash__(x)
hash(x)
 
__init__(...)
x.__init__(...) initializes x; see help(type(x)) for signature
 
__reduce__(...)
helper for pickle
 
__reduce_ex__(...)
helper for pickle
 
__repr__(x)
repr(x)
 
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
int
__sizeof__()
size of object in memory, in bytes
 
__str__(x)
str(x)
 
__subclasshook__(...)
Abstract classes can override this to customize issubclass().
Static Methods [hide private]
    Inherited from Final
a new object with type S, a subtype of T
__new__(self, *args, **kwds)
Class Variables [hide private]
  __union_params__ = None
hash(x)
  __union_set_params__ = None
hash(x)
Properties [hide private]
    Inherited from object
  __class__
the object's class