From 2dd20137fddad3d6f03eaca061b3e33269395cb7 Mon Sep 17 00:00:00 2001 From: Roman Sakharov Date: Fri, 13 Oct 2023 18:52:34 +0300 Subject: [PATCH 1/4] Improve add_action implementation Simplify code, reduce cyclomatic complexity from level C to A --- qtpy/_utils.py | 59 ++++++++++---------------------------------------- 1 file changed, 12 insertions(+), 47 deletions(-) diff --git a/qtpy/_utils.py b/qtpy/_utils.py index ec851fab..0ada4c15 100644 --- a/qtpy/_utils.py +++ b/qtpy/_utils.py @@ -81,6 +81,13 @@ def add_action(self, *args, old_add_action): shortcut: QKeySequence | QKeySequence.StandardKey | str | int receiver: QObject member: bytes + + # if args and isinstance(args[0], QIcon): + if any(map(lambda arg: isinstance(arg, QIcon), args[:1])): # Better to use previous line instead of this + icon, *args = args + else: + icon = QIcon() + if all( isinstance(arg, t) for arg, t in zip( @@ -93,54 +100,12 @@ def add_action(self, *args, old_add_action): ], ) ): - if len(args) == 2: - text, shortcut = args - action = old_add_action(self, text) + if len(args) >= 2: + text, shortcut, *args = args + action = old_add_action(self, icon, text, *args) action.setShortcut(shortcut) - elif len(args) == 3: - text, shortcut, receiver = args - action = old_add_action(self, text, receiver) - action.setShortcut(shortcut) - elif len(args) == 4: - text, shortcut, receiver, member = args - action = old_add_action(self, text, receiver, member, shortcut) - else: - return old_add_action(self, *args) - return action - if all( - isinstance(arg, t) - for arg, t in zip( - args, - [ - QIcon, - str, - (QKeySequence, QKeySequence.StandardKey, str, int), - QObject, - bytes, - ], - ) - ): - if len(args) == 3: - icon, text, shortcut = args - action = old_add_action(self, icon, text) - action.setShortcut(QKeySequence(shortcut)) - elif len(args) == 4: - icon, text, shortcut, receiver = args - action = old_add_action(self, icon, text, receiver) - action.setShortcut(QKeySequence(shortcut)) - elif len(args) == 5: - icon, text, shortcut, receiver, member = args - action = old_add_action( - self, - icon, - text, - receiver, - member, - QKeySequence(shortcut), - ) - else: - return old_add_action(self, *args) - return action + return action + return old_add_action(self, *args) From d6fa72d6589d3742388658872619d15b7ebe9b95 Mon Sep 17 00:00:00 2001 From: Roman Sakharov Date: Fri, 13 Oct 2023 19:01:54 +0300 Subject: [PATCH 2/4] Improve add_action implementation Simplify code, reduce cyclomatic complexity from level C to A --- qtpy/_utils.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/qtpy/_utils.py b/qtpy/_utils.py index 0ada4c15..4f258054 100644 --- a/qtpy/_utils.py +++ b/qtpy/_utils.py @@ -79,8 +79,6 @@ def add_action(self, *args, old_add_action): icon: QIcon text: str shortcut: QKeySequence | QKeySequence.StandardKey | str | int - receiver: QObject - member: bytes # if args and isinstance(args[0], QIcon): if any(map(lambda arg: isinstance(arg, QIcon), args[:1])): # Better to use previous line instead of this From cc424aeb03e6c320a461682e4c1cdec66602412d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Oct 2023 18:55:21 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- qtpy/_utils.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/qtpy/_utils.py b/qtpy/_utils.py index 0ada4c15..347eba3b 100644 --- a/qtpy/_utils.py +++ b/qtpy/_utils.py @@ -83,7 +83,9 @@ def add_action(self, *args, old_add_action): member: bytes # if args and isinstance(args[0], QIcon): - if any(map(lambda arg: isinstance(arg, QIcon), args[:1])): # Better to use previous line instead of this + if any( + isinstance(arg, QIcon) for arg in args[:1] + ): # Better to use previous line instead of this icon, *args = args else: icon = QIcon() @@ -99,12 +101,11 @@ def add_action(self, *args, old_add_action): bytes, ], ) - ): - if len(args) >= 2: - text, shortcut, *args = args - action = old_add_action(self, icon, text, *args) - action.setShortcut(shortcut) - return action + ) and len(args) >= 2: + text, shortcut, *args = args + action = old_add_action(self, icon, text, *args) + action.setShortcut(shortcut) + return action return old_add_action(self, *args) From 1a77685ba0e33500319bbc6ff6ecb2e7a489275a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Oct 2023 18:58:06 +0000 Subject: [PATCH 4/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- qtpy/_utils.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/qtpy/_utils.py b/qtpy/_utils.py index 347eba3b..780de22d 100644 --- a/qtpy/_utils.py +++ b/qtpy/_utils.py @@ -90,18 +90,21 @@ def add_action(self, *args, old_add_action): else: icon = QIcon() - if all( - isinstance(arg, t) - for arg, t in zip( - args, - [ - str, - (QKeySequence, QKeySequence.StandardKey, str, int), - QObject, - bytes, - ], + if ( + all( + isinstance(arg, t) + for arg, t in zip( + args, + [ + str, + (QKeySequence, QKeySequence.StandardKey, str, int), + QObject, + bytes, + ], + ) ) - ) and len(args) >= 2: + and len(args) >= 2 + ): text, shortcut, *args = args action = old_add_action(self, icon, text, *args) action.setShortcut(shortcut)