I wanted wanted to share a package I made to enable use of type annotations for automatic validation/sanitization in Django REST Framework.<p>I've borrowed ideas from the original API Star (<a href="https://github.com/encode/apistar" rel="nofollow">https://github.com/encode/apistar</a>) (before it was an OpenAPI toolkit) and FastAPI (<a href="https://fastapi.tiangolo.com/" rel="nofollow">https://fastapi.tiangolo.com/</a>). Instagram engineering also had a recent blog post about how they do something similar with Django views (<a href="https://instagram-engineering.com/types-for-python-http-apis-an-instagram-story-d3c3a207fdb7" rel="nofollow">https://instagram-engineering.com/types-for-python-http-apis...</a>).
Looks nice! That might save 4-5 lines in beginning of the API view functions for type checks and redundant 400 returns. Title should have "Show HN" I think.
Starred.<p>Aside: If this is appealing to you (automated typing in REST) I think you would love GraphQL.<p>Typing is baked into graphql (<a href="https://graphql.org/learn/schema/#type-system" rel="nofollow">https://graphql.org/learn/schema/#type-system</a>). You can do it with graphene (<a href="https://graphene-python.org/" rel="nofollow">https://graphene-python.org/</a>) and django's graphene-djanago (<a href="https://github.com/graphql-python/graphene-django" rel="nofollow">https://github.com/graphql-python/graphene-django</a>) which handles ORM wrapping and a view baseclass.<p>Having experience with it, one major downside is how exception handling works in graphene. There is a strange implementation of promises which doesn't translate well into python IMO. It ends up hijacking error resolution, making it hard to control execution.<p>On the other hand, being able to get instant documentation from graphql is a big benefit. Everything generates nicely to a schema.graphql file.<p>Instead of POST and PUT, graphql has a concept of mutations: <a href="https://graphql.org/learn/queries/" rel="nofollow">https://graphql.org/learn/queries/</a>, GET would be a query.<p>Also, graphql has built-in pagination via Connections: <a href="https://graphql.org/learn/pagination/" rel="nofollow">https://graphql.org/learn/pagination/</a><p>I still use REST in many places, but graphql has been one thing where the learning curve paid off. I wish REST had typing baked into it from the beginning.
Shameless self plug: a couple of days ago I posted a project that does pretty much the same thing but built on top of flask instead of django:
<a href="https://github.com/plainas/flask-swagger-types" rel="nofollow">https://github.com/plainas/flask-swagger-types</a><p>Also relying on Marshmallow but with the extra bonus of generating an openapi spec for you and hosting swagger ui.<p>It puzzles me that such approach is not the norm. Interesting to see more people tackling the same problem.
Nice. We do something similar with Django (but not DRF) + Marshmallow (+ marshmallow-dataclass (+ marshmallow-dataclass-djangofield which is a very small module I wrote)).