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

slots types by type hints.

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Duplicate
    • Not Evaluated
    • None
    • all
    • Type hints
    • None
    • All

    Description

      It can be a greate improvement for `Slot` decorator to  evaluate the type from type hints.

      note that If you want I already implemented this in a self contained module. 

       

      import itertools
      from typing import Any, Optional, get_argsfrom PySide6 import QtCore as qtcfrom qtgql.typingref import is_optional
      def get_optional_args(annotations: list[Any]) -> list[Any]:
          return [get_args(arg)[0] for arg in annotations if is_optional(arg)]
      def get_concrete(annotation: Any) -> Optional[type]:
          return getattr(annotation, "__origin__", None)
      def get_concretes(annotations: list) -> list:
          ret = []
          for item in annotations:
              if is_optional(item):
                  annotations.remove(item)
                  continue
              if concrete := get_concrete(item):
                  ret.append(concrete)
                  annotations.remove(item)
          return ret + annotations
      def slot(func):
          def wrapper(func) -> func:
              anots: dict = func.__annotations__
              return_ = anots.pop("return", None)
              if return_ is Any:
                  return_ = "QVariant"
              args = list(anots.values())
              stripped_optionals = get_concretes(get_optional_args(args))
              concretes = get_concretes(args)        if stripped_optionals:
                  required_annotations = tuple(concretes)
                  combos = []
                  for i in range(len(stripped_optionals) + 1):
                      for subset in itertools.combinations(stripped_optionals, i):
                          combos.append(subset)
                  for combo in combos:
                      func = qtc.Slot(*combo + required_annotations, result=return_)(func)            return func        return qtc.Slot(*concretes, result=return_)(func)    return wrapper(func) 

      I also have tests for this.

      Attachments

        1. typed_slot_benchmark.py
          1 kB
          nir benlulu
        2. mypypoc.py
          1 kB
          nir benlulu

        Issue Links

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

          Activity

            People

              crmaurei Cristian Maureira-Fredes
              nir nir benlulu
              Votes:
              2 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes