altendky / qtrio

QTrio - a library bringing Qt GUIs together with async and await via Trio
https://qtrio.readthedocs.io/
Other
40 stars 4 forks source link

Using duck-typed signals (non-Qt) - can this cause problems? #94

Open mkroutikov opened 4 years ago

mkroutikov commented 4 years ago

Along with Qt Signal, I also use my own homegrown version which is api-compatible (connect/disconnect/emit), but does not use Qt code. Such signals are handy because some controllers can be used in a CLI-only applications (code reuse).

Not being very familiar with type hints, just getting a bit worried. My understanding is that runtime nothing bad can happen. But maybe UI (PyCharm?) or static type checkers will flag errors?

Question: do you see any problem with duck-typed non-Qt signals?

altendky commented 4 years ago

On the topic of signal-alikes I'll mention that I happened to sneak a little one of my own in here. I probably shouldn't have but it's not public API presently. :] It is a drop in descriptor layer that lets you put actual Qt signals on non-QObjects by creating the host QObjects automatically behind the scenes. There were just too many places in my application where I inherited from QObject exclusively to have signals.

https://github.com/altendky/qtrio/blob/0178ef6361c210bc32c129c32e1228ff4fc1c495/qtrio/_qt.py#L10-L53

But... sure, type hinting could make PyCharm complain. I think the solutions are 'interfaces' (like via zope.interface) or 'protocols' (typing and mypy) or maybe you can just tell PyCharm or mypy that your thing is close enough to a QtCore.SignalInstance?

I'm game for an exploration of supporting this case nicely though. Hopefully it really is just a type hinting thing. Hmm... I suppose if you tried to chain both Qt and non-Qt signals there could be trouble. But I don't recall anything offhand in QTrio itself that should care. I think the following are the trickiest things being done and should at worst require you implementing a couple things to be ducky enough.

https://github.com/altendky/qtrio/blob/0178ef6361c210bc32c129c32e1228ff4fc1c495/qtrio/_core.py#L94-L137 https://github.com/altendky/qtrio/blob/0178ef6361c210bc32c129c32e1228ff4fc1c495/qtrio/_qt.py#L56-L85

I will note lastly here that you can use a QtCore.QCoreApplication for CLI apps afaik. But sure, I'm all for relegating Qt to doing GUI work so I appreciate your efforts on this.