Reputation: 1540
I am trying to update a boolean property in my SQLAlchemy model and I want to make sure MyPy is satisfied with the code as well. However, MyPy is giving me an error when I try to update the property. Here's the error message:
dashing/db/dao/workspace_dao.py:69: error: Incompatible types in assignment
(expression has type "bool", variable has type "Column[bool]") [assignment]
workspace.is_archived = new_value
^~~~~~~~~
Found 1 error in 1 file (checked 57 source files)
This is the function I am using to update the property:
async def archive_workspace(
self,
workspace_id: UUID,
new_value: bool,
) -> Optional[WorkspaceModel]:
workspace = await self.session.get(WorkspaceModel, workspace_id)
if workspace is None:
return None
workspace.is_archived = new_value # ← MyPy does not like this assignment
await self.session.commit()
return workspace
And here is my model definition:
class WorkspaceModel(Base):
__tablename__ = "workspace"
...
is_archived = Column(Boolean, unique=False, default=False)
What is the correct way to update the boolean property in my SQLAlchemy model so that MyPy does not raise any errors?
Upvotes: 1
Views: 2042
Reputation: 1540
As @python_user mentioned in his comment, I was using old syntax. Using SQLAlchemy 2.0 syntax fixes the issue:
from sqlalchemy.orm import Mapped, mapped_column
class WorkspaceModel(Base):
__tablename__ = "workspace"
...
is_archived: Mapped[bool] = mapped_column(unique=False, default=False)
Upvotes: 3