Package: types-pyside2
Type stubs for PySide2 (and soon PySide6)The most accurate type stubs for PySide! They have been tested usingmypyon a code base with many thousands of lines of PySide code.Comparison to other PySide stubsI tried a number of projects before deciding to create my own.  Here's my super-biased assessment:Stub ProjectTechniqueRatingOfficial stubsUses PySide'sgenerate_pyistub generatorabysmalPySide2-Stubs-GenUses a modified version ofgenerate_pyimarginalPySide2-stubsReprocesses official stubs usinglibcstbettertypes-PySide2Uses mypy'sstubgenbestPySide2-stubsis 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 officialstubgentool to directly generate stubs, with a set of corrections applied.Features and fixesGeneral fixesFixed an issue where methods/attributes were not detected, due to presence ofQObject.__getattr__()Added all signals and made new-style signal patterns worke.g.myobject.mysignal.connect(func)andmyobject.mysignal[type].connect(func)Fixed slot arg ofSignalInstance.connect()to betyping.Callableinstead ofobjectFixedSignal.emit()FixedSignal.connect()return value toboolinstead ofNoneFixedObject.disconnect()Added all methods to flag classes:__or__,__xor__, ...Rule-based fixesWhen instantiating subclasses ofQObjectit 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.Qt/PySide has special "flag" enumerator classes that work as pairs: one represents a single flag value, while the other represents multiple combined.  The stubs have been fix to allow either type of flag -- single or multiple -- anywhere that one of the would have been accepted, which is the correct behavior (technicallytyping.SupportsIntis the most correct, but using this would undermine the type enforcement provided by the stubs).Removed redundant overlapping overloads, so that satisfying mypy/liskov on subclassed methods is easierFor methods that implement both classmethod and instancemethod overloads, the classmethod overloads have been removed.  Unfortunately, mypy disallows mixing these and does not correctly analyze them.Corrected all arguments typed astyping.Sequenceto betyping.Iterable.  Tests so far have indicated that this is true as a general rule.Added sub-types toIterableannotations, e.g.Iterable[str],Iterable[int], etcReplacedobjectwithtyping.Anyin return types. e.g.:QSettings.value() -> AnyQModelIndex.internalPointer() -> AnyQPersistentModelIndex.internalPointer() -> AnySpecific fixesCertain 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.TypeCorrected numerous annotations frombytes/QByteArraytostr:QObject.setProperty()QObject.property()QState.assignProperty()QCoreApplication.translate()formatargs on all methodsFixedQTreeWidgetItemIterator.__iter__()to returnIterator[QTreeWidgetItemIterator]Added missingQDialog.exec()methodFixed numerous methods which acceptNone:QPainter.drawText(..., br)QPainter.drawPolygon(..., arg__2)QProgressDialog.setCancelButton(button)*.setModel(model)QLabel.setPixmap(arg__1)Fixed numerous arguments that acceptQModelIndexwhich were typed asintFixed return type forQApplication.instance()andQGuiApplication.instance()Fixed return type forQObject.findChild()andQObject.findChildren()Fixed support for initializingQDatefromdatetime.dateFixed support for initializingQDateTimefromdatetime.datetimeFixedQByteArray.__iter__()to returnIterator[bytes]Fixed support forbytes(QByteArray(b'foo'))Added support for allQSizeandQSizeFoperationsAdded support for allQPolygonoperationsFixedQTextEdit.setFontWeight()to acceptQFont.WeightFixed return type forqVersion()AddQSpacerItem.__init__/changeSizeoverloads that use alternate names:hData->hPolicy,vData->vPolicyLicensingAs a derived work from PySide2, the stubs are delivered under the LGPL v2.1 . See file LICENSE for more details.InstallationInstall the latest stub packages from pypi:$ pip install types-PySide2This will add thePySide2-stubsandshiboken2-stubspackages into your site-packages directory.Yes, the name of the pypi package istypes-PySide2but the python package it installs isPySide2-stubs.It's confusing, butPEP 561requires 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-stubsHelp improve the stubsIf 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.Testingpython3 -m venv .venv
. .venv/bin/activate
toxTODOBuild PySide6 stubsMerge overloads where aUnionwould do instead of multiple overloadsAdd type enforcement for signal types, to protect against incorrect callables provided toconnect()