SQLAlchemy 2.0 ORM在Pycharm中筛选时显示错误的类型。

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

SQLAlchemy 2.0 ORM filter show wrong type in Pycharm

问题

我正在使用Pycharm开发一个使用SQLAlchemy 2.0的应用程序。

当我尝试使用ORM方法查询某个表时,Pycharm总是显示过滤查询中的类型错误。

例如,在下面的代码片段中:

    with Session(engine) as session:
        session.scalars(select(Albums.AlbumId).where(Albums.Id > user_last_sync_id))
                                                              ^^^ 显示错误类型

会得到以下消息:

期望类型为'ColumnElement[bool] | _HasClauseElement | SQLCoreOperations[bool] | ExpressionElementRole[bool] | () -> ColumnElement[bool] | LambdaElement',但实际得到的是'bool'

SQLAlchemy 2.0 ORM在Pycharm中筛选时显示错误的类型。

尽管它指示了类型错误,但脚本仍然会执行(并获取正确的数据),而不显示任何错误消息。

在代码中可能导致此问题的原因是什么?有没有办法使代码更加“正确”,以便Pycharm不显示类型错误?

英文:

I'm using Pycharm to develop an app with SQLAlchemy 2.0.

When I attempt to query some table using ORM approach. Pycharm always display type error in the filter query.

For example, in the code snippet below:

    with Session(engine) as session:
        session.scalars(select(Albums.AlbumId).where(Albums.Id > user_last_sync_id))
                                                              ^^^ Show wrong type

Get the following message

Expected type 'ColumnElement[bool] | _HasClauseElement | SQLCoreOperations[bool] | ExpressionElementRole[bool] | () -> ColumnElement[bool] | LambdaElement',, got 'bool' instead

SQLAlchemy 2.0 ORM在Pycharm中筛选时显示错误的类型。

Even though it indicates a type error, but scripts still be executed (And get correct data) without showing any error messages.

What could potentially be causing this issue in the code? Is there a way to make code more "correct" to let Pycharm not to display type error?

答案1

得分: 3

PyCharm假设形式为a > b的表达式在没有其他类型信息可用时会求值为bool。很可能,SQLAlchemy没有提供足够丰富的类型提示和/或存根文件,以使PyCharm能够正确推断该表达式的类型。

要解决这个警告,你可以使用typing.cast来告诉PyCharm表达式的真实类型:

.where(
    cast("ColumnElement[bool]", Albums.Id > user_last_sync_id)
)

另外,你可以通过在行末添加# type: ignore来忽略该警告。

英文:

PyCharm assumes that expressions of the form a > b evaluate to bool when no other type information is available. Most likely, SQLAlchemy isn't providing rich enough type hints and/or stubfiles for PyCharm to correctly infer the type of that expression.

To resolve that warning, you can inform PyCharm about the true type of the expression using typing.cast:

.where(
    cast("ColumnElement[bool]", Albums.Id > user_last_sync_id)
)

Alternatively, you can suppress the warning by adding # type: ignore to the end of the line.

答案2

得分: 1

我正在查看这个确切的问题,并遇到了这个问题。感谢您的报告。@Brian61354270是正确的,如果您安装了插件,pycharm和mypy会对等式的类型进行投诉。

我刚刚尝试安装了存根,警告就消失了。

pip install sqlalchemy-stubs
英文:

I was looking to this exact problem and i came across this issue. Thanks for reporting it. @Brian61354270 is right, pycharm and mypy (if you have the plugin) will complain about type of the equal expression.

I just tried to install the stubs and the warning is just gone away

pip install sqlalchemy-stubs

huangapple
  • 本文由 发表于 2023年8月9日 03:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862713.html
匿名

发表评论

匿名网友

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

确定