Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31ec52a

Browse files
scopilevkivskyi
authored andcommitted
bpo-35631: Improve typing docs wrt abstract/concrete collection types (GH-11396)
https://bugs.python.org/issue35631
1 parent 47a2fce commit 31ec52a

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

Doc/library/typing.rst

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ A type alias is defined by assigning the type to the alias. In this example,
5050

5151
Type aliases are useful for simplifying complex type signatures. For example::
5252

53-
from typing import Dict, Tuple, List
53+
from typing import Dict, Tuple, Sequence
5454

5555
ConnectionOptions = Dict[str, str]
5656
Address = Tuple[str, int]
5757
Server = Tuple[Address, ConnectionOptions]
5858

59-
def broadcast_message(message: str, servers: List[Server]) -> None:
59+
def broadcast_message(message: str, servers: Sequence[Server]) -> None:
6060
...
6161

6262
# The static type checker will treat the previous type signature as
6363
# being exactly equivalent to this one.
6464
def broadcast_message(
6565
message: str,
66-
servers: List[Tuple[Tuple[str, int], Dict[str, str]]]) -> None:
66+
servers: Sequence[Tuple[Tuple[str, int], Dict[str, str]]]) -> None:
6767
...
6868

6969
Note that ``None`` as a type hint is a special case and is replaced by
@@ -568,6 +568,10 @@ The module defines the following classes, functions and decorators:
568568
.. class:: Mapping(Sized, Collection[KT], Generic[VT_co])
569569

570570
A generic version of :class:`collections.abc.Mapping`.
571+
This type can be used as follows::
572+
573+
def get_position_in_index(word_list: Mapping[str, int], word: str) -> int:
574+
return word_list[word]
571575

572576
.. class:: MutableMapping(Mapping[KT, VT])
573577

@@ -601,8 +605,8 @@ The module defines the following classes, functions and decorators:
601605

602606
Generic version of :class:`list`.
603607
Useful for annotating return types. To annotate arguments it is preferred
604-
to use abstract collection types such as :class:`Mapping`, :class:`Sequence`,
605-
or :class:`AbstractSet`.
608+
to use an abstract collection type such as :class:`Sequence` or
609+
:class:`Iterable`.
606610

607611
This type may be used as follows::
608612

@@ -617,6 +621,8 @@ The module defines the following classes, functions and decorators:
617621
.. class:: Set(set, MutableSet[T])
618622

619623
A generic version of :class:`builtins.set <set>`.
624+
Useful for annotating return types. To annotate arguments it is preferred
625+
to use an abstract collection type such as :class:`AbstractSet`.
620626

621627
.. class:: FrozenSet(frozenset, AbstractSet[T_co])
622628

@@ -678,10 +684,13 @@ The module defines the following classes, functions and decorators:
678684
.. class:: Dict(dict, MutableMapping[KT, VT])
679685

680686
A generic version of :class:`dict`.
681-
The usage of this type is as follows::
687+
Useful for annotating return types. To annotate arguments it is preferred
688+
to use an abstract collection type such as :class:`Mapping`.
682689

683-
def get_position_in_index(word_list: Dict[str, int], word: str) -> int:
684-
return word_list[word]
690+
This type can be used as follows::
691+
692+
def count_words(text: str) -> Dict[str, int]:
693+
...
685694

686695
.. class:: DefaultDict(collections.defaultdict, MutableMapping[KT, VT])
687696

0 commit comments

Comments
 (0)