from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String
Base = declarative_base()
classUser(Base): __tablename__ = "users" id = Column(Integer, primary_key=True) name = Column(String)
defprint_username(name: str): print(name)
if __name__ == "__main__": user = User(id=42, name=42) print_username(user.name)
Pyright 认真负责给出如下报错提示:[Pyright reportGeneralTypeIssues] Argument of type "Column[str]" cannot be assigned to parameter "name" of type "str" in function "print_username" [E]
Pyright 可正确识别 user 实例的 name 属性为 str 类型,而不是代码定义的 Column 类型。
小技巧:无需引用,直接使用 reveal_type 方法调试类型:
WHY???
为什么安装 stub 包后,无需任何配置,即可直接对 pyright 生效?
Try to resolve using stubs or inlined types found within installed packages.
For a given package, try to resolve first using a stub package. Stub packages, as defined in PEP 561, are named the same as the original package but with “-stubs” appended.