-
Notifications
You must be signed in to change notification settings - Fork 2
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.
-
Sequenceis equivalent to collections.abc.Sequence.MutableSequenceis equivalent to collections.abc.MutableSequence.ImmutableSequenceis aSequencethat is not aMutableSequence. -
Sequence[ type ]specializesSequence, accepting a sequence with instances oftype. -
Sequence[ type, ... ]specializedSequence, accepting a sequence with instances of exactly mapping oftype, .... For example,Sequence[int, float]accepts(1, 2.0)or[1, 2.0].
-
Setis equivalent to collections.abc.Set.MutableSetis equivalent to collections.abc.MutableSet.ImmutableSetis aSetthat is not aMutableSet. -
Set[ type ]specializesSequence, accepting a set with instances oftype.
-
Mappingis equivalent to collections.abc.Mapping.MutableMappingis equivalent to collections.abc. MutableMapping.ImmutableMappingis equivalent to types.MappingProxyType. -
Mapping[ key_type, val_type ]specializesMapping, accepting items with key ofkey_typeand value ofval_type.
-
Iteratoris equivalent to collections.abc.Iterator. -
Iterator[ type ](iterator)andIterator[ 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 oftypeelements. -
Similar to
Sequence[ type, ... ],Iterator[ type, ... ]accepts an iterator represents instances with exactly mapping oftype, .... -
Dual to the side effect of iterating the iterator,
isinstance(instance, Iterator[ type ])andisinstance(instance, Iterator[ type, ... ])always returnFalseand do nothing on theinstanceobject.
-
Iterableis equivalent to collections.abc.Iterable. -
Similar to
Iterator[ ... ],Iterable[ type ](iterable)andIterable[ 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 oftypeelements. -
Similar to
Sequence[ type, ... ],Iterable[ type, ... ]accepts an iterable represents instances with exactly mapping oftype, .... -
Dual to the side effect of iterating the iterable,
isinstance(instance, Iterable[ type ])andisinstance(instance, Iterable[ type, ... ])always returnFalseand do nothing on theinstanceobject.
-
Callableis equivalent to collections.abc.Callable and typing.Callable. -
Similar to
IteratorandIterable,Callable[ ... ](callable)creates a callable wrapper with lazy type instrospection on the invocation of thecallable. 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 typereturn_type. -
Callable[ Ellipsis , return_type ]accepts a callable with arbitrary arguments and return value typereturn_type. -
Dual to the side effect of invoking a callable,
isinstance(instance, Callable[ ... ])always returnFalseand do nothing on theinstanceobject.
-
Union[ type, ... ]acceps instance that match one oftype, .... For example,isinstance(42, Union[int, float]returnsTrue. -
Anyaccepts any object, including type and non-type objects. It's guaranteed thatisinstance(..., Any)returnsTrueandissubclass(..., Any)returnsTrue. -
Optional[T]is equivalent toUnion[T, NoneType]. -
NoneTypeis an alias oftype(None).