Skip to content

Add proxy for autocommit property on Connection#312

Open
jonathan-d-zhang wants to merge 1 commit into
omnilib:mainfrom
jonathan-d-zhang:autocommit-proxy
Open

Add proxy for autocommit property on Connection#312
jonathan-d-zhang wants to merge 1 commit into
omnilib:mainfrom
jonathan-d-zhang:autocommit-proxy

Conversation

@jonathan-d-zhang

Copy link
Copy Markdown

Description

Add a proxy property for the autocommit property on sqlite3.Connection objects.

Use Case

I want autocommit=False as suggested by the sqlite3 docs (see docs). I also want to set PRAGMA journal_mode = WAL. The issue is that I can't change the journal_mode when in a transaction, which is always the case when autocommit=False. So the solution is to set the autocommit property after setting the journal_mode. This works fine in sqlite3, but aiosqlite doesn't have the property.

I can work around this by simply reaching into the aiosqlite.Connection object and accessing the _conn attribute, but this is clearly not ideal.

@amyreese

amyreese commented Feb 3, 2025

Copy link
Copy Markdown
Member

Looks like this is a new feature in sqlite for Python 3.12. Can you update this to raise an exception accordingly so that mypy passes cleanly? Also, a simple test case would be appreciated (and I'd be ok with something like @skipIf(sys.version_info < (3,12))).

@amyreese amyreese left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mypy fails because this is a 3.12 feature:

python -m mypy -p aiosqlite
aiosqlite/core.py:246: error: "Connection" has no attribute "autocommit"  [attr-defined]
Found 1 error in 1 file (checked 10 source files)

@jonathan-d-zhang

Copy link
Copy Markdown
Author

It turns out this property is actually a bit difficult to support. With the test,

    @skipIf(sys.version_info < (3, 12), "3.12 sqlite3 library feature")
    async def test_autocommit_property(self):
        async with aiosqlite.connect(TEST_DB) as db:
            self.assertEqual(db.autocommit, db._conn.autocommit)

I get sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id <> and this is thread id <>. I'm not sure why this behaves differently from the other proxied properties (maybe because it can change the connection state (but some of the other properties can do that too ?)). What should be done here, @amyreese?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants