From e8e3ff37b7d4b5e6ae48cfac9c0188c3b6e91b5b Mon Sep 17 00:00:00 2001
From: Tim Jaacks <tim.jaacks@seco.com>
Date: Mon, 31 Jul 2023 14:03:06 +0200
Subject: [PATCH] Refactoring: use single assignment for "if, else"

See for reference:
https://docs.sourcery.ai/Reference/Python/Default-Rules/assign-if-exp/
---
 scripts/.sourcery.yaml            |  1 -
 scripts/changelog_generator.py    |  8 ++------
 scripts/gitlab_backup.py          |  8 ++------
 scripts/lava_api.py               | 12 +++---------
 scripts/lava_create_testreport.py | 12 +++---------
 scripts/markdown_generator.py     |  8 ++------
 scripts/submit_test.py            |  8 ++------
 7 files changed, 14 insertions(+), 43 deletions(-)

diff --git a/scripts/.sourcery.yaml b/scripts/.sourcery.yaml
index 62d412fa..01b1149b 100644
--- a/scripts/.sourcery.yaml
+++ b/scripts/.sourcery.yaml
@@ -16,7 +16,6 @@ rule_settings:
   - replace-interpolation-with-fstring
   - use-fstring-for-concatenation
   - merge-dict-assign
-  - assign-if-exp
   - remove-redundant-if
   - switch
   - low-code-quality
diff --git a/scripts/changelog_generator.py b/scripts/changelog_generator.py
index 83e3880b..5afce6ad 100755
--- a/scripts/changelog_generator.py
+++ b/scripts/changelog_generator.py
@@ -51,9 +51,7 @@ class Project:
         )
 
     def __eq__(self, p):
-        if not p:
-            return False
-        return self.project.id == p.project.id
+        return self.project.id == p.project.id if p else False
 
 
 class Tag:
@@ -167,9 +165,7 @@ class Release:
 
     def description(self):
         m = self.tag.message
-        if not m:
-            return ""
-        return m
+        return m or ""
 
     def __str__(self):
         return self.tag.name
diff --git a/scripts/gitlab_backup.py b/scripts/gitlab_backup.py
index 1e86665b..0a05b48c 100755
--- a/scripts/gitlab_backup.py
+++ b/scripts/gitlab_backup.py
@@ -46,9 +46,7 @@ class Project:
         )
 
     def __eq__(self, p):
-        if not p:
-            return False
-        return self.project.id == p.project.id
+        return self.project.id == p.project.id if p else False
 
 
 class Tag:
@@ -162,9 +160,7 @@ class Release:
 
     def description(self):
         m = self.tag.message
-        if not m:
-            return ""
-        return m
+        return m or ""
 
     def __str__(self):
         return self.tag.name
diff --git a/scripts/lava_api.py b/scripts/lava_api.py
index e76c0062..e66f0d27 100755
--- a/scripts/lava_api.py
+++ b/scripts/lava_api.py
@@ -250,21 +250,15 @@ class LavaDevice:
 
     @property
     def serial(self):
-        if self._serial is None:
-            return ""
-        return self._serial.name
+        return self._serial.name if self._serial is not None else ""
 
     @property
     def hw_revision(self):
-        if self._hw_revision is None:
-            return ""
-        return self._hw_revision.name
+        return self._hw_revision.name if self._hw_revision is not None else ""
 
     @property
     def article_no(self):
-        if self._article_no is None:
-            return ""
-        return self._article_no.name
+        return self._article_no.name if self._article_no is not None else ""
 
     def __lt__(self, other):
         return self.name < other.name
diff --git a/scripts/lava_create_testreport.py b/scripts/lava_create_testreport.py
index d03f56f7..adaeb857 100755
--- a/scripts/lava_create_testreport.py
+++ b/scripts/lava_create_testreport.py
@@ -36,16 +36,12 @@ class TestCollection:
             return ""
         tests = self.tests[machine]
         out = ["{:.2f}".format(float(t.measurement)) for t in tests]
-        if not out:
-            return None
-        return "/".join(out)
+        return "/".join(out) if out else None
 
     @property
     def name(self):
         t = self.get_test()
-        if t is None:
-            return self.test_case_id
-        return t.name
+        return t.name if t is not None else self.test_case_id
 
     # Sort alphabetically
     def __lt__(self, other):
@@ -55,9 +51,7 @@ class TestCollection:
         keys = list(self.tests.keys())
         if not keys:
             return None
-        if len(self.tests[keys[index]]) < 1:
-            return None
-        return self.tests[keys[index]][0]
+        return None if len(self.tests[keys[index]]) < 1 else self.tests[keys[index]][0]
 
     def get_result(self, machine):
         if machine not in self.tests.keys():
diff --git a/scripts/markdown_generator.py b/scripts/markdown_generator.py
index 9d41771f..4a9b8f94 100755
--- a/scripts/markdown_generator.py
+++ b/scripts/markdown_generator.py
@@ -138,14 +138,10 @@ class MarkDownTable:
         return "MarkDownTable (" + self.length + " rows)"
 
     def get_alignment(self, col):
-        if col < len(self.alignment):
-            return self.alignment[col]
-        return self.alignment[-1]
+        return self.alignment[col] if col < len(self.alignment) else self.alignment[-1]
 
     def get_column_width(self, col):
-        if col < len(self.column_width):
-            return self.column_width[col] + 2
-        return 60
+        return self.column_width[col] + 2 if col < len(self.column_width) else 60
 
     def get_column_count(self):
         max_cols = 0
diff --git a/scripts/submit_test.py b/scripts/submit_test.py
index 9e3a5046..f5ea1fc9 100755
--- a/scripts/submit_test.py
+++ b/scripts/submit_test.py
@@ -23,12 +23,8 @@ def call(cmd, stdout=None):
         else:
             result = subprocess.run(cmd, stdout=stdout, check=True)
     except subprocess.CalledProcessError as e:
-        out = ""
-        err = ""
-        if e.stdout is not None:
-            out = e.stdout.decode()
-        if e.stderr is not None:
-            err = e.stderr.decode()
+        out = e.stdout.decode() if e.stdout is not None else ""
+        err = e.stderr.decode() if e.stderr is not None else ""
         logging.error("Command failed %s: %s %s", cmd, out, err)
         exit(1)
     if result is not None and result.stdout is not None:
-- 
GitLab