-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Demo & RFC: Add return type hints to read_evokeds()
#12249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
f44e6e1 to
34ca4bb
Compare
read_evokeds()read_evokeds()
|
Please note that I added 2 commits here. The second one uses an overload for |
This comment was marked as outdated.
This comment was marked as outdated.
949e193 to
3edd1c3
Compare
|
Although such cases will hopefully be rare, this example shows that our API should probably be simplified. Why don't we take this opportunity to e.g. always return a |
|
I agree that this particular overload may be indicative of an API design we may want to reconsider. |
|
-1, not worth the hassle to end users |
|
I think the UX would be greatly improved and from a user's perspective, is not different from the example with Right now when I read evokeds, my IDE doesn't know which data type is actually being returned and I have to manually insert type guards in my code. I tend to agree with @cbrnr that the "hassle" we're looking at here (overloads) might just be indicative of a sub-optimal API design in the first place |
|
Agreed it's suboptimal, but to me the code churn we will force upon people isn't worth it |
|
The simplified solution for typing this function would be to annotate the return type as the union of Evoked and list of Evoked. At least this would make things clearer when viewing the signature in an IDE. No overloads required. Thoughts? |
|
I don't really understand / haven't used overloads but if you mean having this be the signature: then +1 from me, seems simple and reflective of current behavior. |
It's just like multiple dispatch in other languages. You specify what goes into the function and what comes out. You repeat that for all input types / parameter values that yield a different function return type. So that in the example above, when you ask the function to read only a single condition, the type checker knows to expect an Evoked; otherwise, a list of Evoked. The overloads don't change runtime behavior, in case that's something you assumed.
Yes, this is what I meant. It makes the UX slightly better but would still require type guards in user code. Because the return type now is "Evoked or a list of Evoked", hence without inserting an But at least VS Code won't keep telling me that this function returns "Unknown or a list of Unknown" anymore, which is already a slight improvement. |

x-ref #12243
Here I'm demonstrating a slightly tricky case for type hints: the value of a parameter determines the function's return value. I just wanted to show to you what this looks like, so we can evaluate whether this is acceptable.
MWE:
main:This PR:


cc @cbrnr @agramfort @larsoner @drammock