From: Federico Caselli Date: Mon, 5 Apr 2021 19:57:24 +0000 (+0200) Subject: Table arguments name and metadata are positional only X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6a485acaa914933158ca4d28fb305d64c901b728;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Table arguments name and metadata are positional only 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 (cherry picked from commit 3de954bc027ebf655e7ed26c83cfb3fd4b7b5edf) --- diff --git a/doc/build/changelog/unreleased_13/6135.rst b/doc/build/changelog/unreleased_13/6135.rst new file mode 100644 index 0000000000..12fb9eb239 --- /dev/null +++ b/doc/build/changelog/unreleased_13/6135.rst @@ -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. diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index e6ab8efb53..41e8958cb7 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -507,14 +507,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: diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 5cd97c5ef8..3387a87034 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -52,6 +52,7 @@ from sqlalchemy.testing import is_ from sqlalchemy.testing import is_false from sqlalchemy.testing import is_true from sqlalchemy.testing import mock +from sqlalchemy.testing.assertions import assert_raises_message_context_ok class MetaDataTest(fixtures.TestBase, ComparesTables): @@ -1944,6 +1945,23 @@ class PKAutoIncrementTest(fixtures.TestBase): t3.append_constraint(pk) is_(pk._autoincrement_column, t3.c.a) + def test_no_kw_args(self): + assert_raises_message_context_ok( + TypeError, + r"Table\(\) takes at least two positional-only arguments", + Table, + name="foo", + metadata=MetaData(), + ) + + assert_raises_message_context_ok( + TypeError, + r"Table\(\) takes at least two positional-only arguments", + Table, + "foo", + metadata=MetaData(), + ) + class SchemaTypeTest(fixtures.TestBase): __backend__ = True