英文:
How to fix mypy type Any error in sqlalchemy Table.columns
问题
以下是您要翻译的内容:
I'm still new to type hints. Here's the minimal code example of the error I'm getting:
import sqlalchemy as sa
t = sa.Table("a", sa.MetaData(), sa.Column("id_", sa.Integer))
cols = t.columns
This raises the following error when I run mypy:
error: Expression type contains "Any" (has type "ReadOnlyColumnCollection[str, Column[Any]]") [misc]
I'm running mypy with the following configuration turned on (link):
disallow_any_expr = true
I've looked at the sql alchemy source code and the .colums method of the Table class does indeed have the return type that mypy states.
I don't know however how could I go about altering that to remove the Any. Would that be even the correct approach?
请注意,我已经去掉了 HTML 实体编码,以便更清晰地阅读。
英文:
I'm still new to type hints. Here's the minimal code example of the error I'm getting:
import sqlalchemy as sa
t = sa.Table("a", sa.MetaData(), sa.Column("id_", sa.Integer))
cols = t.columns
This raises the following error when I run mypy:
error: Expression type contains "Any" (has type "ReadOnlyColumnCollection[str, Column[Any]]") [misc]
I'm running mypy with the following configuration turned on (link):
disallow_any_expr = true
I've looked at the sql alchemy source code and the .colums
method of the Table
class does indeed have the return type that mypy states.
I don't know however how could I go about altering that to remove the Any. Would that be even the correct approach?
答案1
得分: 1
如果不是您控制的源代码<sup>(a)</sup>,通常最简单的选择是在有问题的行的末尾添加# type: ignore [xxx]
。
我通常也会添加一条注释,解释为什么需要这样做,以便以后查看代码的人能够理解,因为在我们的PR过程中,我们必须为关闭检查工具mypy/pylint/bandit/isort/black
的任何操作进行理由说明。
<sup>(a)</sup> 如果修复我们的旧代码需要的工作量超过一定的阈值,我们也会遵循这个准则。例如,我们不想为了修复一个一行的错误而重构1万行的代码 但所有新的代码(只要我们控制)必须遵守这一准则。
英文:
If it's not source code that you control<sup>(a)</sup>, the easiest option is usually to drop a # type: ignore [xxx]
at the end of the offending line.
I usually also place a comment stating why it's needed so that anyone looking at the code later understands since, during our PR process, we have to justify any disabling of checks for the compliance tools mypy/pylint/bandit/isort/black
.
<sup>(a)</sup> We also follow this guideline if the effort to fix our legacy code is more than a certain threshold. For example, we don't want to have to refactor 10,000 lines of code to make a one-line bug fix All new code (that we control) has to comply however.
答案2
得分: 0
你确定你正在使用最新的SQLAlchemy版本吗?
对于我来说,当我运行mypy来测试你的代码时,我没有找到任何问题:
f.py:5: note: Revealed type is "sqlalchemy.sql.base.ReadOnlyColumnCollection[builtins.str, sqlalchemy.sql.schema.Column[Any]]"
Success: no issues found in 1 source file
当运行时:
import sqlalchemy as sa
t = sa.Table("a", sa.MetaData(), sa.Column("id_", sa.Integer))
cols = t.columns
reveal_type(cols)
- Mypy: 1.0.0
- SQLAlchemy: 2.0.2
英文:
Are you sure you are using the latest sqlalchemy version?
for me, when I run mypy to test your code, I find no issues:
f.py:5: note: Revealed type is "sqlalchemy.sql.base.ReadOnlyColumnCollection[builtins.str, sqlalchemy.sql.schema.Column[Any]]"
Success: no issues found in 1 source file
when running at:
import sqlalchemy as sa
t = sa.Table("a", sa.MetaData(), sa.Column("id_", sa.Integer))
cols = t.columns
reveal_type(cols)
- Mypy: 1.0.0
- sqlalchemy: 2.0.2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论