From 0b89ddcccb69526ace50cc97200bf8b464922f2a Mon Sep 17 00:00:00 2001 From: deepsourcebot Date: Mon, 12 Jun 2023 10:29:42 +0000 Subject: [PATCH 1/2] =?UTF-8?q?Fix=20dangerous=20default=20argument=20Do?= =?UTF-8?q?=20not=20use=20a=20mutable=20like=20`list`=20or=20`dictionary`?= =?UTF-8?q?=20as=20a=20default=20value=20to=20an=20argument.=20Python?= =?UTF-8?q?=E2=80=99s=20default=20arguments=20are=20evaluated=20once=20whe?= =?UTF-8?q?n=20the=20function=20is=20defined.=20Using=20a=20mutable=20defa?= =?UTF-8?q?ult=20argument=20and=20mutating=20it=20will=20mutate=20that=20o?= =?UTF-8?q?bject=20for=20all=20future=20calls=20to=20the=20function=20as?= =?UTF-8?q?=20well.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo_code.py | 12 +++++++++--- foo.py | 16 ++++++++++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/demo_code.py b/demo_code.py index 275fda452..56751279e 100644 --- a/demo_code.py +++ b/demo_code.py @@ -40,8 +40,10 @@ def limits(self): a = 2 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] collections assert all([isinstance(i, int) for i in min_max]) return random.randint(*min_max) @@ -58,7 +60,9 @@ def __getattr__(self, key): return key -def main(options: dict = {}) -> str: +def main(options: dict = None) -> str: + if options is None: + options = {} if "run" in options: value = options["run"] else: @@ -75,7 +79,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/foo.py b/foo.py index 40df53dbf..c97e436f4 100644 --- a/foo.py +++ b/foo.py @@ -9,14 +9,20 @@ breakpoint() print(b, nums) -def baz(a=[]): +def baz(a=None): + if a is None: + a = [] return 0 -def aaa(a=[]): +def aaa(a=None): + if a is None: + a = [] return 1 -def foo(b=[]): +def foo(b=None): + if b is None: + b = [] return 1 def bar(a): @@ -27,7 +33,9 @@ def bar(a): with open(filename, 'w') as f: pass -def boom(a=[]): +def boom(a=None): + if a is None: + a = [] breakpoint() filename = os.tmpnam() breakpoint() From 4e2b756e9165b2edd8f549db1d4204eec5e3e943 Mon Sep 17 00:00:00 2001 From: deepsourcebot Date: Mon, 12 Jun 2023 10:29:51 +0000 Subject: [PATCH 2/2] format code with Black and ISort This commit fixes the style issues introduced in 0b89ddc according to the output from Black and ISort. Details: --- demo_code.py | 10 ++++----- foo.py | 60 +++++++++++++++++++++++++++++----------------------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/demo_code.py b/demo_code.py index 56751279e..76ac5d837 100644 --- a/demo_code.py +++ b/demo_code.py @@ -1,10 +1,10 @@ -import random -import sys as sys +import collections import os -import subprocess -import ssl import pprint -import collections +import random +import ssl +import subprocess +import sys as sys import this diff --git a/foo.py b/foo.py index c97e436f4..6df8a99b7 100644 --- a/foo.py +++ b/foo.py @@ -1,6 +1,7 @@ -import os import logging -nums = [ i for i in range(10)] +import os + +nums = [i for i in range(10)] a = 1 b = 2 @@ -9,43 +10,50 @@ breakpoint() print(b, nums) + def baz(a=None): - if a is None: - a = [] - return 0 + if a is None: + a = [] + return 0 + def aaa(a=None): - if a is None: - a = [] - return 1 + if a is None: + a = [] + return 1 def foo(b=None): - if b is None: - b = [] - return 1 + if b is None: + b = [] + return 1 + def bar(a): - return 1 + return 1 + import os + filename = os.tmpnam() -with open(filename, 'w') as f: - pass +with open(filename, "w") as f: + pass + def boom(a=None): - if a is None: - a = [] - breakpoint() - filename = os.tmpnam() - breakpoint() - return filename + if a is None: + a = [] + breakpoint() + filename = os.tmpnam() + breakpoint() + return filename + def another_test_method(): - f = open("/tmp/.deepsource.toml", "r") - f.write("config file.") - f.close() - print('abc') - -filename = os.tmpnam() + f = open("/tmp/.deepsource.toml", "r") + f.write("config file.") + f.close() + print("abc") + +filename = os.tmpnam()