aio-libs / aio-libs/aiohttp-apischema
Type inference by decorator parameters instead of function refactoring
- Lenguaje dominante
- Python
- Estrellas
- 12
- Forks
- 4
- Merge medio
- 4 min
- PR fusionados (30 d)
- 2
Descripción
Hello and thanks for a great project!
I wondered whether there's any interest in support for type inference at the decorator level rather than by changing the return type of methods?
Right now I have a set of methods that I'd like to create a schema for, but refactoring them all to return `APIResponse` is going to be non-trivial for various reasons (see all the various routes in our [front_end.py](https://github.com/hail-is/hail/blob/main/batch/batch/front_end/front_end.py)).
Some reasons include -
- Non-json return types
- StreamResponses from the APIs
- A set of decorators and helper functions that mutate the parameter lists and return types
It seems like it might be easier if there were a style of decoration that allowed us to document the parameters and return types without having to refactor the server code at all. Perhaps something like -
```
@SCHEMA.api(parameters=[], response=str)
@routes.get('/api/v1alpha/version')
async def rest_get_version(_) -> web.Response:
return web.Response(text=version())
```
Right now I'm forced to refactor the code itself to look like -
```
@SCHEMA.api()
@routes.get('/api/v1alpha/version')
async def rest_get_version(_) -> APIResponse[str, Literal[200]]:
return APIResponse(version())
```
This is easy enough in this case, but even here the version gets "json-ified" (see #39). It would be nice if I could document the API without having to make any refactors to the code itself.
Thank you!
Guía de contribución
Evaluación
Este issue todavía no se ha evaluado.