0

I'm trying to find a way to get the signature of a bound SignalInstance in PySide.

Take the following example:

from PySide2.QtCore import Signal, QObject

class MyObj(QObject):
    some_signal = Signal(int)

obj = MyObj()
sig_instance = obj.some_signal

I can get to the signature of some_signal if I have a pointer to obj or MyObj itself, with obj.metaObject().method(5).methodSignature() ... but what if I only had a pointer to sig_instance?

In C, the SignalInstance keeps the signature in a private struct PySideSignalInstancePrivate (here) ... but after a log of digging, I've been unable to come up with a way to recover that signature given only the python SignalInstance

tips?


update:

If it's genuinely not possible without access to the metaObject on obj itself. I have this stupid solution... but still would love to know if there's a better way.

try:
    sig_instance.emit(*(1,) * 50)  # emit with ridiculous args
except TypeError as e:
    print(str(e).split(' only accepts')[0])  # parse err

I'll also note that a PyQt5.QtCore.pyqtBoundSignal does have an attribute .signal that holds the string form of the signature

tlambert
  • 67
  • 1
  • 3
  • 8
  • I don't think it's possible, anyway that information is in the QMetaObject of the QObject. – eyllanesc Nov 15 '21 at 15:23
  • @tlambert may I ask you why do you need this? – musicamante Nov 15 '21 at 20:31
  • sure, the "general" use case is providing `signal_instance.emit` as a callback to some other thing. It would be nice if that other thing could inspect the signature of `signal_instance.emit` (exactly like Qt itself does if you use `connect(some_callback)`... Qt is able to be smart about not providing too many params to some_callback). Unfortunately: `inspect.signature(sig_instance.emit)` gives `Signature (*args: typing.Any)`... not too helpful. The specific use case is here: https://github.com/tlambert03/psygnal/issues/47 – tlambert Nov 15 '21 at 21:24
  • @tlambert Yes, I found the same issue in PySide too. Maybe you could file a bug report on Qt? – musicamante Nov 15 '21 at 22:08
  • sure, can do. Almost started there, but then chickened out and came here :) – tlambert Nov 16 '21 at 02:02

1 Answers1

0

I give this solution in another question.

If you got error with the Signal, it's because Qt/PySide do some magic tricks and convert Signal to SignalInstance. My solution is to cast SignalInstance to get to right hint.

from typing import cast
from PySide2.QtCore import Signal, SignalInstance

class MyObj(QObject):
    some_signal = cast(SignalInstance, Signal(int))

obj = MyObj()
sig_instance = obj.some_signal
KerberosMorphy
  • 330
  • 1
  • 4
  • 14
  • thanks, but this isn't a typing issue. I have an application where I'd like to know the number of parameters that `some_signal` expects (i.e. 1 in this example). but without having access to `MyObj` or `obj` – tlambert Nov 15 '21 at 15:38