From be15003a7fc0ba50349715f404df986ec0fcd39e Mon Sep 17 00:00:00 2001 From: "deepsource-dev-autofix[bot]" <61578317+deepsource-dev-autofix[bot]@users.noreply.github.com> Date: Wed, 22 Nov 2023 09:43:53 +0000 Subject: [PATCH 1/2] refactor: fix dangerous default argument 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. --- demo_code.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/demo_code.py b/demo_code.py index 35062c075..ab34d3181 100644 --- a/demo_code.py +++ b/demo_code.py @@ -46,13 +46,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"] @@ -71,7 +75,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) From 24324638b15e3a43b19a98952fcfae7c0626166a Mon Sep 17 00:00:00 2001 From: "deepsource-dev-autofix[bot]" <61578317+deepsource-dev-autofix[bot]@users.noreply.github.com> Date: Wed, 22 Nov 2023 09:44:03 +0000 Subject: [PATCH 2/2] style: format code with Autopep8, Black, ISort, Ruff Formatter and Yapf This commit fixes the style issues introduced in be15003 according to the output from Autopep8, Black, ISort, Ruff Formatter and Yapf. Details: https://github.com/srijan-deepsource/demo-python/pull/66 --- demo_code.py | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/demo_code.py b/demo_code.py index ab34d3181..2cf7620de 100644 --- a/demo_code.py +++ b/demo_code.py @@ -1,9 +1,9 @@ -import random -import pdb -import sys as sys +import abc import os +import pdb +import random import subprocess -import abc +import sys as sys # from django.db.models.expressions import RawSQL @@ -27,16 +27,6 @@ def smethod(): def cmethod(cls, something): """class method-to-be""" - - - - - - - - - - cmethod = classmethod(cmethod)