--- /dev/null
+.. change::
+ :tags: bug, sql
+ :tickets: 6786
+
+ Fixed issue where use of the :paramref:`_sql.case.whens` parameter passing
+ a dictionary positionally and not as a keyword argument would emit a 2.0
+ deprecation warning, referring to the deprecation of passing a list
+ positionally. The dictionary format of "whens", passed positionally, is
+ still supported and was accidentally marked as deprecated.
+
def _expression_collection_was_a_list(attrname, fnname, args):
if args and isinstance(args[0], (list, set, dict)) and len(args) == 1:
- util.warn_deprecated_20(
- 'The "%s" argument to %s() is now passed as a series of '
- "positional "
- "elements, rather than as a list. " % (attrname, fnname)
- )
+ if isinstance(args[0], list):
+ util.warn_deprecated_20(
+ 'The "%s" argument to %s(), when referring to a sequence '
+ "of items, is now passed as a series of positional "
+ "elements, rather than as a list. " % (attrname, fnname)
+ )
return args[0]
else:
return args
if "whens" in kw:
util.warn_deprecated_20(
- 'The "whens" argument to case() is now passed as a series of '
- "positional "
- "elements, rather than as a list. "
- )
- whens = kw.pop("whens")
- else:
- whens = coercions._expression_collection_was_a_list(
- "whens", "case", whens
+ 'The "whens" argument to case() is now passed using '
+ "positional style only, not as a keyword argument."
)
+ whens = (kw.pop("whens"),)
+
+ whens = coercions._expression_collection_was_a_list(
+ "whens", "case", whens
+ )
+
try:
whens = util.dictlike_iteritems(whens)
except TypeError:
def test_null_in_empty_set_is_false_bindparam(self, connection):
stmt = select(
case(
- [
- (
- null().in_(bindparam("foo", value=())),
- true(),
- )
- ],
+ (
+ null().in_(bindparam("foo", value=())),
+ true(),
+ ),
else_=false(),
)
)
def test_null_in_empty_set_is_false_direct(self, connection):
stmt = select(
case(
- [
- (
- null().in_([]),
- true(),
- )
- ],
+ (
+ null().in_([]),
+ true(),
+ ),
else_=false(),
)
)
id_ = result.inserted_primary_key[0]
stmt = select(date_table.c.id).where(
case(
- [
- (
- bindparam("foo", type_=self.datatype) != None,
- bindparam("foo", type_=self.datatype),
- )
- ],
+ (
+ bindparam("foo", type_=self.datatype) != None,
+ bindparam("foo", type_=self.datatype),
+ ),
else_=date_table.c.date_data,
)
== date_table.c.date_data
"arguments in version 2.0",
r"The Join.select\(\) method will no longer accept keyword arguments "
"in version 2.0.",
- r"The \"whens\" argument to case\(\) is now passed",
r"The Join.select\(\).whereclause parameter is deprecated",
#
# DML
self.classes.Employee,
self.tables.employees,
polymorphic_on=case(
- value=self.tables.employees.c.type,
- whens={
+ {
"E": "employee",
"M": "manager",
"G": "engineer",
"R": "engineer",
},
+ value=self.tables.employees.c.type,
),
polymorphic_identity="employee",
)
User = self.classes.User
session = fixture_session()
with self._assert_bind_args(session, expect_mapped_bind=True):
- session.query(case([(User.name == "x", "C")], else_="W")).all()
+ session.query(case((User.name == "x", "C"), else_="W")).all()
def test_cast(self):
User = self.classes.User
# this would work with Firebird if you do literal_column('1')
# instead
- case_stmt = case([(Document.title.in_(subq), True)], else_=False)
+ case_stmt = case((Document.title.in_(subq), True), else_=False)
s.query(Document).update(
{"flag": case_stmt}, synchronize_session=False
),
argnames="test_case, expected",
)
- @testing.combinations(("positional",), ("kwarg",), argnames="argstyle")
- def test_when_dicts(self, argstyle, test_case, expected):
+ def test_when_dicts(self, test_case, expected):
t = table("test", column("col1"))
whens, value, else_ = testing.resolve_lambda(test_case, t=t)
if else_ is not None:
kw["else_"] = else_
- if argstyle == "kwarg":
- return case(whens=whens, **kw)
- elif argstyle == "positional":
- return case(whens, **kw)
+ return case(whens, **kw)
# note: 1.3 also does not allow this form
# case([whens], **kw)
t1 = table("t", column("q"))
with testing.expect_deprecated(
- r"The \"whens\" argument to case\(\) is now passed"
+ r"The \"whens\" argument to case\(\), when referring "
+ r"to a sequence of items, is now passed"
):
stmt = select(t1).where(
case(
t1 = table("t", column("q"))
with testing.expect_deprecated(
- r"The \"whens\" argument to case\(\) is now passed"
+ r"The \"whens\" argument to case\(\), when referring "
+ "to a sequence of items, is now passed"
):
stmt = select(t1).where(
case(
"ELSE :param_3 END != :param_4",
)
+ @testing.combinations(
+ (
+ (lambda t: ({"x": "y"}, t.c.col1, None)),
+ "CASE test.col1 WHEN :param_1 THEN :param_2 END",
+ ),
+ (
+ (lambda t: ({"x": "y", "p": "q"}, t.c.col1, None)),
+ "CASE test.col1 WHEN :param_1 THEN :param_2 "
+ "WHEN :param_3 THEN :param_4 END",
+ ),
+ (
+ (lambda t: ({t.c.col1 == 7: "x"}, None, 10)),
+ "CASE WHEN (test.col1 = :col1_1) THEN :param_1 ELSE :param_2 END",
+ ),
+ (
+ (lambda t: ({t.c.col1 == 7: "x", t.c.col1 == 10: "y"}, None, 10)),
+ "CASE WHEN (test.col1 = :col1_1) THEN :param_1 "
+ "WHEN (test.col1 = :col1_2) THEN :param_2 ELSE :param_3 END",
+ ),
+ argnames="test_case, expected",
+ )
+ def test_when_kwarg(self, test_case, expected):
+ t = table("test", column("col1"))
+
+ whens, value, else_ = testing.resolve_lambda(test_case, t=t)
+
+ def _case_args(whens, value=None, else_=None):
+ kw = {}
+ if value is not None:
+ kw["value"] = value
+ if else_ is not None:
+ kw["else_"] = else_
+
+ with testing.expect_deprecated_20(
+ r'The "whens" argument to case\(\) is now passed using '
+ r"positional style only, not as a keyword argument."
+ ):
+
+ return case(whens=whens, **kw)
+
+ # note: 1.3 also does not allow this form
+ # case([whens], **kw)
+
+ self.assert_compile(
+ _case_args(whens=whens, value=value, else_=else_),
+ expected,
+ )
+
def test_case_whens_dict_kw(self):
t1 = table("t", column("q"))
-
with testing.expect_deprecated(
r"The \"whens\" argument to case\(\) is now passed"
):
)
!= "bat"
)
-
self.assert_compile(
stmt,
"SELECT t.q FROM t WHERE CASE WHEN (t.q = :q_1) THEN "
)
s1 = table1.select().scalar_subquery()
- with testing.expect_deprecated(
+ with testing.expect_deprecated_20(
r"The \"columns\" argument to "
- r"Select.with_only_columns\(\) is now passed"
+ r"Select.with_only_columns\(\), when referring "
+ "to a sequence of items, is now passed"
):
stmt = s1.with_only_columns([s1])
self.assert_compile(
s1 = select(table1.c.a, table2.c.b)
self.assert_compile(s1, "SELECT t1.a, t2.b FROM t1, t2")
- with testing.expect_deprecated(
+ with testing.expect_deprecated_20(
r"The \"columns\" argument to "
- r"Select.with_only_columns\(\) is now passed"
+ r"Select.with_only_columns\(\), when referring "
+ "to a sequence of items, is now passed"
):
s2 = s1.with_only_columns([table2.c.b])
elements.BooleanClauseList._construct_raw(operators.and_),
elements.BooleanClauseList._construct_raw(operators.or_),
elements.Tuple(),
- elements.Case([]),
+ elements.Case(),
elements.Extract("foo", column("x")),
elements.UnaryExpression(column("x")),
elements.Grouping(column("x")),