Skip to content

magic_constrains.types

Zhan Haoxun edited this page May 3, 2016 · 5 revisions

magic_constrains.types

Supported ABCs and avaliable forms of specialization:

type     ::=    abc
              | speical
              | <any other type object>

abc      ::=    sequence
              | set
              | mapping 
              | iterator
              | iterable
              | callable

sequence ::=    Sequence
              | Sequence          [ type ]
              | Sequence          [ type , ... ]
              | MutableSequence
              | MutableSequence   [ type ]
              | MutableSequence   [ type , ... ]
              | ImmutableSequence
              | ImmutableSequence [ type ]
              | ImmutableSequence [ type , ... ]

set      ::=    Set
              | Set               [ type ]
              | MutableSet
              | MutableSet        [ type ]
              | ImmutableSet
              | ImmutableSet      [ type ]

mapping  ::=    Mapping
              | Mapping           [ type , type ]
              | MutableMapping
              | MutableMapping    [ type , type ]
              | ImmutableMapping
              | ImmutableMapping  [ type , type ]

iterator ::=    Iterator
              | Iterator          [ type ]
              | Iterator          [ type , ... ]

iterable ::=    Iterable
              | Iterable          [ type ]
              | Iterable          [ type , ... ]

callable ::=    Callable
              | Callable          [ [ type, ... ] , type ]
              | Callable          [ Ellipsis , type ]
              
speical  ::=  | Union             [ type, ... ]
              | Any
              | Optional          [ type ]
              | NoneType
              

Explanations are as follow.

type means type object in Python. abc defines several supported ABCs. speical defines some type objects for some special purposes.

sequence

  • Sequence is equivalent to collections.abc.Sequence. MutableSequence is equivalent to collections.abc.MutableSequence. ImmutableSequence is a Sequence that is not a MutableSequence.

  • Sequence[ type ] specializes Sequence, accepting a sequence with instances of type.

  • Sequence[ type, ... ] specialized Sequence, accepting a sequence with instances of exactly mapping of type, .... For example, Sequence[int, float] accepts (1, 2.0) or [1, 2.0].

set

mapping

iterator

  • Iterator is equivalent to collections.abc.Iterator.

  • Iterator[ type ](iterator) and Iterator[ type, ... ](iterator) creates a iterator proxy with lazy type instrospection on the elements pointed by the original iterator. Example:

    for i in Iterator[int](iter([1, 2, 3])):
        print(i)
  • Similar to Sequence[ type ], Iterator[ type ] accepts an iterator represents arbitrary number of type elements.

  • Similar to Sequence[ type, ... ], Iterator[ type, ... ] accepts an iterator represents instances with exactly mapping of type, ....

  • Dual to the side effect of iterating the iterator, isinstance(instance, Iterator[ type ]) and isinstance(instance, Iterator[ type, ... ]) always return False and do nothing on the instance object.

iterable

  • Iterable is equivalent to collections.abc.Iterable.

  • Similar to Iterator[ ... ], Iterable[ type ](iterable) and Iterable[ type, ... ](iterable) creates a iterable proxy with lazy type instrospection on its elements. Example:

    for i in Iterable[int]([1, 2, 3]):
        print(i)
  • Similar to Sequence[ type ], Iterable[ type ] accepts an iterable represents arbitrary number of type elements.

  • Similar to Sequence[ type, ... ], Iterable[ type, ... ] accepts an iterable represents instances with exactly mapping of type, ....

  • Dual to the side effect of iterating the iterable, isinstance(instance, Iterable[ type ]) and isinstance(instance, Iterable[ type, ... ]) always return False and do nothing on the instance object.

callable

  • Callable is equivalent to collections.abc.Callable and typing.Callable.

  • Similar to Iterator and Iterable, Callable[ ... ](callable) creates a callable wrapper with lazy type instrospection on the invocation of the callable. Example:

    f = Callable[[int, float], float](lambda a, b: a + b)
    
    # ok
    f(1, 42.0)
    
    # type error.
    f(1, 42)
  • Callable[ [ parameter_type, ... ] , return_type ] accepts a callable with parameter types [ parameter_type, ... ] and return value type return_type.

  • Callable[ Ellipsis , return_type ] accepts a callable with arbitrary arguments and return value type return_type.

  • Dual to the side effect of invoking a callable, isinstance(instance, Callable[ ... ]) always return False and do nothing on the instance object.

special

  • Union[ type, ... ] acceps instance that match one of type, .... For example, isinstance(42, Union[int, float] returns True.

  • Any accepts any object, including type and non-type objects. It's guaranteed that isinstance(..., Any) returns True and issubclass(..., Any) returns True.

  • Optional[T] is equivalent to Union[T, NoneType].

  • NoneType is an alias of type(None).

Clone this wiki locally