From 564a09ea9c9f99fdb57cdf44100673710dcfb280 Mon Sep 17 00:00:00 2001 From: Ruslan Ilyasovich Gilfanov Date: Mon, 30 Sep 2024 15:57:35 +0500 Subject: [PATCH 1/3] gh-124748: Fix handling kwargs in WeakKeyDictionary.update() Co-authored-by: sobolevn Co-authored-by: Tomas R --- Lib/test/test_weakref.py | 5 +++++ Lib/weakref.py | 6 ++++-- .../Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst diff --git a/Lib/test/test_weakref.py b/Lib/test/test_weakref.py index 023df68fca7356..49ae4baa511147 100644 --- a/Lib/test/test_weakref.py +++ b/Lib/test/test_weakref.py @@ -1792,6 +1792,11 @@ def test_weak_valued_union_operators(self): def test_weak_keyed_dict_update(self): self.check_update(weakref.WeakKeyDictionary, {C(): 1, C(): 2, C(): 3}) + d = weakref.WeakKeyDictionary() + msg = ("Keyword arguments are not supported: " + "cannot create weak reference to 'str' object") + with self.assertRaisesRegex(TypeError, msg): + d.update(k='v') def test_weak_keyed_delitem(self): d = weakref.WeakKeyDictionary() diff --git a/Lib/weakref.py b/Lib/weakref.py index 25b70927e29c31..8f90c1777d196a 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -508,14 +508,16 @@ def setdefault(self, key, default=None): return self.data.setdefault(ref(key, self._remove),default) def update(self, dict=None, /, **kwargs): + if kwargs: + msg = ("Keyword arguments are not supported: " + "cannot create weak reference to 'str' object") + raise TypeError(msg) d = self.data if dict is not None: if not hasattr(dict, "items"): dict = type({})(dict) for key, value in dict.items(): d[ref(key, self._remove)] = value - if len(kwargs): - self.update(kwargs) def __ior__(self, other): self.update(other) diff --git a/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst b/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst new file mode 100644 index 00000000000000..2d2fcf91d6ea6e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst @@ -0,0 +1,4 @@ +Impove :exc:`TypeError` error message +when :meth:`!weakref.WeakKeyDictionary.update` + +is used with keyword-only parameters. From a66c61b1a97d6c194caec812574b6c21c05f8b09 Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Wed, 18 Feb 2026 18:16:21 +0530 Subject: [PATCH 2/3] Update Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst --- .../next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst b/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst index 2d2fcf91d6ea6e..52be9f48a44eab 100644 --- a/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst +++ b/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst @@ -1,4 +1,4 @@ -Impove :exc:`TypeError` error message +Improve :exc:`TypeError` error message when :meth:`!weakref.WeakKeyDictionary.update` is used with keyword-only parameters. From a39fb51f23fcd26a9fdd8ac4c7e6edd46c2cf78e Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Wed, 18 Feb 2026 18:17:15 +0530 Subject: [PATCH 3/3] Apply suggestion from @kumaraditya303 --- .../Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst b/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst index 52be9f48a44eab..5067db357fc577 100644 --- a/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst +++ b/Misc/NEWS.d/next/Library/2024-09-30-15-31-59.gh-issue-124748.KYzYFp.rst @@ -1,4 +1,2 @@ -Improve :exc:`TypeError` error message -when :meth:`!weakref.WeakKeyDictionary.update` - +Improve :exc:`TypeError` error message when :meth:`!weakref.WeakKeyDictionary.update` is used with keyword-only parameters.