Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve add_action implementation #460

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ramses44
Copy link

Simplify code, reduce cyclomatic complexity from level C to A of add_action()

ramses44 and others added 3 commits October 13, 2023 18:52
Simplify code, reduce cyclomatic complexity from level C to A
Simplify code, reduce cyclomatic complexity from level C to A
@ramses44
Copy link
Author

pre-commit.ci autofix

Copy link
Member

@dalthviz dalthviz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @ramses44 for checking how to improve this! Left a question regarding some comments that where left in the changes. Also, maybe adding more test to this util could be worthy? Besides that this looks good to me 👍 Thank you again for giving this a check!

Also, what do you think about this @ccordoba12 ? and sorry for the out of the blue ping @StSav012 but do you have any comment on these changes? Probably you have a better understanding of the shortcomings that could come when changing this util function :)

Comment on lines +83 to +86
# if args and isinstance(args[0], QIcon):
if any(
isinstance(arg, QIcon) for arg in args[:1]
): # Better to use previous line instead of this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a pending change to be done?

@CAM-Gerlach
Copy link
Member

Should this be a blocker for v2.4.1 given the breakage that's evidently causing per #458 ?

Copy link
Contributor

@StSav012 StSav012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code, as it is, contains at least two logical errors. Moreover, the code and the tests should be improved to accommodate the case of a Qt.Key instance as the shortcut argument.

# if args and isinstance(args[0], QIcon):
if any(
isinstance(arg, QIcon) for arg in args[:1]
): # Better to use previous line instead of this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition means that an instance of QIcon is not the last argument. However, in the next line, you imply that the instance is the first argument. This might work correctly, but only by chance. I'd rather use the commented out line

# if args and isinstance(args[0], QIcon):
here.

)
and len(args) >= 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you lose the case when only an icon and a text are provided. Indeed, from

icon, *args = args
, you get the icon, and the args becomes ('text', ), i.e., len(args) == 1.

return old_add_action(self, *args)
text, shortcut, *args = args
action = old_add_action(self, icon, text, *args)
action.setShortcut(shortcut)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might have to explicitly convert the shortcut to QKeySequence, for Qt accepts only that type of the argument here. See the Qt5 docs and the Qt6 docs.

For the latest packages, help(QAction.setShortcut) reads the following:

  • for PyQt5, the valid call is
    • setShortcut(self, shortcut: Union[QKeySequence, QKeySequence.StandardKey, Optional[str], int]);
  • for PySide2, the valid call is
    • setShortcut(self, shortcut: PySide2.QtGui.QKeySequence);
  • for PyQt6, the valid call is
    • setShortcut(self, shortcut: Union[QKeySequence, QKeySequence.StandardKey, Optional[str], int]);
  • and only for PySide6, the valid calls are
    • setShortcut(self, arg__1: Qt.Key) -> None,
    • setShortcut(self, shortcut: Union[QKeySequence, QKeyCombination, QKeySequence.StandardKey, str, int]) -> None.

It appears that all the flavors support a string as the shortcut. However, PySide2 fails when a Qt.Key is passed here. I admit, there is an omission in the tests.

I've composed #461 thanks to this PR. If you wish, you may cherry-pick the test from #461. I'm going to add a back port for QAction.setShortcut to call it with Qt.Key argument. You are welcome to prepare the latter PR before I do.

@dalthviz
Copy link
Member

dalthviz commented Oct 18, 2023

Should this be a blocker for v2.4.1 given the breakage that's evidently causing per #458 ?

I thought that maybe this was a kind of simple improvement but now I see that this would need way more work to accomodate things with the new findings (thanks @StSav012 for giving this a check!). So not a blocker 👍

@dalthviz dalthviz modified the milestones: v2.4.1, v2.4.2 Oct 18, 2023
@ccordoba12
Copy link
Member

Also, what do you think about this @ccordoba12 ?

Since (according to the description) this is refactoring, @StSav012 knows this code quite well and his review shows that it has several problems, and @StSav012's PR #461 conflicts with this one, I think we should close it in favor of that one.

@dalthviz dalthviz modified the milestones: v2.4.2, v2.5.0 Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants