Skip to content

Commit 2dd2013

Browse files
committed
Improve add_action implementation
Simplify code, reduce cyclomatic complexity from level C to A
1 parent 1faf764 commit 2dd2013

File tree

1 file changed

+12
-47
lines changed

1 file changed

+12
-47
lines changed

qtpy/_utils.py

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ def add_action(self, *args, old_add_action):
8181
shortcut: QKeySequence | QKeySequence.StandardKey | str | int
8282
receiver: QObject
8383
member: bytes
84+
85+
# if args and isinstance(args[0], QIcon):
86+
if any(map(lambda arg: isinstance(arg, QIcon), args[:1])): # Better to use previous line instead of this
87+
icon, *args = args
88+
else:
89+
icon = QIcon()
90+
8491
if all(
8592
isinstance(arg, t)
8693
for arg, t in zip(
@@ -93,54 +100,12 @@ def add_action(self, *args, old_add_action):
93100
],
94101
)
95102
):
96-
if len(args) == 2:
97-
text, shortcut = args
98-
action = old_add_action(self, text)
103+
if len(args) >= 2:
104+
text, shortcut, *args = args
105+
action = old_add_action(self, icon, text, *args)
99106
action.setShortcut(shortcut)
100-
elif len(args) == 3:
101-
text, shortcut, receiver = args
102-
action = old_add_action(self, text, receiver)
103-
action.setShortcut(shortcut)
104-
elif len(args) == 4:
105-
text, shortcut, receiver, member = args
106-
action = old_add_action(self, text, receiver, member, shortcut)
107-
else:
108-
return old_add_action(self, *args)
109-
return action
110-
if all(
111-
isinstance(arg, t)
112-
for arg, t in zip(
113-
args,
114-
[
115-
QIcon,
116-
str,
117-
(QKeySequence, QKeySequence.StandardKey, str, int),
118-
QObject,
119-
bytes,
120-
],
121-
)
122-
):
123-
if len(args) == 3:
124-
icon, text, shortcut = args
125-
action = old_add_action(self, icon, text)
126-
action.setShortcut(QKeySequence(shortcut))
127-
elif len(args) == 4:
128-
icon, text, shortcut, receiver = args
129-
action = old_add_action(self, icon, text, receiver)
130-
action.setShortcut(QKeySequence(shortcut))
131-
elif len(args) == 5:
132-
icon, text, shortcut, receiver, member = args
133-
action = old_add_action(
134-
self,
135-
icon,
136-
text,
137-
receiver,
138-
member,
139-
QKeySequence(shortcut),
140-
)
141-
else:
142-
return old_add_action(self, *args)
143-
return action
107+
return action
108+
144109
return old_add_action(self, *args)
145110

146111

0 commit comments

Comments
 (0)