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

Callable type hints are not compliant with the Python typing specification

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3: Somewhat important
    • 6.8.1
    • 6.7
    • Type hints
    • None
    • 7ee3fb795 (dev), c1498fe58 (6.8)

    Description

      Problem:

      There are many function arguments in the PySide6 type stubs which are simply typed as Callable without any type arguments. One example of this is the singleShot timer method:

      class QTimer(PySide6.QtCore.QObject):
          ...
          @overload
          @staticmethod
          def singleShot(msec: int, functor: Callable) -> None: ...

      Although I suspect this means the functor argument accepts any callable, strict type checkers will mark this as a partially unknown type since the typing specification states that type hint arguments are always required:

      Annotating callable objects

      The subscription syntax must always be used with exactly two values: the argument list and the return type. The argument list must be a list of types, a ParamSpec, Concatenate, or an ellipsis. The return type must be a single type.

      If a literal ellipsis ... is given as the argument list, it indicates that a callable with any arbitrary parameter list would be acceptable:

      def concat(x: str, y: str) -> str:
          return x + y
      
      x: Callable[..., str]
      x = str     # OK
      x = concat  # Also OK

      Source: typing - Support for type hints

      Impact:

      The impact of this typing error is relatively low as unknown argument types for library methods rarely have cascading effects. Nevertheless, it does result in the following error when using Pylance in strict mode and should not be difficult to fix:

      Type of "singleShot" is partially unknown Pylance(reportUnknownMemberType)

      Proposed Solution:

      Assuming all Callable type hints without arguments refer to cases where any callable object is acceptable, they should simply be replaced with the following:

      from typing import Any, Callable
      
      def example(functor: Callable) -> ... # Before
      def example(functor: Callable[..., Any]) -> ... # After

      Alternatively, if the Callable type hints are intended to represent a callable that takes no arguments and returns anything, the following change would be appropriate:

      from typing import Any, Callable
      
      def example(functor: Callable) -> ... # Before
      def example(functor: Callable[[], Any]) -> ... # After

       

      Attachments

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

        Activity

          People

            ctismer Christian Tismer
            bflecker Bryton Flecker
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes