From 1997fa99aec5e7491ad8f7ea62b8331a62da4004 Mon Sep 17 00:00:00 2001 From: "deepsource-dev-autofix[bot]" <61578317+deepsource-dev-autofix[bot]@users.noreply.github.com> Date: Tue, 27 May 2025 21:29:45 +0000 Subject: [PATCH] refactor: replace dangerous mutable default argument **Fixes are generated by AI. Review them carefully before applying to your codebase.** This PR refactors function parameters to avoid mutable default arguments and prevent state persistence across calls. - Dangerous default argument: Functions using mutable defaults such as lists or dictionaries may retain state across invocations, leading to unintended side effects. We replaced the default argument with None and added a conditional inside the function to initialize a new object when the parameter is None. This ensures each function call operates on a fresh instance and avoids persistent state issues. --- demo_code.py | 16 ++++++++++++---- hello.py | 12 +++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/demo_code.py b/demo_code.py index 9fde50644..fa5a2b6a6 100644 --- a/demo_code.py +++ b/demo_code.py @@ -34,13 +34,17 @@ class RandomNumberGenerator: def limits(self): return self.limits - def get_number(self, min_max=[1, 10]): + def get_number(self, min_max=None): """Get a random number between min and max.""" + if min_max is None: + min_max = [1, 10] assert all([isinstance(i, int) for i in min_max]) return random.randint(*min_max) - def get_digits(self, min_max=[1, 10]): + def get_digits(self, min_max=None): """Get a random number between min and max.""" + if min_max is None: + min_max = [1, 10] assert all([isinstance(i, int) for i in min_max]) return random.randint(*min_max) @@ -48,7 +52,9 @@ def sum(self, a, b): return eval("a + b") -def main(options: dict = {}) -> str: +def main(options: dict = None) -> str: + if options is None: + options = {} pdb.set_trace() if "run" in options: value = options["run"] @@ -67,7 +73,9 @@ def main(options: dict = {}) -> str: f.close() -def moon_chooser(moon, moons=["europa", "callisto", "phobos"]): +def moon_chooser(moon, moons=None): + if moons is None: + moons = ["europa", "callisto", "phobos"] if moon is not None: moons.append(moon) diff --git a/hello.py b/hello.py index dc8227b07..d0d399dce 100644 --- a/hello.py +++ b/hello.py @@ -36,13 +36,17 @@ class RandomNumberGenerator: def limits(self): return self.limits - def get_number(self, min_max=[1, 10]): + def get_number(self, min_max=None): """Get a random number between min and max.""" + if min_max is None: + min_max = [1, 10] assert all([isinstance(i, int) for i in min_max]) return random.randint(*min_max) -def main(options: dict = {}) -> str: +def main(options: dict = None) -> str: + if options is None: + options = {} pdb.set_trace() if "run" in options: value = options["run"] @@ -61,7 +65,9 @@ def main(options: dict = {}) -> str: f.close() -def moon_chooser(moon, moons=["europa", "callisto", "phobos"]): +def moon_chooser(moon, moons=None): + if moons is None: + moons = ["europa", "callisto", "phobos"] if moon is not None: moons.append(moon)