MyPy complains about Qt.AlignmentFlag

Viewed 222

When I upgrade to PySide6 6.2.2, MyPy starts complaining about Qt.AlignmentFlag. Here's a small example:

from PySide6.QtGui import Qt
from PySide6.QtWidgets import QLabel, QApplication

app = QApplication()
label = QLabel("Hello, World!")
label.setAlignment(Qt.AlignCenter)
label.show()

app.exec()

That still runs fine, but MyPy complains:

$ mypy foo.py 
foo.py:6: error: Argument 1 to "setAlignment" of "QLabel" has incompatible type "AlignmentFlag"; expected "Alignment"
Found 1 error in 1 file (checked 1 source file)
$

OK, I try converting the AlignmentFlag to an Alignment.

from PySide6.QtGui import Qt
from PySide6.QtWidgets import QLabel, QApplication

app = QApplication()
label = QLabel("Hello, World!")
label.setAlignment(Qt.Alignment(Qt.AlignCenter))
label.show()

app.exec()

Again, that runs fine, but MyPy now complains about the constructor.

$ mypy foo.py 
foo.py:6: error: Too many arguments for "Alignment"
Found 1 error in 1 file (checked 1 source file)
$

Could someone please explain how to use Qt.Alignment and Qt.AlignmentFlag?

1 Answers

Until I find a better option, I'm going to assume that someone messed up the type hints in PySide6 6.2.2, and I'll just tell MyPy to ignore it.

from PySide6.QtGui import Qt
from PySide6.QtWidgets import QLabel, QApplication

app = QApplication()
label = QLabel("Hello, World!")
label.setAlignment(Qt.AlignCenter)  # type: ignore
label.show()

app.exec()
Related