Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-3045

typing: QObject.findChildren() and findChild() need typing improvements

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Fixed
    • Not Evaluated
    • 6.9.0, 6.10.0
    • None
    • Type hints
    • 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]: ...

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            ctismer Christian Tismer
            bluebird75 Philippe Fremy
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: