sqlalchemy: Mypy 1.1 type errors with dataclasses
Describe the typing issue
In mypy 1.1.1 we’re seeing a lot of typing errors along the lines of:
auth.py:136: error: Argument "email" to "User" has incompatible type
"str"; expected "Mapped[str]" [arg-type]
user = User(email=email, ***
Errors are not present in mypy 1.0.0.
I suspect it has to do with some new features around dataclasses.
To Reproduce
# Base defined like:
class Base(MappedAsDataclass, DeclarativeBase):
...
Let me know if you can’t reproduce, and I’ll try to look into it further.
Versions
- SQLAlchemy: 2.0.5
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 30 (29 by maintainers)
Commits related to this issue
- document no-pep681 workarounds Mypy 1.1.1 has been released which includes a non-compliant pep-681 implementation that fails with SQLAlchemy's :class:`.MappedAsDataclass` and similar features. In ord... — committed to matusvalo/sqlalchemy by zzzeek a year ago
- document no-pep681 workarounds Mypy 1.1.1 has been released which includes a non-compliant pep-681 implementation that fails with SQLAlchemy's :class:`.MappedAsDataclass` and similar features. In ord... — committed to lelit/sqlalchemy by zzzeek a year ago
1.2 seems to surface also a few errors: https://github.com/sqlalchemy/sqlalchemy/actions/runs/4632974601/jobs/8197675058
oh yes, putting the init=False inside the Annotated is not going to work, sorry. pep-681 needs to see mapped_column(init=False) inline inside the body of the class.
seems that 1.2 will fix it: https://github.com/python/mypy/issues/14868#issuecomment-1495910814 🎉
so yes that issue reprodcues on 2.0.5 and is now fixed