[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    typing: Handling of arguments that can be None
From:       Florian Bruhin <me () the-compiler ! org>
Date:       2023-07-08 23:23:57
Message-ID: 168885863762.451705.13309823172366183173 () aragog ! localdomain
[Download RAW message or body]


Hey,

In Qt, all arguments are pointers, yet only rarely it's intended usage
to pass a nullptr to functions.

PyQt currently handles this in the type annotations by never using
Optional[...], i.e. always disallowing None.

While this is often the right thing to do, it means that e.g.

    from PyQt6.QtWidgets import QTableView, QApplication
    app = QApplication([])
    tv = QTableView()
    tv.setModel(None)

despite being intended usage. Unfortunately (like here), the Qt
documentation never really points that out:

https://doc.qt.io/qt-6/qabstractitemview.html#setModel

However, the PyQt6-stubs project has gathered a list of known methods
where it's intended usage to pass None for an argument:

https://github.com/python-qt-tools/PyQt6-stubs/blob/main/fixes/annotation_fixes.py#L74

They are:

	- QLineEdit.setText(None)
	- QAbstractItemView.setModel(None) and everything inheriting it
      (QColumnView, QHeaderView, QTableView, QTreeView)
	- QMessageBox.aboutQt(None, ...)  # parent
	- QMessageBox.about(None, ...)  # parent
	- QProgressDialog.setCancelButton(None)

Undoubtedly there will be more yet to be discovered.
But maybe it'd make sense to have some kind of sip annotation (or the existing
/AllowNone/?) so that it generates Optional[...] in the type hints? Then this
could probably be fixed piece by piece when it pops up.

Florian

[Attachment #3 (application/pgp-signature)]

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic