how to have mypy spot wrong ORM object creations with sqlalchemy 2.1
11:56 02 Nov 2026

I recently installed moved to sqlalchemy 2. I'm using mypy in my project for static type checking. I followed the doc here https://docs.sqlalchemy.org/en/21/changelog/whatsnew_20.html#step-one-orm-declarative-base-is-superseded-by-orm-declarativebase to get rid of the mypy plugin. My code checks successfully but I noticed that mypy does not complain anymore when creating ORM object with non existing columns or mistyped columns, as below (last two lines):

from sqlalchemy import Integer, String
from sqlalchemy.orm import DeclarativeBase, mapped_column, Mapped

class Base(DeclarativeBase):
    pass

class Usr(Base):
    __tablename__ = "usr"
    usr_id: Mapped[int] = mapped_column(Integer, primary_key=True)
    usr_code: Mapped[str] = mapped_column(String, nullable=False, unique=True)

Usr(usr_id="bliblu")  # no complaint on type error
Usr(usr_codexxx="bliblu")  # no complaint on non existing column

The execution fails:

$ python test.py
...
TypeError: 'usr_codexxx' is an invalid keyword argument for Us

Here is the output of mypy:

$ mypy --strict test.py
Success: no issues found in 1 source file

and the versions of sqlalchemy and mypy used:

$ pip list | grep -i "mypy\|sqlalchemy"
mypy                                     2.4.1
mypy_extensions                          1.1.0
SQLAlchemy                               2.1.6
SQLAlchemy-Utils                         0.41.2

Is it possible to have mypy raise errors on these two lines ?

python sqlalchemy mypy