Skip to content

fix(sqlite): align adaptation protocol with CPython #5964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Lib/test/test_sqlite3/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,17 +439,13 @@ def test_missing_protocol(self):
with self.assertRaises(sqlite.ProgrammingError):
sqlite.adapt(1, None)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_defect_proto(self):
class DefectProto():
def __adapt__(self):
return None
with self.assertRaises(sqlite.ProgrammingError):
sqlite.adapt(1., DefectProto)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_defect_self_adapt(self):
class DefectSelfAdapt(float):
def __conform__(self, _):
Expand Down
12 changes: 10 additions & 2 deletions stdlib/src/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,11 @@ mod _sqlite {
}
if let Ok(adapter) = proto.get_attr("__adapt__", vm) {
match adapter.call((obj,), vm) {
Ok(val) => return Ok(val),
Ok(val) => {
if !vm.is_none(&val) {
return Ok(val);
}
}
Comment on lines +698 to +702
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Verify CPython compatibility with test coverage.

The None check for __adapt__ return values correctly implements the adaptation protocol, but comprehensive test coverage would help ensure this change doesn't introduce regressions.


🏁 Script executed:

#!/bin/bash
# Description: Check for existing tests related to the adaptation protocol and None handling

# Search for test files related to sqlite adaptation
fd -e py -e rs | xargs rg -l "adapt.*None|__adapt__|__conform__" | head -10

# Look for specific test cases that might cover this behavior
rg -A 5 -B 5 "__adapt__.*None|__conform__.*None" --type py --type rs

# Check for existing sqlite adaptation tests
rg -A 10 -B 5 "test.*adapt|adapt.*test" stdlib/src/sqlite.rs || echo "No adaptation tests found in this file"

Length of output: 893


Add missing adaptation protocol tests for None handling in sqlite

There are currently no tests covering the case where __adapt__ (or __conform__) returns None in the SQLite module. Please add targeted test cases to prevent regressions:

• Location: stdlib/src/sqlite.rs (around lines 698–702)
• What to add:

  • A Rust unit test (e.g. in a #[cfg(test)] mod tests) that calls the adapter on a type which returns None, asserts that vm.is_none(&val) is true and that the function short-circuits by returning Ok(None).
  • If your test harness supports Python‐level tests, add a snippet in the Python testsuite that registers a dummy adapter returning None, runs a query binding it, and verifies the value comes back as None.

Example sketch in Rust:

 #[cfg(test)]
 mod tests {
     use super::*;
     use crate::vm::VirtualMachine;

     #[test]
     fn sqlite_adapt_returns_none() {
         let vm = VirtualMachine::new();
         // Define or fetch a value whose __adapt__ returns None
         let dummy = DummyAdapterReturningNone::new();
         let val = dummy.__adapt__(&vm).unwrap();
         assert!(vm.is_none(&val), "Expected None from __adapt__");
         // Confirm the sqlite path respects that None short-circuit
         match sqlite_bind(&vm, val) {
             Ok(v) => assert!(vm.is_none(&v)),
             Err(e) => panic!("Binding failed: {}", e),
         }
     }
 }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In stdlib/src/sqlite.rs around lines 698 to 702, add missing tests for the case
when __adapt__ or __conform__ returns None. Create a Rust unit test in the
#[cfg(test)] mod tests that defines a dummy adapter returning None, calls the
adapter, asserts vm.is_none(&val) is true, and verifies the function returns
Ok(None) to ensure proper short-circuiting. Additionally, if possible, add a
Python-level test in the testsuite that registers a dummy adapter returning
None, executes a query binding it, and confirms the returned value is None to
prevent regressions.

Err(exc) => {
if !exc.fast_isinstance(vm.ctx.exceptions.type_error) {
return Err(exc);
Expand All @@ -705,7 +709,11 @@ mod _sqlite {
}
if let Ok(adapter) = obj.get_attr("__conform__", vm) {
match adapter.call((proto,), vm) {
Ok(val) => return Ok(val),
Ok(val) => {
if !vm.is_none(&val) {
return Ok(val);
}
}
Err(exc) => {
if !exc.fast_isinstance(vm.ctx.exceptions.type_error) {
return Err(exc);
Expand Down
Loading