From ea0f7ba94eff791610e8ae4cc4323cc731dfe810 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Wed, 13 Apr 2022 01:22:16 -0700 Subject: [PATCH 1/9] Add note about new key not replacing existing key for weakref --- Doc/library/weakref.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 1102c634edaf32..23f4421584287c 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -167,6 +167,15 @@ See :ref:`__slots__ documentation ` for details. application without adding attributes to those objects. This can be especially useful with objects that override attribute accesses. + Note that when a key with equal value to an existing key (but not equal identity) + is inserted into the dictionary, it replaces the value but does not replace the + existing key:: + + d = WeakKeyDictionary() + d[k1] = 1 # d = {k1: 1} + d[k2] = 2 # d = {k1: 2} + del k1 # d = {} + .. versionchanged:: 3.9 Added support for ``|`` and ``|=`` operators, specified in :pep:`584`. From 5961498675afc0f9b3c686e49d1d9855a9a9f886 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Wed, 13 Apr 2022 01:37:42 -0700 Subject: [PATCH 2/9] Add extra sentence to correct misunderstanding of issue --- Doc/library/weakref.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 23f4421584287c..6c92c5315a1ed5 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -169,7 +169,8 @@ See :ref:`__slots__ documentation ` for details. Note that when a key with equal value to an existing key (but not equal identity) is inserted into the dictionary, it replaces the value but does not replace the - existing key:: + existing key. Due to this, when the reference to the original key is deleted, it + also deletes entry in the dictionary:: d = WeakKeyDictionary() d[k1] = 1 # d = {k1: 1} From 4ccddc5e113ae39d115beee5278f5599e49d95c4 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Wed, 13 Apr 2022 01:39:05 -0700 Subject: [PATCH 3/9] Add missing "the" --- Doc/library/weakref.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 6c92c5315a1ed5..1fc05e1a6f178d 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -170,7 +170,7 @@ See :ref:`__slots__ documentation ` for details. Note that when a key with equal value to an existing key (but not equal identity) is inserted into the dictionary, it replaces the value but does not replace the existing key. Due to this, when the reference to the original key is deleted, it - also deletes entry in the dictionary:: + also deletes the entry in the dictionary:: d = WeakKeyDictionary() d[k1] = 1 # d = {k1: 1} From 3c1413da9fa2c32e99b2105380656dab65bd86d2 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Wed, 27 Apr 2022 01:47:28 -0700 Subject: [PATCH 4/9] Add specific example Co-authored-by: Pieter Eendebak --- Doc/library/weakref.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 1fc05e1a6f178d..6250c5afa60d9c 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -172,6 +172,10 @@ See :ref:`__slots__ documentation ` for details. existing key. Due to this, when the reference to the original key is deleted, it also deletes the entry in the dictionary:: + class T(str): + pass + k1 = T('hello') + k2 = T('hello') d = WeakKeyDictionary() d[k1] = 1 # d = {k1: 1} d[k2] = 2 # d = {k1: 2} From c2f649f45b5bff66044f34245c468952789885ff Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Wed, 27 Apr 2022 01:52:25 -0700 Subject: [PATCH 5/9] Make example runnable --- Doc/library/weakref.rst | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 6250c5afa60d9c..b60da963112a73 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -172,14 +172,15 @@ See :ref:`__slots__ documentation ` for details. existing key. Due to this, when the reference to the original key is deleted, it also deletes the entry in the dictionary:: - class T(str): - pass - k1 = T('hello') - k2 = T('hello') - d = WeakKeyDictionary() - d[k1] = 1 # d = {k1: 1} - d[k2] = 2 # d = {k1: 2} - del k1 # d = {} + >>> class T(str): + ... pass + ... + >>> k1, k2 = T(), T() + >>> d = WeakKeyDictionary() + >>> d[k1] = 1 # d = {k1: 1} + >>> d[k2] = 2 # d = {k1: 2} + >>> del k1 # d = {} + .. versionchanged:: 3.9 Added support for ``|`` and ``|=`` operators, specified in :pep:`584`. From c4ac22d19007c1a8621c16dd12f5ac777b0bcdb9 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Wed, 27 Apr 2022 01:53:37 -0700 Subject: [PATCH 6/9] Add weakref. for dict example --- Doc/library/weakref.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index b60da963112a73..e03e4a626a1827 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -176,7 +176,7 @@ See :ref:`__slots__ documentation ` for details. ... pass ... >>> k1, k2 = T(), T() - >>> d = WeakKeyDictionary() + >>> d = weakref.WeakKeyDictionary() >>> d[k1] = 1 # d = {k1: 1} >>> d[k2] = 2 # d = {k1: 2} >>> del k1 # d = {} From 69b323134cab5ab7be1f223a53fccc7a665b9ab3 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Fri, 9 Dec 2022 19:31:31 -0800 Subject: [PATCH 7/9] Remove one newline --- Doc/library/weakref.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index e03e4a626a1827..30a259e42d13f3 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -181,7 +181,6 @@ See :ref:`__slots__ documentation ` for details. >>> d[k2] = 2 # d = {k1: 2} >>> del k1 # d = {} - .. versionchanged:: 3.9 Added support for ``|`` and ``|=`` operators, specified in :pep:`584`. From 9f22bd7fcb1442d9180bafd2d1d40837c89a1813 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Fri, 9 Dec 2022 19:37:32 -0800 Subject: [PATCH 8/9] Add workaround example --- Doc/library/weakref.rst | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 30a259e42d13f3..06a0f935a9c57d 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -172,8 +172,7 @@ See :ref:`__slots__ documentation ` for details. existing key. Due to this, when the reference to the original key is deleted, it also deletes the entry in the dictionary:: - >>> class T(str): - ... pass + >>> class T(str): pass ... >>> k1, k2 = T(), T() >>> d = weakref.WeakKeyDictionary() @@ -181,6 +180,17 @@ See :ref:`__slots__ documentation ` for details. >>> d[k2] = 2 # d = {k1: 2} >>> del k1 # d = {} + A workaround would be to remove the key prior to reassignment:: + + >>> class T(str): pass + ... + >>> k1, k2 = T(), T() + >>> d = weakref.WeakKeyDictionary() + >>> d[k1] = 1 # d = {k1: 1} + >>> d.pop(k1) + >>> d[k2] = 2 # d = {k2: 2} + >>> del k1 # d = {k2: 2} + .. versionchanged:: 3.9 Added support for ``|`` and ``|=`` operators, specified in :pep:`584`. From e17e69f2953ac68706410622fd703bde3c624f80 Mon Sep 17 00:00:00 2001 From: Stanley <46876382+slateny@users.noreply.github.com> Date: Tue, 20 Dec 2022 18:13:47 -0800 Subject: [PATCH 9/9] Use del over pop() Co-authored-by: Jelle Zijlstra --- Doc/library/weakref.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/weakref.rst b/Doc/library/weakref.rst index 06a0f935a9c57d..9da5e01de3c979 100644 --- a/Doc/library/weakref.rst +++ b/Doc/library/weakref.rst @@ -187,7 +187,7 @@ See :ref:`__slots__ documentation ` for details. >>> k1, k2 = T(), T() >>> d = weakref.WeakKeyDictionary() >>> d[k1] = 1 # d = {k1: 1} - >>> d.pop(k1) + >>> del d[k1] >>> d[k2] = 2 # d = {k2: 2} >>> del k1 # d = {k2: 2}