Skip to content

Type inference by decorator parameters instead of function refactoring #40

@cjllanwarne

Description

@cjllanwarne

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).

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions