Closed
Description
Describe the bug
I am running some tests using pytest and I got the following exception from sqlalchemy with Python 3.6 only when the contextvars
module is installed: AttributeError: This Python interpreter does not support context variables
I understand Context Variables is a new feature introduced in Python 3.7 and then I noticed one of my project dependency, httpx
, installed this backport of Context Variables: https://pypi.org/project/contextvars/
Expected behavior
I expect to run the script below using Python 3.6 with the contextvars
module installed.
To Reproduce
Setup a virtual env and install the dependencies:
python3.6 -m venv python3.6-venv
source python3.6-venv/bin/activate
python3 -m pip install pytest==6.2.2 pytest-asyncio==0.14.0 sqlalchemy==1.4.4 aiosqlite===0.17.0 contextvars==2.4
Create a new script test.py
containing this code:
import pytest
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
@pytest.fixture
async def db_session():
engine = create_async_engine(
"sqlite+aiosqlite:///:memory:",
connect_args={"check_same_thread": False},
future=True
)
session = AsyncSession(engine)
try:
yield session
finally:
await session.close()
@pytest.mark.asyncio
async def test_db_session(db_session):
assert db_session
Launch the test with python3 -m pytest test.py
Error
===================================================================================== test session starts ======================================================================================
platform linux -- Python 3.6.13, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/grossmj/PycharmProjects/gns3-server, configfile: pytest.ini
plugins: asyncio-0.14.0, timeout-1.4.2
collected 1 item
test.py .E [100%]
============================================================================================ ERRORS ============================================================================================
_____________________________________________________________________________ ERROR at teardown of test_db_session _____________________________________________________________________________
@pytest.fixture
async def db_session():
engine = create_async_engine(
"sqlite+aiosqlite:///:memory:",
connect_args={"check_same_thread": False},
future=True
)
session = AsyncSession(engine)
try:
yield session
finally:
> await session.close()
test.py:19:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
python3.6-venv/lib/python3.6/site-packages/sqlalchemy/ext/asyncio/session.py:326: in close
return await greenlet_spawn(self.sync_session.close)
python3.6-venv/lib/python3.6/site-packages/sqlalchemy/util/_concurrency_py3k.py:96: in greenlet_spawn
context = _AsyncIoGreenlet(fn, greenlet.getcurrent())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <_AsyncIoGreenlet object at 0x7fa40a7ba638 (otid=0x7fa40b5094c8) pending>, fn = <bound method Session.close of <sqlalchemy.orm.session.Session object at 0x7fa40c4cb5f8>>
driver = <greenlet.greenlet object at 0x7fa40b492768 (otid=0x7fa40c4fdf78) current active started main>
def __init__(self, fn, driver):
greenlet.greenlet.__init__(self, fn, driver)
self.driver = driver
if _copy_context is not None:
> self.gr_context = _copy_context()
E AttributeError: This Python interpreter does not support context variables
python3.6-venv/lib/python3.6/site-packages/sqlalchemy/util/_concurrency_py3k.py:32: AttributeError
-------------------------------------------------------------------------------------- Captured log setup --------------------------------------------------------------------------------------
DEBUG asyncio:selector_events.py:54 Using selector: EpollSelector
=================================================================================== short test summary info ====================================================================================
ERROR test.py::test_db_session - AttributeError: This Python interpreter does not support context variables
================================================================================== 1 passed, 1 error in 0.17s ==================================================================================
Versions.
- OS: Linux Ubuntu 20.04 LTS
- Python: 3.6
- SQLAlchemy: 1.4.4
- Database: sqlite
- DBAPI: aiosqlite
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
zzzeek commentedon Mar 31, 2021
this issue stems from the PR for greenlet at python-greenlet/greenlet#198 . Since you are on a very old Python version you can work around this by pinning greenlet to 0.4.17.
Demonstration of the issue. Even though this uses the third party contextvars directly, greenlet's compatibility check is specific to the Python version in use so this only reproduces on an older Python
for our end we'd need to do a py3 compatibility check.
zzzeek commentedon Mar 31, 2021
patch:
grossmj commentedon Mar 31, 2021
Thanks for the work around, it solved my problem 👍
sqla-tester commentedon Apr 1, 2021
Federico Caselli has proposed a fix for this issue in the master branch:
Prevent loading contextvars in python 3.6 https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2705