QtCore.QCoreApplication.instance() may return None#126
Conversation
|
While this is true in theory, I wonder if it's worth the pain it causes in practice? In qutebrowser, with this change I get: For all those, I know a QApplication exists at that point. I'd now have to replace all occurrences of app = QApplication.instance()
assert app is not None
app.something()to appease mypy, for very little benefit (if there was no QApplication, my application would crash at runtime either way). |
|
Hmm that's a pretty good point and certainly a common use case. Is there any way to tell mypy that something should usually be This situation is making me think of GCC's |
|
I'm not aware of one - if we tell mypy that it can be |
|
Just to have the original situation that made me notice this listed here... It's nothing beautiful, but so it goes with global persistent state. def maybe_build_application() -> QtCore.QCoreApplication:
"""Create a new Qt application object if one does not already exist.
Returns:
The Qt application object.
"""
application: QtCore.QCoreApplication
# TODO: https://bugreports.qt.io/browse/PYSIDE-1467
if qtpy.PYQT5:
maybe_application = QtWidgets.QApplication.instance()
elif qtpy.PYSIDE2:
maybe_application = typing.cast(
typing.Optional[QtCore.QCoreApplication], QtWidgets.QApplication.instance()
)
else: # pragma: no cover
raise qtrio.InternalError(
"You should not be here but you are running neither PyQt5 nor PySide2.",
)
if maybe_application is None:
application = QtWidgets.QApplication(sys.argv[1:])
else:
application = maybe_application
application.setQuitOnLastWindowClosed(False)
return applicationIf you don't want to check (I understand that) it could be a cast instead of an assert. Not that that changes much. There could be an exception-raising wrapper function that always returns an instance that is called instead. I hesitate to intentionally make hints wrong especially if the cost is touching just twenty bits of code in a fairly simple way with multiple options. If it is relevant, I'd be perfectly happy to do a PR for qutebrowser to respond to this change. I'll look around in case there's some other way to tell mypy to ignore the optionality. |
We know that QApplication.instance() will always be non-None for practical purposes, but the stubs now (correctly) declare it as Optional. See python-qt-tools/PyQt5-stubs#126
We know that QApplication.instance() will always be non-None for practical purposes, but the stubs now (correctly) declare it as Optional. See python-qt-tools/PyQt5-stubs#126
QtCore.QCoreApplication.instance() may return None
No description provided.