Release constraint that provider parameters must be declared before non-provider ones
Closed this issue · 0 comments
florimondmanca commented
We have put this constraint in for simplicity, but this is a blocker when non-provider parameters cannot be converted to provider ones, and we want to keep an existing API in place.
More specifically, I am encountering this issue while trying to integrate Bocadillo WebSocket views with aiodine: the first parameter is ws
and we can't make it a provider parameter without greatly sacrificing readability.
Implementation idea:
- The list of positional providers should be the same size as the number of positional arguments of the consumer. Just use
None
(or a special sentinel value) if no provide is available. - Merge the list of positional providers with the arguments given at runtime, inserting non-provider arguments where the list contains the sentinel.
- If the callee missed to pass all required positional arguments, sentinels will remain in the list. Crop it so that no sentinels remain, and pass the cropped args to the function. It will raise a "not enough arguments" exception to the callee, as expected.
This should allow to do something like:
import aiodine
@aiodine.provider
async def foo():
return "foo"
@aiodine.consumer
async def consume(bar, foo, baz):
print(bar, foo, baz)
async def main():
await consume("bar", "baz") # "bar foo baz"