]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
dont warn for dictionary passed positionally
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 20 Jul 2021 15:03:08 +0000 (11:03 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 21 Jul 2021 20:00:12 +0000 (16:00 -0400)
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.

Removes warning filter for case statement.

Fixes: #6786
Change-Id: I8efd1882563773bec89ae5e34f0dfede77fc4683

12 files changed:
doc/build/changelog/unreleased_14/6786.rst [new file with mode: 0644]
lib/sqlalchemy/sql/coercions.py
lib/sqlalchemy/sql/elements.py
lib/sqlalchemy/testing/suite/test_select.py
lib/sqlalchemy/testing/suite/test_types.py
lib/sqlalchemy/testing/warnings.py
test/orm/test_merge.py
test/orm/test_query.py
test/orm/test_update_delete.py
test/sql/test_case_statement.py
test/sql/test_deprecations.py
test/sql/test_selectable.py

diff --git a/doc/build/changelog/unreleased_14/6786.rst b/doc/build/changelog/unreleased_14/6786.rst
new file mode 100644 (file)
index 0000000..7d95d5c
--- /dev/null
@@ -0,0 +1,10 @@
+.. 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.
+
index e21f4a9a5fcf895688da878ff463864490177d8c..dce32935245b65b980c52bd6ad2ddf6497ec3daf 100644 (file)
@@ -97,11 +97,12 @@ def _document_text_coercion(paramname, meth_rst, param_rst):
 
 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
index e253ddb936f312b28e79060262b8df714bcf81ed..ae93eb2a6cd219374c9d3d77a863e6571a3059cb 100644 (file)
@@ -2918,15 +2918,15 @@ class Case(ColumnElement):
 
         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:
index 0a1227e584c3244edaf7c1cc7e0b7d05bb386876..c96c62a458e7650043afc6835e75b347413d0c06 100644 (file)
@@ -1259,12 +1259,10 @@ class ExpandingBoundInTest(fixtures.TablesTest):
     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(),
             )
         )
@@ -1273,12 +1271,10 @@ class ExpandingBoundInTest(fixtures.TablesTest):
     def test_null_in_empty_set_is_false_direct(self, connection):
         stmt = select(
             case(
-                [
-                    (
-                        null().in_([]),
-                        true(),
-                    )
-                ],
+                (
+                    null().in_([]),
+                    true(),
+                ),
                 else_=false(),
             )
         )
index f793ff5290d6993cab6594e5ae762dfbf383e0f4..d367e7dc3c4ca99e0babf4023944808e7b6c5151 100644 (file)
@@ -362,12 +362,10 @@ class _DateFixture(_LiteralRoundTripFixture, fixtures.TestBase):
             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
index f8d6296a0c0dcf3d6857074f27bf649368e49d48..af5ad184fb9894317554b2535c9521f27919e6b8 100644 (file)
@@ -74,7 +74,6 @@ def setup_filters():
         "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
index 22f0cd9ac3d13f561d4121bc369071d8cc77a7c1..a0cb5426f3adacd52735bf59fe5bc6ae8ee43c03 100644 (file)
@@ -2086,13 +2086,13 @@ class PolymorphicOnTest(fixtures.MappedTest):
             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",
         )
index 07399030f03d52a0040eb8b2ba911b10420c8240..3c806e9d5c333dcbf5bdf5d3964b51bd42a9980f 100644 (file)
@@ -7290,7 +7290,7 @@ class SessionBindTest(QueryTest):
         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
index 9c8c4a5fc85c34b2d12c7320b895e007d84047e5..3890fd8bba8b0499b8c491af2efaf2a31cb25bf6 100644 (file)
@@ -1721,7 +1721,7 @@ class UpdateDeleteFromTest(fixtures.MappedTest):
 
         # 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
index 7dd66840f5d122cdd7a1874e7df0917b1b3b754c..c6d5f0185ba21957e4560d876c5dcb870ad9595d 100644 (file)
@@ -175,8 +175,7 @@ class CaseTest(fixtures.TestBase, AssertsCompiledSQL):
         ),
         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)
@@ -188,10 +187,7 @@ class CaseTest(fixtures.TestBase, AssertsCompiledSQL):
             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)
index 7b2b6c57e7b5aa54252d99b0fe24b25914c1fcaf..3256ebcadf9e97c231cf72619e63b1db04436e44 100644 (file)
@@ -605,7 +605,8 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL):
         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(
@@ -625,7 +626,8 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL):
         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(
@@ -642,9 +644,56 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL):
             "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"
         ):
@@ -655,7 +704,6 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL):
                 )
                 != "bat"
             )
-
         self.assert_compile(
             stmt,
             "SELECT t.q FROM t WHERE CASE WHEN (t.q = :q_1) THEN "
@@ -685,9 +733,10 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL):
         )
         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(
@@ -702,9 +751,10 @@ class SelectableTest(fixtures.TestBase, AssertsCompiledSQL):
         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])
 
index cfdf4ad02ee884dea7514b601f48700ad2a66e5a..61e25ddd71f11ee1b24f28a481b817f6f33c1e4f 100644 (file)
@@ -3137,7 +3137,7 @@ class ReprTest(fixtures.TestBase):
             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")),