From 83464c7de5e10e10741caeb8d878337a9dd80b8a Mon Sep 17 00:00:00 2001 From: a-eT <32207200078@e.gzhu.edu.cn> Date: Sat, 29 Nov 2025 22:18:20 +0800 Subject: [PATCH 1/8] gh-142085: Fix ChainMap.__ior__ to return NotImplemented for unsupported types --- Lib/collections/__init__.py | 5 ++++- Lib/test/test_collections.py | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 55ffc36ea5b0c7..6bea12bb6bde65 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1151,7 +1151,10 @@ def clear(self): self.maps[0].clear() def __ior__(self, other): - self.maps[0].update(other) + try: + self.maps[0].update(other) + except TypeError: + return NotImplemented return self def __or__(self, other): diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 22595239252814..50cbcdbddf62d9 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -308,7 +308,20 @@ def __ror__(self, other): tmp = ChainMap() | SubclassRor() self.assertIs(type(tmp), SubclassRor) self.assertIs(type(tmp.maps[0]), dict) - + + def test_ior_fallback(self): + # Verify that __ior__ returns NotImplemented for unrecognized types, + # allowing the other operand to handle the operation via __ror__. + class Rescuer: + def __ror__(self, other): + return "fallback" + + cm = ChainMap() + # This should not raise TypeError. + # Since ChainMap.__ior__ returns NotImplemented, Python calls Rescuer.__ror__, + # and the result ("fallback") is assigned back to 'cm'. + cm |= Rescuer() + self.assertEqual(cm, "fallback") ################################################################################ ### Named Tuples From b9881751825dca075a652f15291796b06b2dd938 Mon Sep 17 00:00:00 2001 From: a-eT <32207200078@e.gzhu.edu.cn> Date: Sat, 29 Nov 2025 22:27:13 +0800 Subject: [PATCH 2/8] Add news entry for gh-142085 --- Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst diff --git a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst new file mode 100644 index 00000000000000..37d1012071fb56 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst @@ -0,0 +1 @@ +Fix :meth:~collections.ChainMap.__ior__ to return :const:NotImplemented instead of raising :exc:TypeError for unsupported types. From f2826a9c2932b3f3a55b80c03babcc6988bc03af Mon Sep 17 00:00:00 2001 From: a-eT <32207200078@e.gzhu.edu.cn> Date: Sat, 29 Nov 2025 22:40:28 +0800 Subject: [PATCH 3/8] Fix news entry format and encoding --- .../next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst | 1 + Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst delete mode 100644 Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst diff --git a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst new file mode 100644 index 00000000000000..3e32fec435d047 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst @@ -0,0 +1 @@ +Fix :meth:`~collections.ChainMap.__ior__` to return :const:`NotImplemented` instead of raising :exc:`TypeError` for unsupported types. diff --git a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst deleted file mode 100644 index 37d1012071fb56..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.rst +++ /dev/null @@ -1 +0,0 @@ -Fix :meth:~collections.ChainMap.__ior__ to return :const:NotImplemented instead of raising :exc:TypeError for unsupported types. From eca91f07c81819d9104a538a1e8ec4bbc3543176 Mon Sep 17 00:00:00 2001 From: a-eT <32207200078@e.gzhu.edu.cn> Date: Sat, 29 Nov 2025 22:50:30 +0800 Subject: [PATCH 4/8] Fix trailing whitespace manually --- Lib/test/test_collections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 50cbcdbddf62d9..52616116c13974 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -315,7 +315,7 @@ def test_ior_fallback(self): class Rescuer: def __ror__(self, other): return "fallback" - + cm = ChainMap() # This should not raise TypeError. # Since ChainMap.__ior__ returns NotImplemented, Python calls Rescuer.__ror__, From 35093d6544d8bd004a43325808629e09f879c861 Mon Sep 17 00:00:00 2001 From: a-eT <32207200078@e.gzhu.edu.cn> Date: Sat, 29 Nov 2025 22:53:56 +0800 Subject: [PATCH 5/8] Fix trailing whitespace final --- Lib/test/test_collections.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 52616116c13974..6b6197d4d85ae5 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -308,7 +308,7 @@ def __ror__(self, other): tmp = ChainMap() | SubclassRor() self.assertIs(type(tmp), SubclassRor) self.assertIs(type(tmp.maps[0]), dict) - + def test_ior_fallback(self): # Verify that __ior__ returns NotImplemented for unrecognized types, # allowing the other operand to handle the operation via __ror__. From 465acdc0387369a11716b1944799b731b85824d5 Mon Sep 17 00:00:00 2001 From: a-eT <32207200078@e.gzhu.edu.cn> Date: Sat, 29 Nov 2025 23:07:20 +0800 Subject: [PATCH 6/8] Fix sphinx warning in news entry --- .../next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst index 3e32fec435d047..063c1dd23e48ae 100644 --- a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst +++ b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst @@ -1 +1 @@ -Fix :meth:`~collections.ChainMap.__ior__` to return :const:`NotImplemented` instead of raising :exc:`TypeError` for unsupported types. +Fix ``ChainMap.__ior__`` to return ``NotImplemented`` instead of raising ``TypeError`` for unsupported types. From 78bd649df53b6fd68ce4526b782823882aacaf38 Mon Sep 17 00:00:00 2001 From: Fatin <32006697+ChuheLin@users.noreply.github.com> Date: Sat, 29 Nov 2025 23:23:15 +0800 Subject: [PATCH 7/8] Update Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst index 063c1dd23e48ae..e0a268e5f79144 100644 --- a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst +++ b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst @@ -1 +1,2 @@ -Fix ``ChainMap.__ior__`` to return ``NotImplemented`` instead of raising ``TypeError`` for unsupported types. +Fix :class:`ChainMap.__ior__ ` to return :data:`NotImplemented` +instead of raising :exc:`TypeError`` for unsupported types. From d3acacf46a9c593270d276163b6387d6b88c28d0 Mon Sep 17 00:00:00 2001 From: Fatin <32006697+ChuheLin@users.noreply.github.com> Date: Sat, 29 Nov 2025 23:30:56 +0800 Subject: [PATCH 8/8] Update Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst index e0a268e5f79144..d931ed6ec51a5b 100644 --- a/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst +++ b/Misc/NEWS.d/next/Library/2025-11-29-22-23-00.gh-issue-142085.7u8i9o.rst @@ -1,2 +1,2 @@ Fix :class:`ChainMap.__ior__ ` to return :data:`NotImplemented` -instead of raising :exc:`TypeError`` for unsupported types. +instead of raising :exc:`TypeError` for unsupported types.