--- /dev/null
+.. change::
+ :tags: change, orm
+ :tickets: 5573
+
+ Passing keyword arguments to methods such as :meth:`_orm.Session.execute`
+ to be passed into the :meth:`_orm.Session.get_bind` method is deprecated;
+ the new :paramref:`_orm.Session.execute.bind_arguments` dictionary should
+ be passed instead.
+
"""
statement = coercions.expect(roles.CoerceTextStatementRole, statement)
- if not bind_arguments:
- bind_arguments = kw
- elif kw:
- bind_arguments.update(kw)
+ if kw:
+ util.warn_deprecated_20(
+ "Passing bind arguments to Session.execute() as keyword "
+ "arguments is deprecated and will be removed SQLAlchemy 2.0. "
+ "Please use the bind_arguments parameter."
+ )
+ if not bind_arguments:
+ bind_arguments = kw
+ else:
+ bind_arguments.update(kw)
+ elif not bind_arguments:
+ bind_arguments = {}
if future and (
statement._propagate_attrs.get("compile_state_plugin", None)
self,
statement,
params=None,
- execution_options=None,
- mapper=None,
- bind=None,
+ execution_options=util.EMPTY_DICT,
+ bind_arguments=None,
**kw
):
"""Like :meth:`~.Session.execute` but return a scalar result."""
return self.execute(
- statement, params=params, mapper=mapper, bind=bind, **kw
+ statement,
+ params=params,
+ execution_options=execution_options,
+ bind_arguments=bind_arguments,
+ **kw
).scalar()
def close(self):
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_
from sqlalchemy.testing import is_true
+from sqlalchemy.testing import mock
from sqlalchemy.testing.mock import call
from sqlalchemy.testing.mock import Mock
from sqlalchemy.testing.schema import Column
):
Session(autocommit=True)
+ @testing.combinations(
+ {"mapper": None},
+ {"clause": None},
+ {"bind_arguments": {"mapper": None}, "clause": None},
+ {"bind_arguments": {}, "clause": None},
+ )
+ def test_bind_kwarg_deprecated(self, kw):
+ s1 = Session(testing.db)
+
+ for meth in s1.execute, s1.scalar:
+ m1 = mock.Mock(side_effect=s1.get_bind)
+ with mock.patch.object(s1, "get_bind", m1):
+ expr = text("select 1")
+
+ with testing.expect_deprecated_20(
+ r"Passing bind arguments to Session.execute\(\) as "
+ "keyword "
+ "arguments is deprecated and will be removed SQLAlchemy "
+ "2.0"
+ ):
+ meth(expr, **kw)
+
+ bind_arguments = kw.pop("bind_arguments", None)
+ if bind_arguments:
+ bind_arguments.update(kw)
+
+ if "clause" not in kw:
+ bind_arguments["clause"] = expr
+ eq_(m1.mock_calls, [call(**bind_arguments)])
+ else:
+ if "clause" not in kw:
+ kw["clause"] = expr
+ eq_(m1.mock_calls, [call(**kw)])
+
@testing.requires.independent_connections
@testing.emits_warning(".*previous exception")
def test_failed_rollback_deactivates_transaction_ctx_integration(self):
assert u2 is u
assert (
sess.execute(
- "select count(1) from users", mapper=User
+ "select count(1) from users",
+ bind_arguments=dict(mapper=User),
).scalar()
== 1
)
sess.commit()
assert (
sess.execute(
- "select count(1) from users", mapper=User
+ "select count(1) from users",
+ bind_arguments=dict(mapper=User),
).scalar()
== 1
)
sa.exc.NoInspectionAvailable, callable_, *args, **kw
)
- raises_("connection", mapper=user_arg)
+ raises_("connection", bind_arguments=dict(mapper=user_arg))
- raises_("execute", "SELECT 1", mapper=user_arg)
+ raises_("execute", "SELECT 1", bind_arguments=dict(mapper=user_arg))
raises_("get_bind", mapper=user_arg)
- raises_("scalar", "SELECT 1", mapper=user_arg)
+ raises_("scalar", "SELECT 1", bind_arguments=dict(mapper=user_arg))
eq_(
watchdog,