SQLAlchemy引擎和会话对象的类型提示

时间:2019-05-13 18:37:52

标签: python sqlalchemy type-hinting mypy typehints

我正在尝试向我的SQLAlchemy脚本添加类型提示:

connection_string: str = "sqlite:///:memory:"
engine = create_engine(connection_string)
session = Session(bind=engine)
reveal_type(engine)
reveal_type(session)

我已经针对mypy运行了此脚本,但是两种类型都以Any的形式返回。 enginesession变量应该是什么类型?

1 个答案:

答案 0 :(得分:1)

想通了:

connection_string: str = "sqlite:///:memory:"
engine = create_engine(connection_string)
session = Session(bind=engine)
print(type(engine))   # sqlalchemy.engine.base.Engine
print(type(session))  # sqlalchemy.orm.session.Session