From 0f0276d92e3903b57d7e1f9b5422684e57dc2b77 Mon Sep 17 00:00:00 2001 From: Sobhan Mohammadpour Date: Mon, 23 Jun 2025 10:34:32 -0400 Subject: [PATCH 1/6] Use walrus operator --- Lib/copy.py | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/Lib/copy.py b/Lib/copy.py index c64fc0761793f5..fdf1de625bda68 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -129,32 +129,25 @@ def deepcopy(x, memo=None, _nil=[]): copier = _deepcopy_dispatch.get(cls) if copier is not None: y = copier(x, memo) - else: - if issubclass(cls, type): + elif issubclass(cls, type): y = x # atomic copy + elif (copier := getattr(x, "__deepcopy__", None)) is not None: + y = copier(memo) + else: + if (reductor := dispatch_table.get(cls)): + rv = reductor(x) + elif (reductor := getattr(x, "__reduce_ex__", None)) is not None: + rv = reductor(4) + elif (reductor := getattr(x, "__reduce__", None)): + rv = reductor() else: - copier = getattr(x, "__deepcopy__", None) - if copier is not None: - y = copier(memo) - else: - reductor = dispatch_table.get(cls) - if reductor: - rv = reductor(x) - else: - reductor = getattr(x, "__reduce_ex__", None) - if reductor is not None: - rv = reductor(4) - else: - reductor = getattr(x, "__reduce__", None) - if reductor: - rv = reductor() - else: - raise Error( - "un(deep)copyable object of type %s" % cls) - if isinstance(rv, str): - y = x - else: - y = _reconstruct(x, memo, *rv) + raise Error( + "un(deep)copyable object of type %s" % cls) + + if isinstance(rv, str): + y = x + else: + y = _reconstruct(x, memo, *rv) # If is its own copy, don't memoize. if y is not x: From 427cb1fbf43f61edcd06e0d6358059352e96cdd6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 23 Jun 2025 14:44:11 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst b/Misc/NEWS.d/next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst new file mode 100644 index 00000000000000..5b7d7348a3e840 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst @@ -0,0 +1 @@ +Use the walrus operator in deepcopy. From 05aa894b4848db384b205c0a463de661a2c4a5e0 Mon Sep 17 00:00:00 2001 From: Sobhan Mohammadpour Date: Mon, 23 Jun 2025 10:45:37 -0400 Subject: [PATCH 3/6] fix trailing space --- Lib/copy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/copy.py b/Lib/copy.py index fdf1de625bda68..02a7b2c3b8531a 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -143,7 +143,7 @@ def deepcopy(x, memo=None, _nil=[]): else: raise Error( "un(deep)copyable object of type %s" % cls) - + if isinstance(rv, str): y = x else: From c01498cf5e652821f3ae24667881063206110389 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 23 Jun 2025 21:45:13 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst b/Misc/NEWS.d/next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst new file mode 100644 index 00000000000000..18fea4f6f1b73e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst @@ -0,0 +1 @@ +Refactor `copy.deepcopy` with the walrus operator. From cda51d3c3bdef42c21c095c5ab2fac1ce2556337 Mon Sep 17 00:00:00 2001 From: Sobhan Mohammadpour Date: Mon, 23 Jun 2025 17:46:28 -0400 Subject: [PATCH 5/6] remove previos blurb --- .../next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst b/Misc/NEWS.d/next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst deleted file mode 100644 index 5b7d7348a3e840..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-06-23-14-44-10.gh-issue-NNNNNN.wh5f04.rst +++ /dev/null @@ -1 +0,0 @@ -Use the walrus operator in deepcopy. From 6a293e943971b15a54c29eac4fa3827898fcfd04 Mon Sep 17 00:00:00 2001 From: Sobhan Mohammadpour Date: Mon, 23 Jun 2025 17:49:03 -0400 Subject: [PATCH 6/6] fix lint error? --- .../next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst b/Misc/NEWS.d/next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst index 18fea4f6f1b73e..d253561bdba44d 100644 --- a/Misc/NEWS.d/next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst +++ b/Misc/NEWS.d/next/Library/2025-06-23-21-45-12.gh-issue-135865.3C6P4k.rst @@ -1 +1 @@ -Refactor `copy.deepcopy` with the walrus operator. +Refactor ``copy.deepcopy`` with the walrus operator.