Hi All,
I've hit a little deadlock.
We want to use SQLAlchemy 2.x ORM typing (Mapped[...] =
mapped_column(...)) with
PEP 604 union types (int | None) in model annotations and must support
Python 3.9 (which does not support int | None at runtime).
We do not want to use Optional[...] (ruff UP007 is enforced). And, we
already have from __future__ import annotations everywhere. And getting
error like:
sqlalchemy.orm.exc.MappedAnnotationError: Could not resolve all types within
mapped annotation: "Mapped[int | None]"
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
mypy/ruff are not happy with Mapped[Optional[int]] (*ruff UP007*), and
SQLAlchemy 2.x cannot parse Mapped[int | None] on Python 3.9.
With from __future__ import annotations, SQLAlchemy stores the annotation
as a string. At runtime, it tries to eval() the string in the module's
namespace. On Python 3.9, eval("int | None") fails.
Need help and suggestion on how shall I proceed?
Thanks,
Avi