Three different enum behavior

Philippe Fremy phil at freehackers.org
Sat Nov 13 17:42:37 GMT 2021


Hi,

I am working on improving the typing information of the project 
PyQt5-stubs for all QFlags classes.

While working on this, I discovered some differences between QFlags 
enums. Basically, QFlags related classes are always composed of two 
classes:
- the enum class which inherits from int
- the QFlag classes which represents the composition of one or several 
enum

The differences I noticed are on the behavior of the "or" and "ror" for 
the enum class. In some cases, it will result in an int, kind of 
defeating the purpose of enum classes. In some others, it will result in 
the corresponding QFlag classes.

Examples:

1.a Oring two enum results in a QFlag:

	>>> from PyQt5 import QtWidgets
	>>> flag = QtWidgets.QAbstractSpinBox.StepEnabledFlag()
	>>> flag | flag
	<PyQt5.QtWidgets.QAbstractSpinBox.StepEnabled object at 0x03A32DF0>


1.b Oring two enum results in an int:

	>>> from PyQt5 import QtWidgets
	>>> flag = QtWidgets.QStyleOptionTab.TabFeature()
	>>> flag | flag
	0

2.a Oring enum with int results in a QFlag

	>>> from PyQt5 import QtGui
	>>> flag = QtGui.QTouchEvent.TouchPoint.InfoFlag()
	>>> flag | 1
	<PyQt5.QtGui.QTouchEvent.TouchPoint.InfoFlags object at 0x03A32FB0>

2.b Oring enum with int results in an int

	>>> from PyQt5 import QtCore
	>>> flag = QtWidgets.QAbstractSpinBox.StepEnabledFlag()
	>>> flag | 1
	1

3.a Oring int with enum results in a QFlag

	>>> from PyQt5 import QtCore
	>>> flag = QtCore.QTextBoundaryFinder.BoundaryReason()
	>>> 1 | flag
	<PyQt5.QtCore.QTextBoundaryFinder.BoundaryReasons object at 0x03A32FB0>


3.b Oring int with enum results in an int

	>>> from PyQt5 import QtWidgets
	>>> flag = QtWidgets.QStyleOptionTab.TabFeature()
	>>> 1 | flag
	1

Globally, all enum class fall into three categories:
- behavior 1.a + 2.a + 3.a
- behavior 1.a + 2.b + 3.a
- behavior 1.b + 2.b + 3.b

I am not sure if this is intended behavior. Anyway, this will be 
properly handled in the next release of PyQt5-stubs.

Have a nice day,

Philippe


More information about the PyQt mailing list