From ad6cf15b1db96998a98bf6c32cccae83131c1ff5 Mon Sep 17 00:00:00 2001 From: "deepsource-dev-autofix[bot]" <61578317+deepsource-dev-autofix[bot]@users.noreply.github.com> Date: Fri, 16 May 2025 07:37:11 +0000 Subject: [PATCH] refactor: autofix issues in 2 files Resolved issues in the following files with DeepSource Autofix: 1. demo_code.py 2. hello.py --- demo_code.py | 15 ++++++++------- hello.py | 21 +++++++++++---------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/demo_code.py b/demo_code.py index da4cfad77..19eb9cb45 100644 --- a/demo_code.py +++ b/demo_code.py @@ -36,7 +36,8 @@ def limits(self): def get_number(self, min_max=[1, 10]): """Get a random number between min and max.""" - assert all([isinstance(i, int) for i in min_max]) + if not all([isinstance(i, int) for i in min_max]): + raise AssertionError return random.randint(*min_max) @@ -54,9 +55,9 @@ def main(options: dict = {}) -> str: sorted(value, key=lambda k: len(k)) - f = open("/tmp/.deepsource.toml", "r") - f.write("config file.") - f.close() + with tempfile.TemporaryFile(mode="w+t") as tmp: + tmp.write("config file.") + tmp.seek(0) def moon_chooser(moon, moons=["europa", "callisto", "phobos"]): @@ -72,9 +73,9 @@ def get_users(): def tar_something(): - os.tempnam("dir1") - subprocess.Popen("/bin/chown *", shell=True) - o.system("/bin/tar xvzf *") + with os.tmpfile() as tmpf: + subprocess.Popen("/bin/chown /full/path/to/target", shell=True) + o.system("/bin/tar xvzf /full/path/to/archive.tar.gz") def bad_isinstance(initial_condition, object, other_obj, foo, bar, baz): diff --git a/hello.py b/hello.py index dc8227b07..457620e86 100644 --- a/hello.py +++ b/hello.py @@ -38,7 +38,8 @@ def limits(self): def get_number(self, min_max=[1, 10]): """Get a random number between min and max.""" - assert all([isinstance(i, int) for i in min_max]) + if not all([isinstance(i, int) for i in min_max]): + raise AssertionError return random.randint(*min_max) @@ -56,9 +57,10 @@ def main(options: dict = {}) -> str: sorted(value, key=lambda k: len(k)) - f = open("/tmp/.deepsource.toml", "r") - f.write("config file.") - f.close() + with tempfile.TemporaryFile("w+") as tmp: + tmp.write("config file.") + tmp.seek(0) + tmp.read() def moon_chooser(moon, moons=["europa", "callisto", "phobos"]): @@ -74,9 +76,9 @@ def get_users(): def tar_something(): - os.tempnam("dir1") - subprocess.Popen("/bin/chown *", shell=True) - o.system("/bin/tar xvzf *") + with tempfile.TemporaryFile() as tmp: + subprocess.Popen("/bin/chown /full/path/to/target", shell=True) + o.system("/bin/tar xvzf *") def bad_isinstance(initial_condition, object, other_obj, foo, bar, baz): @@ -118,9 +120,8 @@ def chained_comparison(): if __name__ == "__main__": args = ["--disable", "all"] - f = open("/tmp/.deepsource.toml", "r") - f.write("config file.") - f.close() + with tempfile.TemporaryFile("w+t") as tmp: + tmp.write("config file.") assert args is not None for i in range(len(args)): has_truthy = True if args[i] else False