Repository navigation
feat: make Signal and SignalInstance generic - #446
jacopoabramo wants to merge 6 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #446 +/- ##
==========================================
- Coverage 99.68% 99.68% -0.01%
==========================================
Files 23 23
Lines 2559 2522 -37
==========================================
- Hits 2551 2514 -37
Misses 8 8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
I pinged mypy to ask when a new release is expected with the required fix. They're pushing for more frequent releases in the last few months so maybe we're lucky... |
846d477 to
6c42124
Compare
|
Pushed two commits fixing a regression this branch introduced: 5d6f391 types untyped signals as
5cd34a4 rewrites the new checks in Remaining difference from |
Assisted by Opus 5.5 (code is generated by it and reviewed by me, PR text is my own)
Supersedes #304
This PR makes
SignalandSignalInstancegeneric over the types they emit, soSignal(int)givesSignalInstance[int]on instances, and type checkers now checkconnect,emit,emit_fastand__call__against those types.Signal(...)supports construction with up to 5 types, unions included.Apparently, the problem is in Python's typing system, and we can't support any number of types. i.e.
The approach suggested in PEP 646 is one overload per number of types. #304 did this for
connect; here the constructor does it too, so that the parameters are the emitted types rather thantype[...]. The limit of 5 is theMAX_ARGS#304 already used. The_connector.pyistub holds the annotations the overloads share.Right now it focuses only on
SignalandSignalInstance. I deferred typing the rest of the API (SignalGroup, evented containers, etc.) to a later PR.While reviewing this work I realized why I started #445 first: a generic class needs
TypeVarTupleandUnpackat runtime,typinghas them only from 3.11, and psygnal has no runtime dependencies.This PR keeps 3.10 and adds no dependency: on 3.10 the two names come from
typing_extensionswhen it is installed. Without it, on 3.10 only, the classes accept a single type argument at runtime (SignalInstance[int, str]raisesTypeError); static checking is not affected.That fallback lives in a new
_typing.pymodule, excluded from the mypyc build because mypyc cannot run anelsebranch afterif TYPE_CHECKING(mypyc/mypyc#1159). Requiring Python 3.11 would remove the module and the limitation, so up to you.At this time the PR cannot be merged, because the mypyc build needs python/mypy#22169, which is not in a release yet. So we still have to wait on that before making a decision. Locally, with mypyc cloned from main the build works.
You'll notice that the overloads of
Signal.__init__andSignalInstance.connecttake their keyword arguments as typed dictionaries (**kwargs: Unpack[...]).The intention was to keep the overloads readable, because otherwise every overload repeats the full keyword list, which is a huge amount of text. I'm not particularly happy with it, so feel free to offer an alternative (I'm guessing right now the only alternative is to keep the overloads as they are and drop the typed dictionaries... wordy but it'll be the truth about the types).
My concern is: is there anybody who's using more than 5 types to declare signals? Of that I have no clue.
The first commit is unrelated: it makes the
typingCI job installpytest-mypy-pluginson Python 3.14, where it was being skipped.