list, tuple, set, frozenset, dict, str, bytes, bytearray, range, Container, Hashable, Iterable, Iterator, Reversible, Generator, Sized, Collection, Sequence, MutableSequence, Set, MutableSet, Mapping, MutableMapping, MappingView, ItemsView, KeysView and ValuesView can be used for iterables as shown below:
*Memo:
- For
str,bytes,bytearray,range,Hashable,SizedandMappingView:- Each has no parameters.
-
Hashablecannot be used for an iterator in mypy even it's immutable according to the issue. -
MappingViewaccepts itself,dict_keys(KeysView),dict_values(ValuesView) anddict_items(ItemsView).
- For
list,set,MutableSequenceandMutableSet:- The 1st parameter is
_T(Required-PO-Type:Type):- It's a type.
- Use
KeysViewandValuesViewinstead ofdict_keysanddict_valuesrespectively because type checkers don't supportdict_keysanddict_valuesin _collections_abc.
- The 1st parameter is
- For
list,set,frozenset,Container,Iterable,Iterator,Reversible,Generator,Collection,Sequence,MutableSequence,Set,MutableSet,KeysViewandValuesView:- The 1st parameter is
_T(Required-PO-Type:Type):- It's a type.
- Use
Iteratorinstead of iter because type checkers don't supportiter. - Use
KeysViewandValuesViewinstead ofdict_keysanddict_valuesrespectively because type checkers don't supportdict_keysanddict_valuesin _collections_abc.
- The 1st parameter is
- For
dict,Mapping,MutableMappingandItemsView:- The 1st argument is
keytype(Required:-Type:list(Type)):- It's a key type.
- Don't use
keytype=.
- The 2nd argument is
valuetype(Required-Type:Type)):- It's a value type.
- Don't use
valuetype=.
- Use
ItemsViewinstead ofdict_itemsbecause type checkers don't supportdict_itemsin _collections_abc.
- The 1st argument is
- For
tuple:- The 1st arguments are
*types(Required:-Type:Type):- It's one or more types.
- Only in the 2nd argument,
...(but notEllipsis) can be used to accept the zero or more arguments of a type. - Don't use any keywords like *types=, types=, etc.
-
tupleis special so each type must be specified for each value. - Unpacking a tuple of types is possible with
*from Python 3.11:- The old way with Unpack is still possible.
- The doc explains the type hint with a tuple.
- The 1st arguments are
<list>:
from collections.abc import (
MutableSequence, Sequence, Reversible,
Collection, Sized, Container, Iterable
)
v: list[int] = [0, 1, 2, 3, 4]
# v: MutableSequence[int] = [0, 1, 2, 3, 4]
# v: Sequence[int] = [0, 1, 2, 3, 4]
# v: Reversible[int] = [0, 1, 2, 3, 4]
# v: Collection[int] = [0, 1, 2, 3, 4]
# v: Sized = [0, 1, 2, 3, 4]
# v: Container[int] = [0, 1, 2, 3, 4]
# v: Iterable[int] = [0, 1, 2, 3, 4]
print(v)
# No error
print(v[0], v[1:3])
# Reversible, Collection, Sized, Container and Iterable get error.
print(*v)
# Sized and Container get error.
print(v+v)
# MutableSequence, Sequence, Reversible, Collection,
# Sized, Container, Iterable get error
<tuple>:
from collections.abc import (
Hashable, Sequence, Reversible, Collection, Sized, Container, Iterable
)
# from typing import Unpack
v: tuple[int, ...] = (0, 1, 2, 3, 4)
# v: tuple[int, int, int, int, int] = (0, 1, 2, 3, 4)
# v: tuple[int, int, *tuple[int, ...]] = (0, 1, 2, 3, 4)
# v: tuple[int, int, Unpack[tuple[int, ...]]] = (0, 1, 2, 3, 4)
# v: tuple[int, int, *tuple[int, int, int]] = (0, 1, 2, 3, 4)
# v: tuple[int, int, Unpack[tuple[int, int, int]]] = (0, 1, 2, 3, 4)
# v: Hashable = (0, 1, 2, 3, 4)
# v: Sequence[int] = (0, 1, 2, 3, 4)
# v: Reversible[int] = (0, 1, 2, 3, 4)
# v: Collection[int] = (0, 1, 2, 3, 4)
# v: Sized = (0, 1, 2, 3, 4)
# v: Container[int] = (0, 1, 2, 3, 4)
# v: Iterable[int] = (0, 1, 2, 3, 4)
print(v)
# No error
print(v[0], v[1:3])
# Hashable, Reversible, Collection, Sized, Container and Iterable get error.
print(*v)
# Hashable, Sized and Container get error.
print(v+v)
# Hashable, Sequence, Reversible, Collection,
# Sized, Container and Iterable get error.
<set>:
from collections.abc import (
MutableSet, Set, Collection, Sized, Container, Iterable
)
v: set[int] = {0, 1, 2, 3, 4}
# v: MutableSet[int] = {0, 1, 2, 3, 4}
# v: Set[int] = {0, 1, 2, 3, 4}
# v: Collection[int] = {0, 1, 2, 3, 4}
# v: Sized = {0, 1, 2, 3, 4}
# v: Container[int] = {0, 1, 2, 3, 4}
# v: Iterable[int] = {0, 1, 2, 3, 4}
print(v)
# No error
print(*v)
# Sized and Container get error.
print(v | v)
# Collection, Sized, Container and Iterable get error.
<frozenset>:
from collections.abc import (
Set, Hashable, Collection, Sized, Container, Iterable
)
v: frozenset[int] = frozenset([0, 1, 2, 3, 4])
# v: Set[int] = frozenset([0, 1, 2, 3, 4])
# v: Hashable = frozenset([0, 1, 2, 3, 4])
# v: Collection[int] = frozenset([0, 1, 2, 3, 4])
# v: Sized = frozenset([0, 1, 2, 3, 4])
# v: Container[int] = frozenset([0, 1, 2, 3, 4])
# v: Iterable[int] = frozenset([0, 1, 2, 3, 4])
print(v)
# No error
print(*v)
# Hashable, Sized and Container get error.
print(v | v)
# Hashable, Collection, Sized, Container and Iterable get error.
Top comments (0)