The most accurate stubs for PySide6
Project description
Type stubs for PySide6
The most accurate type stubs for PySide! They have been tested using mypy on a code base with many thousands of lines of PySide code.
Comparison to other PySide stubs
I tried a number of projects before deciding to create my own. Here's my super-biased assessment:
| Stub Project | Technique | Rating |
|---|---|---|
| Official stubs | Uses PySide's generate_pyi stub generator |
abysmal |
| PySide2-Stubs-Gen | Uses a modified version of generate_pyi |
marginal |
| PySide2-stubs | Reprocesses official stubs using libcst | better |
| types-PySide2 | Uses mypy's stubgen | best |
PySide2-stubs is pretty good, but it still produced hundreds of errors in our code base.
I considered contributing new features to that project, but the approach of using an AST/CST parser to modify
an upstream set of bad official stubs to make them good is convoluted and prone to errors from upstream changes.
This project uses mypy's official stubgen tool to directly generate stubs, with a set of corrections applied.
Features and fixes
General fixes
- Fixed an issue where methods/attributes were not detected, due to presence of
QObject.__getattr__() - Added all signals and made new-style signal patterns work
- e.g.
myobject.mysignal.connect(func)andmyobject.mysignal[type].connect(func) - Fixed slot arg of
SignalInstance.connect()to betyping.Callableinstead ofobject - Fix type arg of
SignalInstance.connect()to beQtCore.Qt.ConnectionTypeinstead oftype | None - Fixed
Signal.emit() - Fixed
Signal.connect()return value toboolinstead ofNone - Fixed
Object.disconnect()
- e.g.
- Added all methods to flag classes:
__or__,__xor__, ...
Rule-based fixes
- When instantiating subclasses of
QObjectit is possible to pass the values of properties and signals as**kwargsto__init__. The stubs have been fix to include these args on all relevant__init__methods. - Removed redundant overlapping overloads, so that satisfying mypy/liskov on subclassed methods is easier
- Corrected all arguments typed as
typing.Sequenceto betyping.Iterable. Tests so far have indicated that this is true as a general rule. - Added sub-types to
Iterableannotations, e.g.Iterable[str],Iterable[int], etc - Replaced
objectwithtyping.Anyin return types. e.g.:QSettings.value() -> AnyQModelIndex.internalPointer() -> AnyQPersistentModelIndex.internalPointer() -> Any
- Added support for overloads that mix static and instance methods.
mypydisallows this using traditional overloads, so this project achieves it by generating specialized decorator classes that hold each of the overloads.
Specific fixes
- Certain argument types implicitly accept alternative types for brevity. Below are the known fixes so far (Note that I've debated not including these, since one of the advantages of static typing is it gives you the confidence to be explicit rather than ambiguous. I could introduce a strict mode in the future that would disable these):
QKeySequence:strQColor:Qt.GlobalColorandintQBrush:QLinearGradientandQColor(and by extensionQt.GlobalColor)QCursor:Qt.CursorShapeQEasingCurve:QEasingCurve.Type
- Corrected numerous annotations from
bytes/QByteArraytostr:QObject.setProperty()QObject.property()QState.assignProperty()QCoreApplication.translate()formatargs on all methods
- Fixed
QTreeWidgetItemIterator.__iter__()to returnIterator[QTreeWidgetItemIterator] - Added missing
QDialog.exec()method - Fixed numerous methods which accept
None:QPainter.drawText(..., br)QPainter.drawPolygon(..., arg__2)QProgressDialog.setCancelButton(button)*.setModel(model)QLabel.setPixmap(arg__1)
- Fixed numerous arguments that accept
QModelIndexwhich were typed asint - Fixed return type for
QApplication.instance()andQGuiApplication.instance() - Fixed return type for
QObject.findChild()andQObject.findChildren() - Fixed support for initializing
QDatefromdatetime.date - Fixed support for initializing
QDateTimefromdatetime.datetime - Fixed
QByteArray.__iter__()to returnIterator[bytes] - Fixed support for
bytes(QByteArray(b'foo')) - Added support for all
QSizeandQSizeFoperations - Added support for all
QPolygonoperations - Fixed
QTextEdit.setFontWeight()to acceptQFont.Weight - Fixed return type for
qVersion() - Add
QSpacerItem.__init__/changeSizeoverloads that use alternate names:hData->hPolicy,vData->vPolicy - Fixed
QAction.menuto return optionalQMenuinstead ofQOjbect
Licensing
As a derived work from PySide2, the stubs are delivered under the LGPL v2.1 . See file LICENSE for more details.
Installation
Install the latest stub packages from pypi:
$ pip install types-PySide2
This will add the PySide2-stubs and shiboken2-stubs packages into your site-packages directory.
Yes, the name of the pypi package is types-PySide2 but the python package it installs is PySide2-stubs.
It's confusing, but PEP 561 requires that the installed package name is of the form $PACKAGE-stubs, so all of us PySide stub developers are installing a package with the same name.
Note, you may need to uninstall other PySide2 stubs first:
$ pip uninstall PySide2-stubs
Help improve the stubs
If you notice incorrect or missing typing information (i.e. mypy reports errors even though your code is correct), please report it or make a PR to fix it.
Testing
python3 -m venv .venv
. .venv/bin/activate
tox
TODO
- Build PySide6 stubs
- Merge overloads where a
Unionwould do instead of multiple overloads - Add type enforcement for signal types, to protect against incorrect callables provided to
connect()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file types_pyside6-6.10.1.1-py2.py3-none-any.whl.
File metadata
- Download URL: types_pyside6-6.10.1.1-py2.py3-none-any.whl
- Upload date:
- Size: 590.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
30ff49550c8f40e0606d70f9fbcf1f0c4b688d896df38f4f6a6912c231cbfa16
|
|
| MD5 |
ac05b4924e0a7b1c474765ac983edf7b
|
|
| BLAKE2b-256 |
dd89ebea68732d692df588cc3f5308bd341c0431f4acf70f39e3680089a83222
|