VSCode Intellisense for SQLAlchemy query result

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

VSCode Intellisense for SQLAlchemy query result

问题

在第3行的代码中,customer.age = 50,可能是因为在前一行中,VSCode无法推断first()方法的返回类型,所以Intellisense不起作用。

您可以尝试显式地使用类转换或其他解决方法,以使类Customer的方法和字段在Intellisense中对其对象customer可用。

编辑1:

我已经像这样声明了类:

class Customer(base):
    __tablename__ = "customers"
    name = Column(String, primary_key=True, unique=True)
    age = Column(Float, nullable=False)

在VSCode的Intellisense中,它仅显示Customer的dunder字段。它不显示nameage字段。

编辑2:

在我的VSCode中,已安装了以下与Python相关的扩展:

  1. Pylance
  2. Python
  3. Python Environment Manager
  4. Python Extension Pack
  5. Python Indent
英文:

Below is a code fragment for querying a table using SQLAlchemy and reading the first record:

session = Session()
customer = session.query(Customer).filter(Customer.name == "John").first()
customer.age = 50

In the 3rd line of code customer.age = 50, the VSCode Intellisense does not work, perhaps because VSCode cannot infer what will be the return type of first() method in the previous line.

Is there a way I can use the class cast explicitly or any other workaround, so that the methods and fields of the class Customer become available for its object customer in Intellisense?

EDIT 1:

I have declared class like this:

class Customer(base):
    __tablename__ = "customers"
    name = Column(String, primary_key=True, unique=True)
    age= Column(Float, nullable = False)

In the VSCode Intellisense, it is showing only the dunder fields of Customer. It is not showing the name and age fields.

Below is the screenshot how it is coming for me in VSCode:

VSCode Intellisense for SQLAlchemy query result

EDIT 2:

In my VSCode, the following Python related extensions are installed:

  1. Pylance
  2. Python
  3. Python Environment Manager
  4. Python Extension Pack
  5. Python Indent

答案1

得分: 1

尝试:

customer: Customer = session.query(Customer).filter(Customer.name == "John").first()

有了这个明确的类型提示,自动完成可能会起作用。

英文:

Try:

customer: Customer = session.query(Customer).filter(Customer.name == "John").first()

With this explicit type hint, the auto completion is likely to work.

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

发表评论

匿名网友

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

确定