FastAPI and SQLAlchemy 2.0: Name a More Pythonic Duo
PyMi Meetup
-
19th April 2023
About me
Education between Econ & CS (with a bit of ML)
Mostly working with web technologies and data-related applications.
Currently leading the Eng. team @ Quick Algorithm
@Giuzzilla
/in/giuliomazzanti/
Go argue with me on..
Link to this presentation
We have Python APIs
...and we are envious
Go APIs are so fast
Node.js is async by default
TypeScript frameworks are so damn.. TypeSafe
This presentation
Showing an ideal TypeScript stack
Building our Python competitor
&
If you're looking for an Async primer, watch the previous PyMI meetup on youtube!
Warnings
All roads lead to Rome. So if you know a better way let me know! (e.g. Quart, Django Ninja...)
Excalidraw sketches coming
Probably irrelevant in a few months with some cool Rust-powered framework (watch also the previous PyMI talk!)... or possibly a language model will fire us all
Digression
Why type-safety?
Less bugs / catch them sooner!
Python type hints are available since 3.5 and constantly improving
It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.
From PEP 484
Our TS-based app
Node.js + tRPC + Prisma
+ React + vite.. but less relevant
Node.js has an async event loop!
=
Our workers can spend time on useful stuff instead of waiting on I/O
"The hitchhiker's guide to asyncio." by Emanuele Fabbiani @ Past PyMi meetup
tRPC
A nice library to sync types between the front-end and the back-end!
Server-side
Client-side
Why are shared API types awesome?
Less errors
Less communication
A typed ORM!
*Logo redrawn with Excalidraw because they only had the light-mode version, don't sue me plz
*
TS-stack demo
What about Python?!?
A "micro-framework".
Untyped, Blocking I/O (sync)
The Best Python ORM
1.x
You need to perform all the validation yourself!
(of course you can fully rely on copilot)
Or use something as ugly as Marshmallow / Flask-apispec..
Your front-end dev needs to code the HTTPs calls manually!
And s/he *will* get them wrong (i.e. not type-safe)
Flask demo
Your ORM typing sucks!!
And we don't even have async?
And async?!?
Introducing...
A python framework similar to Flask but Async & with type-hints/validation
Pydantic validation and type hints!
Automatic OpenAPI & Docs generation!
Did anyone say "OpenAPI"??
FastAPI demo
Async/ASGI!
FastAPI is based on Starlette, a performant python ASGI framework.
You can use Uvicorn (uvloop) as the ASGI webserver.
Not so fast!
SQLAlchemy <1.4 doesn't implement an async interface natively. Either need to use TortoiseORM (ugh) or an adapter (e.g. encode/databases).
Still no types on SQLAlchemy < 2.x
The Bestest Python ORM
2.x
SQLA & Type hints!
Native Async support!
(from 1.4)
SQLA 2.0 demo
Conclusions
Even though the Typescript experience is more standard, Python is catching up
Watch out for updates on deeper Pydantic integrations with SQLAlchemy & SQLModel from FastAPI's creator
Pydantic 2.x is coming, with a Rust rewrite™
Questions?
PyMI - 19th April 2023
By giuzzilla
PyMI - 19th April 2023
- 254