braincore.mixin.OneOfTypes

Contents

braincore.mixin.OneOfTypes#

braincore.mixin.OneOfTypes = typing.OneOfTypes#

Sole type; OneOfTypes[X, Y] means either X or Y.

To define a union, use e.g. OneOfTypes[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.:

    assert OneOfTypes[OneOfTypes[int, str], float] == OneOfTypes[int, str, float]
    
  • Unions of a single argument vanish, e.g.:

    assert OneOfTypes[int] == int  # The constructor actually returns int
    
  • Redundant arguments are skipped, e.g.:

    assert OneOfTypes[int, str, int] == OneOfTypes[int, str]
    
  • When comparing unions, the argument order is ignored, e.g.:

    assert OneOfTypes[int, str] == OneOfTypes[str, int]
    
  • You cannot subclass or instantiate a union.

  • You can use Optional[X] as a shorthand for OneOfTypes[X, None].