CARVIEW |
Navigation Menu
-
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.
-
Sequence
is equivalent to collections.abc.Sequence.MutableSequence
is equivalent to collections.abc.MutableSequence.ImmutableSequence
is aSequence
that 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]
.
-
Set
is equivalent to collections.abc.Set.MutableSet
is equivalent to collections.abc.MutableSet.ImmutableSet
is aSet
that is not aMutableSet
. -
Set[ type ]
specializesSequence
, accepting a set with instances oftype
.
-
Mapping
is equivalent to collections.abc.Mapping.MutableMapping
is equivalent to collections.abc. MutableMapping.ImmutableMapping
is equivalent to types.MappingProxyType. -
Mapping[ key_type, val_type ]
specializesMapping
, accepting items with key ofkey_type
and value ofval_type
.
-
Iterator
is 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 oftype
elements. -
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 returnFalse
and do nothing on theinstance
object.
-
Iterable
is 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 oftype
elements. -
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 returnFalse
and do nothing on theinstance
object.
-
Callable
is equivalent to collections.abc.Callable and typing.Callable. -
Similar to
Iterator
andIterable
,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 returnFalse
and do nothing on theinstance
object.
-
Union[ type, ... ]
acceps instance that match one oftype, ...
. For example,isinstance(42, Union[int, float]
returnsTrue
. -
Any
accepts any object, including type and non-type objects. It's guaranteed thatisinstance(..., Any)
returnsTrue
andissubclass(..., Any)
returnsTrue
. -
Optional[T]
is equivalent toUnion[T, NoneType]
. -
NoneType
is an alias oftype(None)
.