perf(persistence): avoid formatting missing SQL attribute errors (#2776)

This commit is contained in:
huanghuoguoguo
2026-10-27 11:19:44 +08:00
committed by GitHub
parent fd255a13f4
commit 62c0f46dc4
2 changed files with 51 additions and 5 deletions
@@ -1219,3 +1219,32 @@ async def test_after_commit_task_is_cancelled_and_coroutine_closed_on_rollback(t
assert coro.cr_frame is None
finally:
await engine.dispose()
@pytest.mark.parametrize('source', ['instance', 'comparator'])
async def test_scoped_statement_revalidates_mutated_optional_attributes(source) -> None:
expression = sa.literal(1)
statement = sa.select(expression)
_validate_scoped_statement_call((statement,), {})
if source == 'instance':
expression._suffixes = ('unsafe SQL',)
else:
expression.comparator = SimpleNamespace(_suffixes=('unsafe SQL',))
with pytest.raises(ScopedSessionTransactionError, match='textual statement modifiers'):
_validate_scoped_statement_call((statement,), {})
async def test_scoped_statement_retains_custom_attribute_fallback(monkeypatch) -> None:
expression = sa.literal(1)
statement = sa.select(expression)
_validate_scoped_statement_call((statement,), {})
original = sa.sql.elements.ColumnElement.__getattr__
def changed_attribute(self, name):
if name == '_suffixes':
return ('unsafe SQL',)
return original(self, name)
monkeypatch.setattr(sa.sql.elements.ColumnElement, '__getattr__', changed_attribute)
with pytest.raises(ScopedSessionTransactionError, match='textual statement modifiers'):
_validate_scoped_statement_call((statement,), {})