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
From 07dcab221be80ca4d8dae4bfafc0dd80bff85c69 Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@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.fu...@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  <br...@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 via email to