在SQLAlchemy(2.x)模型中更新布尔属性以满足MyPy

huangapple go评论119阅读模式
英文:

Updating a boolean property in SQLAlchemy(2.x) model while satisfying MyPy

问题

I'm here to provide the translated code portion as you requested:

  1. 我正在尝试更新我的SQLAlchemy模型中的布尔属性并且我希望确保MyPy也满意这段代码但是当我尝试更新属性时MyPy会给我一个错误这是错误消息
  2. ```python
  3. dashing/db/dao/workspace_dao.py:69: error: 赋值中的类型不兼容
  4. (表达式类型为 "bool"变量类型为 "Column[bool]") [赋值]
  5. workspace.is_archived = new_value
  6. ^~~~~~~~~
  7. 1 个文件中找到 1 个错误检查了 57 个源文件

这是我用于更新属性的函数:

  1. async def archive_workspace(
  2. self,
  3. workspace_id: UUID,
  4. new_value: bool,
  5. ) -> Optional[WorkspaceModel]:
  6. workspace = await self.session.get(WorkspaceModel, workspace_id)
  7. if workspace is None:
  8. return None
  9. workspace.is_archived = new_value # ← MyPy不喜欢这个赋值
  10. await self.session.commit()
  11. return workspace

这是我的模型定义:

  1. class WorkspaceModel(Base):
  2. __tablename__ = "workspace"
  3. ...
  4. is_archived = Column(Boolean, unique=False, default=False)

如何正确更新我的SQLAlchemy模型中的布尔属性,以便MyPy不会引发任何错误?

  1. Please note that the translation may not be exact due to differences in code formatting between English and Chinese, but it conveys the same information.
  2. <details>
  3. <summary>英文:</summary>
  4. 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&#39;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)

  1. This is the function I am using to update the property:
  2. ```python
  3. async def archive_workspace(
  4. self,
  5. workspace_id: UUID,
  6. new_value: bool,
  7. ) -&gt; Optional[WorkspaceModel]:
  8. workspace = await self.session.get(WorkspaceModel, workspace_id)
  9. if workspace is None:
  10. return None
  11. workspace.is_archived = new_value # ← MyPy does not like this assignment
  12. await self.session.commit()
  13. return workspace

And here is my model definition:

  1. class WorkspaceModel(Base):
  2. __tablename__ = &quot;workspace&quot;
  3. ...
  4. 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?

答案1

得分: 1

@python_user 在他的评论中提到的,我之前在使用旧的语法。使用SQLAlchemy 2.0的语法修复了这个问题:

  1. from sqlalchemy.orm import Mapped, mapped_column
  2. class WorkspaceModel(Base):
  3. __tablename__ = &quot;workspace&quot;
  4. ...
  5. is_archived: Mapped[bool] = mapped_column(unique=False, default=False)
英文:

As @python_user mentioned in his comment, I was using old syntax. Using SQLAlchemy 2.0 syntax fixes the issue:

  1. from sqlalchemy.orm import Mapped, mapped_column
  2. class WorkspaceModel(Base):
  3. __tablename__ = &quot;workspace&quot;
  4. ...
  5. is_archived: Mapped[bool] = mapped_column(unique=False, default=False)

huangapple
  • 本文由 发表于 2023年5月24日 17:32:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定