Skip to content

Instantly share code, notes, and snippets.

@karlicoss
Created October 6, 2020 23:02
Show Gist options
  • Save karlicoss/dcb5ecc446029f0fb9c92343a6744f63 to your computer and use it in GitHub Desktop.
Save karlicoss/dcb5ecc446029f0fb9c92343a6744f63 to your computer and use it in GitHub Desktop.
sqlalchemy + typing + loggger WTF????
### NOTE: you need to keep hack_logger.py and main.py in separate files, otherwise it's not reproducing
### hack_logger.py
# NOTE: if you comment this out, it works fine
import typing
def enabled_for(*args, **kwargs):
return True
import logging
logger = logging.getLogger('whatever')
# NOTE: if you comment this out, it works fine
logger.isEnabledFor = enabled_for
###
### main.py
from typing import List
from sqlalchemy import Column
# NOTE: if you comment this out, it works fine
_what_could_possibly_go_wrong: List[Column] = []
import hack_logger
def it():
from sqlalchemy import create_engine
engine = create_engine('sqlite:////tmp/test.sqlite')
with engine.connect() as conn:
conn.execute('DROP TABLE IF EXISTS test')
conn.execute('CREATE TABLE IF NOT EXISTS test (value INT)')
with conn.begin():
for i in range(5):
conn.execute(f'INSERT INTO test VALUES ({i})')
yield i
from functools import lru_cache
# NOTE: if you remove lcu_cache, it works fine
@lru_cache(1)
def itc():
return it()
next(itc())
###
@karlicoss
Copy link
Author

karlicoss commented Oct 6, 2020

Result:

|Exception ignored in: <generator object it at 0x7fcbfd4963c0>                                                                  
|Traceback (most recent call last):                                                                                             
|  File "qqq.py", line 18, in it                                                                                                
|  File "/home/karlicos/.local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1790, in __exit__                   
|  File "/home/karlicos/.local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1760, in rollback                   
|  File "/home/karlicos/.local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1800, in _do_rollback               
|  File "/home/karlicos/.local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 760, in _rollback_impl              
|  File "/home/karlicos/.local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1517, in _handle_dbapi_exception    
|  File "/home/karlicos/.local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 178, in raise_                      
|  File "/home/karlicos/.local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 755, in _rollback_impl              
|  File "/home/karlicos/.local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 545, in do_rollback              
|sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError)  Cannot operate on a closed database.                               
|(Background on this error at: http://sqlalche.me/e/13/f405) 

Expected: the exception is maybe sort of expected considering that GeneratorExit isn't really handled and all the lru_cache + generator madness, but wtf with logging and typing stuff??

Python: 3.8.2
SQLAlchemy: 1.3.18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment