Skip to content

Commit 6ab5118

Browse files
Mention typing.Never used to type hint empty containers
1 parent c461aa9 commit 6ab5118

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Doc/library/typing.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,16 @@ using ``[]``.
973973
case _:
974974
never_call_me(arg) # OK, arg is of type Never (or NoReturn)
975975

976+
Or to define an empty container that must remain empty::
977+
978+
from typing import Never
979+
980+
empty_list: list[Never] = []
981+
empty_list.append(1) # type checker error
982+
983+
empty_dict: dict[Never, Never] = {}
984+
empty_dict['key'] = 'value' # type checker error
985+
976986
:data:`!Never` and :data:`!NoReturn` have the same meaning in the type system
977987
and static type checkers treat both equivalently.
978988

0 commit comments

Comments
 (0)