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

Fix type inference for QmlElement

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • P4: Low
    • None
    • 6.6.2
    • Type hints
    • None
    • VSCode + Pylance, Python 3.10, PySide 6.6

    Description

      Currently, using the QmlElement decorator erases type information, because it is defined to return object/Any. Users can work around this issue by wrapping the decorator with a generic function, for example:

      from PySide6.QtCore import QObject
      from PySide6.QtQml import QmlElement
      
      @QmlElement
      class Erased(QObject):
          pass
      
      E = TypeVar("E")
      def TypedQmlElement(arg: E) -> E:
          return QmlElement(arg)
      
      @TypedQmlElement
      class NotErased(QObject):
          pass
      
      def do_something():
          return Erased(), NotErased()
      
      erased, not_erased = do_something()
      # Pylance shows "erased: Any, not_erased: NotErased"
      

      This is fixed either by adding the typevar to the .pyi file instead, or apparently by removing the type hints entirely:

      E = TypeVar("E")
      def QmlElement(arg__1: E) -> E: ... 
      # or
      def QmlElement(arg__1): ...

      Attachments

        Issue Links

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

          Activity

            People

              ctismer Christian Tismer
              thorn132 Tim Hornick
              Votes:
              2 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Gerrit Reviews

                  There are no open Gerrit changes