Why Eclipse shows lines with Qt.LeftButton as an error with PySide6, even though the code still works
-
Hi there,
We are finally making a switch from pySide2 to pySide6 and this issue caught my attention. The code in question is pretty much anything that is using QCore.Qt library and this is one of examples:
if event.button() == Qt.RightButton:
The same code with pySide2 didn't show any errors, but with the pySide6, it is not recognizing RightButton part. If code is changed to this instead:
if event.button() == Qt.MouseButton.RightButton:
then Eclipse is happy and no error is presented in the IDE.
Yet, either version of the code is working just fine.Looking into the definition of these enumerations, I can see this structure with the pySide6:
class Qt(Shiboken.Object): class MouseButton(enum.Flag): MouseButtonMask = ... # -1 NoButton = ... # 0x0 LeftButton = ... # 0x1 RightButton = ... # 0x2 MiddleButton = ... # 0x4 . . .
This definition explains why there is an error there with the direct use of the enumeration, but not why the code still executes correctly both versions. As a matter of fact, I am perfectly fine with both version running, since I could leave the code as is. I don't have access to the pySide2 version of this library so I can't compare them, which would certainly help. But seeing all those errors in the IDE is annoying for sure.
Is there a way to understand better how this is working both ways, that could help me tweak Eclipse, so it doesn't show those lines as errors anymore?
Thanks
-
this is typically what happens when you change versions, especially with major releases (the first part of the version number), some changes are made for lots of different reasons .
switching from pyside2 (Qt 5.a.b) to pyside6 (Qt 6.x.y) makes you change to another major release, so in this case you have to expect some changes and some code to be rewritten. -
I definitely understand that part about rewriting some of the code, but here is that code that still works without any changes. Yet, Eclipse is showing it as an error. Here is how that looks like in a made up code:
This code works just fine and accepts both left and right mouse clicks.
I am wondering why that is, while hoping that would help me to suppress those IDE errors
-
probably something (module, extension or whatsoever) not up to date in your editor
-
probably something (module, extension or whatsoever) not up to date in your editor
@ankou29666 said in Why Eclipse shows lines with Qt.LeftButton as an error with PySide6, even though the code still works:
probably something (module, extension or whatsoever) not up to date in your editor
I am running the latest version of the Eclipse and latest version of the python plugin as well
-
I definitely understand that part about rewriting some of the code, but here is that code that still works without any changes. Yet, Eclipse is showing it as an error. Here is how that looks like in a made up code:
This code works just fine and accepts both left and right mouse clicks.
I am wondering why that is, while hoping that would help me to suppress those IDE errors
@MrAWD
Using PyCharm as the IDE results in the same behaviour: although the program runs fine the editor claimsQt.LeftButton
is an "unresolved attribute reference" and you cannot "follow" it to find any further information.At Qt6 PySide6 enum Flags were moved from all appearing in the "global" Qt class and being implemented in Shiboken to being true Python enums in a specific class like
Qt.MouseButton
. You are supposed to change over your existing code to use the new definitions.However Doing a Smooth Transition from the Old Enums explains
Changing all the enum code to suddenly use the new syntax is cumbersome and error-prone, because such necessary changes are not easy to find. Therefore a
forgiveness mode
was developed:The
forgiveness mode
allows you to continue using the old constructs but translates them silently into the new ones. If you for example write
[...]
you get in reality a construct that mimics the following code which is the recommended way of writing Flags and Enums:
[...]
This has the effect that you can initially ignore the difference between old and new enums, as long as the new enums are properties of classes.See also https://doc.qt.io/qtforpython-6/considerations.html#enums-behavior-in-pyside and https://doc.qt.io/qtforpython-6/developer/enumfeatures_doc.html#the-set-of-enum-features.
I have not looked into it further, but whatever that
forgiveness mode
does it must be enough to make the code work correctly when run but must create the definition or whatever "dynamically at runtime" such that IDEs like PyCharm or your Eclipse do not evaluate it, know nothing about it and hence complain it is "undefined".Note that this is a PySide6 feature only: if try the code under PyQt6 I get the AttributeError on
Qt.RightButton
.To help you spot all the changed definitions and upgrade you could either (a) use the links above to tell PySide6 not to do the
forgiveness mode
and hence error at runtime like PyQt6 does or (b) in the solution at Migrating to Qt6/PyQt6: what are all the deprecated short-form names in Qt5? someone has written a script:I wrote a script to extract all the short-form and corresponding fully qualified enum names from the PyQt6 installation. It then does the conversions automatically:
Similarly there is PyQtEnumConverter 1.0.
Although these are PyQt-specific you might either adapt them or take inspiration from their code to achieve similar for PySide.