-
-
Notifications
You must be signed in to change notification settings - Fork 346
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 alternative APIs that improve typing and tooling support (e.g. autocompletion) #2208
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2208 +/- ##
==========================================
- Coverage 99.51% 99.47% -0.04%
==========================================
Files 114 114
Lines 14759 14789 +30
Branches 2344 2346 +2
==========================================
+ Hits 14687 14711 +24
- Misses 47 56 +9
+ Partials 25 22 -3
|
I have no real comments about the implementation of this but I am absolutely, vehemently against the concept of making libraries work around Python's anemic type system. This should be fixed upstream, by making |
It should also be noted that trio, for reasons beyond my understanding, does not want type annotations in the actual codebase, but instead in trio-typing. |
This isn't true, for what it's worth. It's just that adding types after the fact is hard and the efforts to merge type hints stalled, see #1873. I'd like to add that trying to do everything in one pull request is highly likely to fail. If anyone wants to work on adding types, they should do it gradually, like urllib3 did: https://sethmlarson.dev/blog/tests-arent-enough-case-study-after-adding-types-to-urllib3 |
Once PEP 646 (Variadic Generics) is implemented in checkers, the current APIs can be typed directly so this API change isn't as necessary. |
Closing this since subsequent PEPs mean we'll be able to precisely type-annotate the current signatures 🙂 |
I would love to see this merged! |
We have improved the type annotations to the code now, so all of the effects of this PR should be live afaik. |
Can you elaborate? Documentation |
You can pass either a |
This interface would be much more convenient. |
✨ Add alternative APIs that improve typing and tooling support (e.g. autocompletion in editors)
Description
I want to propose adding a new API style for three functions (additional to the current API) that would take advantage of new type annotations (PEP 612 - Parameter Specification Variables), to get better tooling and editor support.
In particular, this would allow/support mypy checks, inline editor errors, and ✨ autocompletion ✨ for positional and keyword arguments for:
nursery.start_soon()
)to_thread.run_sync()
)from_thread.run()
)This would also allow mypy and editors to provide type checks, autocompletion, etc. for the return values received when sending tasks to a worker thread and when receiving them from a worker thread:
to_thread.run_sync()
from_thread.run()
Previous Discussion
I proposed this in AnyIO here: agronholm/anyio#403
I was pointed out at the discussion here: #470
That discussion revolves mainly around accepting keyword arguments for functions called, and this is also related to that. But one of the main things I want from this is better tooling and editor support: autocompletion and inline errors... not only supporting kwargs. So I thought it was worth sharing this additional perspective.
Usage
The main difference is that instead of having a function that takes the function to call plus:
...this new API only takes the function to call plus Trio-specific configs (like the task name), and then returns another function that takes the positional and keyword arguments. And when that function is called, it does the actual work. So it's used like this:
And now it also supports keyword arguments, not only positional ones, so it's not necessary to use (and understand) partials to use keyword arguments:
...but the main benefit, from my point of view, is actually better typing/tooling support. Autocompletion and inline type errors in the editor and mypy.
Examples
Nursery
To Thread (asyncify)
From Thread (syncify)
Function names Bikeshedding
I wanted to have short function names that were descriptive enough, that's why the "somethingify".
I thought any more descriptive variant like
generate_async_function_that_calls_run_sync
would be too long. 😅My rationale is that if a user wants to call a blocking function while inside async stuff, the user might want to have a way to "asyncify" that blocking function. And also the "somethingify" doesn't imply it is being executed/called right away, but that something is "converted" in some way. I would hope that would help with the intuition that it just returns another function that is then called, so it's necessary to add the extra chained parenthesis with any possible needed arguments.
But of course, I can change the names if there's another preference.
Tests and Docs
I want to gather feedback first before writing tests and docs, but I'll add them if this or something like this is acceptable.
Type Annotations
I'm also adding the bare minimum type annotation stuff to make it all work with mypy.
Note on AnyIO
I want to have this on AnyIO: agronholm/anyio#403, but AnyIO would want any decision to be taken here first, so I'm making the PR here as well, even if just as a conversation starter.
Note on FastAPI
I want to have this type of interface for users and myself, I use autocompletion and tooling/types a lot. I thought of adding something like this to FastAPI itself, but I think this would really belong here in Trio and AnyIO, not in FastAPI.
I also intend to add it to the FastAPI docs to explain "advanced" use cases with concurrency and blocking code in threads run from async code, etc. I would like to document all those things in FastAPI referring to and explaining these libraries directly (AnyIO, Trio), instead of writing wrappers in FastAPI or anything else. 🤓
As I see this (in particular taking kwargs) is quite debated,
I'm considering buildingI built a small package (Asyncer) just with those wrappers, documenting it as highly experimental and subjective (to my point of view). Maybe that could help gauge how useful that API style is for Trio and AnyIO before making any commitments. I don't like the feel of "yet another library", but it might help separate what is more stable (Trio and AnyIO) and what is a Frankenstein temporary experiment (this idea I'm proposing).2022-01-08 Edit: I built Asyncer to try out this API design. 🤓