bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Collin Funk <collin.funk1@gmail.com>
To: bug-gnulib@gnu.org
Subject: gnulib-tool.py: Don't use mutable default arguments.
Date: Sat, 13 Apr 2024 19:03:17 -0700	[thread overview]
Message-ID: <0ddf04f4-e11a-4715-bed3-b4d9201f464a@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 803 bytes --]

This patch fixes an interesting warning given by PyCharm.

With this line:

    def __init__(self, config: GLConfig, transformers: dict[str, tuple[re.Pattern, str] | None] = dict()) -> None:

Under '= dict()', I see a warning about mutable default arguments.
Here is a test program to demonstrate:

--------------------------------
#!/usr/bin/env python3

def function(arg1, arg2 = dict()):
    arg2[arg1] = 0
    print(arg2)

function('one')
function('two')
function('three')
--------------------------------

When executing the following is printed:

     {'one': 0}
     {'one': 0, 'two': 0}
     {'one': 0, 'two': 0, 'three': 0}

To avoid this behavior we can set the default value of 'transformers'
to None. Then in the body of __init__() we can set it to an empty
dictionary if it is None.

Collin

[-- Attachment #2: 0001-gnulib-tool.py-Don-t-use-mutable-default-arguments.patch --]
[-- Type: text/x-patch, Size: 2334 bytes --]

From 07dcab221be80ca4d8dae4bfafc0dd80bff85c69 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.funk1@gmail.com>
Date: Sat, 13 Apr 2024 18:51:06 -0700
Subject: [PATCH] gnulib-tool.py: Don't use mutable default arguments.

* pygnulib/GLFileSystem.py (GLFileAssistant.__init__): Set the default
argument for 'transformers' to None. If it is None then set it to an
empty dictionary in the body.
---
 ChangeLog                | 7 +++++++
 pygnulib/GLFileSystem.py | 6 ++++--
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f57cb59b81..9a1618e833 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-04-13  Collin Funk  <collin.funk1@gmail.com>
+
+	gnulib-tool.py: Don't use mutable default arguments.
+	* pygnulib/GLFileSystem.py (GLFileAssistant.__init__): Set the default
+	argument for 'transformers' to None. If it is None then set it to an
+	empty dictionary in the body.
+
 2024-04-13  Bruno Haible  <bruno@clisp.org>
 
 	bootstrap: Implement phase 1 as documented in the --help output.
diff --git a/pygnulib/GLFileSystem.py b/pygnulib/GLFileSystem.py
index ee78181d82..4bd8b1f386 100644
--- a/pygnulib/GLFileSystem.py
+++ b/pygnulib/GLFileSystem.py
@@ -154,7 +154,7 @@ def shouldLink(self, original: str, lookedup: str) -> bool:
 class GLFileAssistant:
     '''GLFileAssistant is used to help with file processing.'''
 
-    def __init__(self, config: GLConfig, transformers: dict[str, tuple[re.Pattern, str] | None] = dict()) -> None:
+    def __init__(self, config: GLConfig, transformers: dict[str, tuple[re.Pattern, str] | None] | None = None) -> None:
         '''Create GLFileAssistant instance.
 
         config stores information shared between classes.
@@ -164,7 +164,9 @@ def __init__(self, config: GLConfig, transformers: dict[str, tuple[re.Pattern, s
         if type(config) is not GLConfig:
             raise TypeError('config must be a GLConfig, not %s'
                             % type(config).__name__)
-        if type(transformers) is not dict:
+        if transformers == None:
+            transformers = dict()
+        elif type(transformers) is not dict:
             raise TypeError('transformers must be a dict, not %s'
                             % type(transformers).__name__)
         for key in ['lib', 'aux', 'main', 'tests']:
-- 
2.44.0


             reply	other threads:[~2024-04-14  2:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-14  2:03 Collin Funk [this message]
2024-04-14  6:13 ` gnulib-tool.py: Don't use mutable default arguments Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0ddf04f4-e11a-4715-bed3-b4d9201f464a@gmail.com \
    --to=collin.funk1@gmail.com \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).