-
Notifications
You must be signed in to change notification settings - Fork 213
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
Add ParamSpec typing for AsyncToSync and SyncToAsync #373
Conversation
After doing the work, I realised there was at least one existing draft PR in #298 , albeit a couple of years old now. The approach we took seems pretty similar; I've included at least one change to my PR from the feedback in that PR's comments. |
|
96fde17
to
10196f6
Compare
Feedback welcomed, I haven't done ParamSpec generics on a publicly used project before...I am not 100% sure if this will play well with users' deployments, but I've tested it with my own projects and seems okay so far. Also, to link it into the original issue - this is to fix #270 |
One comment that I think applies to both of these implementations — I’ve been using this to work around the lack of typing and it works well:
|
Thanks for the suggestion Mark! For Again, let me know if I'm missing something or misunderstood you here. Feel free to annotate the PR changes with specific comments if you want, as well. |
Got it, that makes sense. Obviously I didn’t look closely enough at your diff yet. I’ll comment directly on it if I see anything. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with the idea that this will need to be a minor release due to the mypy dep change.
I've looked over it and it all seems to make sense to me; I've not dabbled with ParamSpec more than a few times, but the arguments being passed through without modification makes this simpler than the times I have.
My one question - is there a way to write a test for this? Obviously a standard unit test is not going to work, but something that fails the typechecker if it's not correct might be possible.
Good question; I'd thought about this in passing but now you've prompted me to go looking- upon a little research found that Sobolevn wrote about it a while back and mentions complex generics specifically - https://sobolevn.me/2019/08/testing-mypy-types I've started experimenting with it, and already found some issues with the return types! I'll add some tests to the PR. |
Adds pytest plugin pytest-mypy-plugins. Adds two basic examples of simple tests.
This uses typing-extensions to add ParamSpec typing to the decorators and their related classes sync_to_async and async_to_sync. The typing-extensions dependency needed to be extended to python 3.9, to provide backported support for ParamSpec.
The ParamSpec code requires a minimum mypy version. Upon some manually testing, it seems 0.991 is the minimum version that works, so update the setup.cfg accordingly.
typing_extensions isn't a dependency above py3.9, so make the import conditional, and for py3.10 and newer, use typing.ParamSpec instead.
Add a first test to verify the PR, which currently fails.
b79ecb1
to
0d325e7
Compare
To sum up my findings so far:
I'll take a further look in the next couple of days as time allows and try to figure out what the issue is. And sorry, I might've made a mess of the review history due to a force-push or two while I was trying to wrangle the commit history. |
We do this on Starlette: https://github.com/encode/starlette/blob/master/tests/test_config.py#L12-L30 |
Thanks, that does avoid the extra dependency at least, I'll explore that as an alternative too. |
This uses typing-extensions to add ParamSpec typing to the decorators and their related classes sync_to_async and async_to_sync.
The typing-extensions dependency needed to be extended to python 3.9, to provide backported support for ParamSpec.
Also found through some manual testing that the minimum mypy version that works with ParamSpec is 0.991. Maybe this means this PR needs to wait for at least an asgiref minor release, as it will affect the version of mypy people need to be using.