Details
-
Suggestion
-
Resolution: Fixed
-
Not Evaluated
-
None
-
None
-
The type argument is not fixed.
-
1001b0bf0 (dev), 0e764101d (6.9), f325c0f0f (6.8), 1f8161f2e (dev), 51778ac56 (6.9), ac014ca05 (6.8)
Description
Reported from PySide6-stubs
QObject.findChildren() and findChild() need typing improvements.
The following example should work:
a: List[QObject]
a = o1.findChildren(QObject)
But it fails on two reasons:
- findChildren() returns Iterable[] but this prevents assigning the result to an actual list. For input values, it is a good idea to be generic and use Iterable[] (covariant type). For return values, it is a good idea to be very specific (contravariant type)
- the type of the return value of findChildren() is not type-checked against the type we are looking for.
For example, the following code should generate a typing error, but it does not:
c: List[QWidget]
c = o1.findChildren(QObject, '')
See https://github.com/python-qt-tools/PySide6-stubs/blob/main/tests/qobject.py for a full example.
The correct signature to have propre type-checking is the following:
@overload
def findChildren(self, type: Type[PlaceHolderType], name: str = ..., options: PySide6.QtCore.Qt.FindChildOption = ...) -> List[PlaceHolderType]: ...
@overload
def findChildren(self, type: Type[PlaceHolderType], pattern: Union[PySide6.QtCore.QRegularExpression, str], options: PySide6.QtCore.Qt.FindChildOption = ...) -> List[PlaceHolderType]: ...
This applies also to findChild():
def findChild(self, type: Type[PlaceHolderType], name: str = ..., options: PySide6.QtCore.Qt.FindChildOption = ...) -> Optional[PlaceHolderType]: ...