dangerzone icon indicating copy to clipboard operation
dangerzone copied to clipboard

PySide6 does not offer sufficient Mypy hints

Open apyrgio opened this issue 2 years ago • 3 comments

PySide6 has the same issues as PySide2 with regards to Mypy; they both lack the necessary type hints, and Mypy complains loudly.

dangerzone/gui/logic.py:43: error: "Type[QFontDatabase]" has no attribute "FixedFont"  [attr-defined]
dangerzone/gui/logic.py:136: error: "Type[Qt]" has no attribute "CustomizeWindowHint"  [attr-defined]
dangerzone/gui/logic.py:137: error: "Type[Qt]" has no attribute "WindowTitleHint"  [attr-defined]
dangerzone/gui/logic.py:138: error: "Type[Qt]" has no attribute "WindowSystemMenuHint"  [attr-defined]
dangerzone/gui/logic.py:139: error: "Type[Qt]" has no attribute "WindowCloseButtonHint"  [attr-defined]
dangerzone/gui/logic.py:140: error: "Type[Qt]" has no attribute "WindowStaysOnTopHint"  [attr-defined]

For PySide2, we have sidestepped this issue by installing a separate project as a dev (lint) dependency: PySide2-stubs. For PySide6 though, there are currently no separate stubs, and it's still an open issue in their tracker.

apyrgio avatar Jan 25 '23 16:01 apyrgio

For the time being, our suggestion is to always use PySide2 stubs for typing checks:

if typing.TYPE_CHECKING:
    from PySide2 import QtCore, QtGui, QtWidgets
else:
    try:
        from PySide6 import QtCore, QtGui, QtWidgets
    except ImportError:
        from PySide2 import QtCore, QtGui, QtWidgets

The rationale is the following:

  1. The PySide2 and PySide6 interfaces we are using happen to be the same, so the checks we do for PySide2 will apply to PySide6 as well.
  2. Environments only with PySide2 remain unaffected.

apyrgio avatar Jan 25 '23 16:01 apyrgio

Developments that may render the above not necessary:

  1. PySide6 devs have recently closed some issues related to missing Mypy stubs. See https://bugreports.qt.io/browse/PYSIDE-1603, and the duplicate links section. This fix will land in 6.4.3, else 6.5.0. We should check it out at some point.
  2. There's a project called types-PySide, which offers PySide2 stubs, and aims to offer PySide6 stuff along the road as well. We should keep an eye to that project.

apyrgio avatar Jan 25 '23 16:01 apyrgio

Qt 6.8 improved how they are dealing with types. I've not tried it, but it might help with this issue.

almet avatar Oct 14 '24 11:10 almet