Repository navigation
feat: make Signal and SignalInstance generic - #446
Draft
jacopoabramo wants to merge 7 commits into
Draft
jacopoabramo wants to merge 7 commits into
jacopoabramo wants to merge 7 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.76% 98.80% -0.97%
==========================================
Files 23 24 +1
Lines 2559 2506 -53
==========================================
- Hits 2553 2476 -77
- Misses 6 30 +24 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Author
|
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... |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.