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

typing: Make QtCore Slot() decorator typesafe

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 6.8.2
    • Type hints
    • None

    Description

      As you know, PySide6 provides a decorator to help hinting that a specific method is actually a slot.

       

      For example :

       
       

      class SomeClass2(QObject):
          @Slot(int, result=str)
          def f_int_returns_str2(self, i: int) -> str: 
              return 'abc'

       
      Since Slot describes its argument types and return value, an interesting challenge was ... can we make this typesafe ? Can we make mpy check the consistency of information passed to Slot and the actual type information of the method ?

      The answer is yes, but it's not so easy.

      I attach here :

      • the example that should type-check correctly
      • an implementation of type-checked Slot()

       

      The implementation is quite heavy because I had to provide type signature for each overload with a diffrent number of arguments and each overload has two variant whether the return value is specified or not.

      This was written long before recent improvements in specifying overloads arguments. So there might be a simpler way to achieve this same result today.

       

      Anyway, here you are.
       
       

      Attachments

        1. slot.py
          5 kB
        2. typed_slot.pyi
          4 kB
        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:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes