DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Type hints in Python (4)

Buy Me a Coffee

*Memo:

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, Sized and MappingView:
    • Each has no arguments.
    • Hashable cannot be used for an iterator even it's immutable according to the issue.
    • MappingView accepts itself, dict_keys(KeysView), dict_values(ValuesView) and dict_items(ItemsView).
  • For list, set, frozenset, Container, Iterable, Iterator, Reversible, Generator, Collection, Sequence, MutableSequence, Set, MutableSet, KeysView and ValuesView:
    • The 1st argument is type(Required:-Type:Type):
      • It's a type.
      • Don't use type=.
    • Use Iterator instead of iter because type checkers don't support iter.
    • Use KeysView and ValuesView instead of dict_keys and dict_values respectively because type checkers don't support dict_keys and dict_values in _collections_abc.
  • For dict, Mapping, MutableMapping and ItemsView:
    • 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 ItemsView instead of dict_items because type checkers don't support dict_items in _collections_abc.
  • For tuple:
    • The 1st arguments are *types(Required:-Type:Type):
      • It's one or more types.
      • Only in the 2nd argument, ... (but not Ellipsis) can be used to accept the zero or more arguments of a type.
      • Don't use any keywords like *types=, types=, etc.
    • tuple is special so each type must be specified for each value.
    • An unpacked tuple is possible.
    • The doc explains the type hint with a tuple.

<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
Enter fullscreen mode Exit fullscreen mode

<tuple>:

from collections.abc import (
    Hashable, Sequence, Reversible, Collection, Sized, Container, Iterable
)

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, *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.
Enter fullscreen mode Exit fullscreen mode

<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.
Enter fullscreen mode Exit fullscreen mode

<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.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)