]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Table arguments name and metadata are positional only
authorFederico Caselli <cfederico87@gmail.com>
Mon, 5 Apr 2021 19:57:24 +0000 (21:57 +0200)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 6 Apr 2021 21:18:44 +0000 (17:18 -0400)
The :class:`_sql.Table` object now raises an informative error message if
it is instantiated without passing at least the :paramref:`_sql.Table.name`
and :paramref:`_sql.Table.metadata` arguments positionally. Previously, if
these were passed as keyword arguments, the object would silently fail to
initialize correctly.

Fixes: #6135
Change-Id: I54d0c89fd549fc504289a87ea0bb37369f982b06

doc/build/changelog/unreleased_13/6135.rst [new file with mode: 0644]
lib/sqlalchemy/sql/schema.py
test/sql/test_metadata.py

diff --git a/doc/build/changelog/unreleased_13/6135.rst b/doc/build/changelog/unreleased_13/6135.rst
new file mode 100644 (file)
index 0000000..12fb9eb
--- /dev/null
@@ -0,0 +1,10 @@
+.. change::
+    :tags: sql, bug
+    :tickets: 6135
+    :versions: 1.4.6
+
+    The :class:`_sql.Table` object now raises an informative error message if
+    it is instantiated without passing at least the :paramref:`_sql.Table.name`
+    and :paramref:`_sql.Table.metadata` arguments positionally. Previously, if
+    these were passed as keyword arguments, the object would silently fail to
+    initialize correctly.
index 9d76173705ee05ed1451fa3e4b3f2727296dc671..f2c1c86ec8e30c5ce6af9be59f728f8a9d40b52c 100644 (file)
@@ -546,14 +546,17 @@ class Table(DialectKWArgs, SchemaItem, TableClause):
         ),
     )
     def __new__(cls, *args, **kw):
-        if not args:
+        if not args and not kw:
             # python3k pickle seems to call this
             return object.__new__(cls)
 
         try:
             name, metadata, args = args[0], args[1], args[2:]
         except IndexError:
-            raise TypeError("Table() takes at least two arguments")
+            raise TypeError(
+                "Table() takes at least two positional-only "
+                "arguments 'name' and 'metadata'"
+            )
 
         schema = kw.get("schema", None)
         if schema is None:
index e2f015b00ea44cf9298d3d692a3d95a0362c3e64..25834ac766dc213b033a7955be841394f0f39663 100644 (file)
@@ -47,6 +47,7 @@ from sqlalchemy.testing import AssertsCompiledSQL
 from sqlalchemy.testing import ComparesTables
 from sqlalchemy.testing import emits_warning
 from sqlalchemy.testing import eq_
+from sqlalchemy.testing import expect_raises_message
 from sqlalchemy.testing import fixtures
 from sqlalchemy.testing import is_
 from sqlalchemy.testing import is_false
@@ -2022,6 +2023,20 @@ class PKAutoIncrementTest(fixtures.TestBase):
         t3.append_constraint(pk)
         is_(pk._autoincrement_column, t3.c.a)
 
+    def test_no_kw_args(self):
+        with expect_raises_message(
+            TypeError,
+            r"Table\(\) takes at least two positional-only arguments",
+            check_context=False,
+        ):
+            Table(name="foo", metadata=MetaData())
+        with expect_raises_message(
+            TypeError,
+            r"Table\(\) takes at least two positional-only arguments",
+            check_context=False,
+        ):
+            Table("foo", metadata=MetaData())
+
 
 class SchemaTypeTest(fixtures.TestBase):
     __backend__ = True