question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Asynchronous sessionmaker initialization type error

See original GitHub issue

Describe the bug mypy fails on asynchronous sessionmaker initialization.

Expected behavior No errors.

To Reproduce Please try to provide a Minimal, Complete, and Verifiable example. See also Reporting Bugs on the website, and some example issues.

Please do not use Flask-SQLAlchemy or any other third-party extensions or dependencies in test cases. The test case must illustrate the problem without using any third party SQLAlchemy extensions. Otherwise, please report the bug to those projects first.

import os

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import sessionmaker

engine = create_async_engine(os.getenv("DATABASE_URL"))
async_session = sessionmaker(engine, class_=AsyncSession)

Error

session.py:8: error: Value of type variable "_TSessionMakerType" of "sessionmaker" cannot be "AsyncSession"
Found 1 error in 1 file (checked 1 source file)

Versions.

  • OS: Linux
  • Python: 3.9.6
  • SQLAlchemy: 1.4.23
  • mypy: 0.910
  • SQLAlchemy2-stubs: v0.0.2a9

Additional context The problem appeared on the latest release of sqlalchemy2-stubs and was not present on v0.0.2a8.

Have a nice day!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
zzzeekcommented, Sep 23, 2022

@mjpieters I am hoping 2.0 betas are very soon. that’s where these issues if you have them will be permanently fixed so i hope you have time to test a bit.

1reaction
mjpieterscommented, Sep 23, 2022

This appears to have regressed? I’m using version 0.0.2a27, and the example in this issue now produces:

Argument of type "Type[AsyncSession]" cannot be assigned to parameter "class_" of type "Type[_TSessionMakerType@sessionmaker]" in function "__init__"
  Type "Type[AsyncSession]" cannot be assigned to type "Type[_TSessionMakerType@sessionmaker]"

when using Pyright.

I noticed that almost the same error message appears when using sqlalchemy.create_engine() and sqlalchemy.orm.Session:

Argument of type "Type[Session]" cannot be assigned to parameter "class_" of type "Type[_TSessionMakerType@sessionmaker]" in function "__init__"
  Type "Type[Session]" cannot be assigned to type "Type[_TSessionMakerType@sessionmaker]"
Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronous I/O (asyncio) - SQLAlchemy 1.4 Documentation
This is useful when initializing a series of objects which involve existing database queries, where the uncompleted object should not yet be ...
Read more >
Correct usage of session with asynchronous servers in ...
Background: We have a Python web application which uses SqlAlchemy as ORM. We run this application with Gunicorn(sync worker) currently. This ...
Read more >
Async SQLAlchemy with FastAPI
In base.py we will initialize the new SQLAlchemy engine with create_async_engine() and create an async session maker by passing it the new ...
Read more >
Async SqlAlchemy with FastAPI: Getting single session for all ...
The problem appears when I try to execute multiple requests, an error appears that seems to indicate that the same session is being...
Read more >
FastAPI with Async SQLAlchemy, SQLModel, and Alembic
SQLModel. Next, let's add SQLModel, a library for interacting with SQL databases from Python code, with Python objects. Based on Python type ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found