Skip to content

Commit

Permalink
Improve add_action implementation
Browse files Browse the repository at this point in the history
Simplify code, reduce cyclomatic complexity from level C to A
  • Loading branch information
ramses44 committed Oct 13, 2023
1 parent 1faf764 commit 2dd2013
Showing 1 changed file with 12 additions and 47 deletions.
59 changes: 12 additions & 47 deletions qtpy/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)


Expand Down

0 comments on commit 2dd2013

Please sign in to comment.