From 1faadc520639873be6d43bb17c816b3c433ebf8d Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Fri, 4 Oct 2019 09:45:24 +0100 Subject: [PATCH 1/3] Add example of non-mutable mapping to cheat sheet --- docs/source/cheat_sheet_py3.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/source/cheat_sheet_py3.rst b/docs/source/cheat_sheet_py3.rst index 377112ad31d4..e297cf6abf9c 100644 --- a/docs/source/cheat_sheet_py3.rst +++ b/docs/source/cheat_sheet_py3.rst @@ -201,6 +201,7 @@ that are common in idiomatic Python are standardized. .. code-block:: python + from types import MappingProxyType from typing import Mapping, MutableMapping, Sequence, Iterable, List, Set # Use Iterable for generic iterables (anything usable in "for"), @@ -216,13 +217,15 @@ that are common in idiomatic Python are standardized. def f(my_dict: Mapping[int, str]) -> List[int]: return list(my_dict.keys()) - f({3: 'yes', 4: 'no'}) + f({3: 'yes', 4: 'no'}) # Ok + f(MappingProxyType({3: 'yes', 4: 'no'})) # Ok def f(my_mapping: MutableMapping[int, str]) -> Set[str]: my_mapping[5] = 'maybe' return set(my_mapping.values()) - f({3: 'yes', 4: 'no'}) + f({3: 'yes', 4: 'no'}) # Ok + f(MappingProxyType({3: 'yes', 4: 'no'})) # This will fail (as it's not mutable) Classes From 5fad6e1804c1102c33d32d3ae303921793b25936 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Mon, 7 Oct 2019 09:04:44 +0100 Subject: [PATCH 2/3] Rewrite example --- docs/source/cheat_sheet_py3.rst | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/docs/source/cheat_sheet_py3.rst b/docs/source/cheat_sheet_py3.rst index 0b867c802835..4c990559f217 100644 --- a/docs/source/cheat_sheet_py3.rst +++ b/docs/source/cheat_sheet_py3.rst @@ -199,7 +199,6 @@ that are common in idiomatic Python are standardized. .. code-block:: python - from types import MappingProxyType from typing import Mapping, MutableMapping, Sequence, Iterable, List, Set # Use Iterable for generic iterables (anything usable in "for"), @@ -213,18 +212,16 @@ that are common in idiomatic Python are standardized. # Mapping describes a dict-like object (with "__getitem__") that we won't # mutate, and MutableMapping one (with "__setitem__") that we might def f(my_dict: Mapping[int, str]) -> List[int]: + my_dict.update({5: 'maybe'}) # if we try this, mypy will throw an error... return list(my_dict.keys()) - f({3: 'yes', 4: 'no'}) # Ok - f(MappingProxyType({3: 'yes', 4: 'no'})) # Ok + f({3: 'yes', 4: 'no'}) def f(my_mapping: MutableMapping[int, str]) -> Set[str]: - my_mapping[5] = 'maybe' + my_dict.update({5: 'maybe'}) # ...but mypy is OK with this. return set(my_mapping.values()) - f({3: 'yes', 4: 'no'}) # Ok - f(MappingProxyType({3: 'yes', 4: 'no'})) # This will fail (as it's not mutable) - + f({3: 'yes', 4: 'no'}) Classes ******* From b769458e61fc49e8c8a52e20f2de920e51cda1c4 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Mon, 7 Oct 2019 09:07:32 +0100 Subject: [PATCH 3/3] revert to original dict assignment --- docs/source/cheat_sheet_py3.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/source/cheat_sheet_py3.rst b/docs/source/cheat_sheet_py3.rst index 4c990559f217..47b36e24d351 100644 --- a/docs/source/cheat_sheet_py3.rst +++ b/docs/source/cheat_sheet_py3.rst @@ -212,17 +212,18 @@ that are common in idiomatic Python are standardized. # Mapping describes a dict-like object (with "__getitem__") that we won't # mutate, and MutableMapping one (with "__setitem__") that we might def f(my_dict: Mapping[int, str]) -> List[int]: - my_dict.update({5: 'maybe'}) # if we try this, mypy will throw an error... + my_mapping[5] = 'maybe' # if we try this, mypy will throw an error... return list(my_dict.keys()) f({3: 'yes', 4: 'no'}) def f(my_mapping: MutableMapping[int, str]) -> Set[str]: - my_dict.update({5: 'maybe'}) # ...but mypy is OK with this. + my_mapping[5] = 'maybe' # ...but mypy is OK with this. return set(my_mapping.values()) f({3: 'yes', 4: 'no'}) + Classes *******