From e06a4abd3791510d50cf3cd2921ab07ed5b09287 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 10 Jan 2024 02:56:59 +0800 Subject: [PATCH] refactor: fix dangerous default argument (#22) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not use a mutable like `list` or `dictionary` as a default value to an argument. Python’s default arguments are evaluated once when the function is defined. Using a mutable default argument and mutating it will mutate that object for all future calls to the function as well. Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> --- src/plugins/nonebot_plugin_addFriend/configUtil.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/plugins/nonebot_plugin_addFriend/configUtil.py b/src/plugins/nonebot_plugin_addFriend/configUtil.py index ed464be..14798d8 100644 --- a/src/plugins/nonebot_plugin_addFriend/configUtil.py +++ b/src/plugins/nonebot_plugin_addFriend/configUtil.py @@ -32,7 +32,9 @@ def check_dict_key_bot_id(config: dict, requestorDict: dict, numDict: dict, bot: # return True -def readData(path, content={}, update=0) -> dict: +def readData(path, content=None, update=0) -> dict: + if content is None: + content = {} if not exists(path): with open(path, "w", encoding="utf-8") as fp: json.dump(content, fp, ensure_ascii=False)