]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Validate metadata schema arg
authorFederico Caselli <cfederico87@gmail.com>
Tue, 28 Feb 2023 22:14:05 +0000 (23:14 +0100)
committerFederico Caselli <cfederico87@gmail.com>
Wed, 1 Mar 2023 21:28:28 +0000 (22:28 +0100)
Validate that when provided the :paramref:`_sql.MetaData.schema`
argument of :class:`_sql.MetaData` is a string.

Change-Id: I4237232d2ee0f5a4d0b9dbd9af5f5b57abf395b4

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

diff --git a/doc/build/changelog/unreleased_20/metadata_schema.rst b/doc/build/changelog/unreleased_20/metadata_schema.rst
new file mode 100644 (file)
index 0000000..88664d1
--- /dev/null
@@ -0,0 +1,5 @@
+.. change::
+    :tags: schema
+
+    Validate that when provided the :paramref:`_sql.MetaData.schema`
+    argument of :class:`_sql.MetaData` is a string.
index 20c0341adb5f6e160b08a73795bb96174575d4b8..b4263137b38182c770a47001a38284f645eec08b 100644 (file)
@@ -5227,6 +5227,11 @@ class MetaData(HasSchemaAttr):
                 examples.
 
         """
+        if schema is not None and not isinstance(schema, str):
+            raise exc.ArgumentError(
+                "expected schema argument to be a string, "
+                f"got {type(schema)}."
+            )
         self.tables = util.FacadeDict()
         self.schema = quoted_name.construct(schema, quote_schema)
         self.naming_convention = (
index bd5920f136a2c2aea26af2e281b9e523bde16a7a..8f6c81f151a0c79fe570903ee0a1935a2d5d0a77 100644 (file)
@@ -783,6 +783,19 @@ class MetaDataTest(fixtures.TestBase, ComparesTables):
         ):
             eq_(repr(const), exp)
 
+    @testing.variation("kind", ["engine", "conn", "something"])
+    def test_metadata_bind(self, connection, kind):
+        with expect_raises_message(
+            exc.ArgumentError,
+            "expected schema argument to be a string, got",
+        ):
+            if kind.engine:
+                MetaData(connection.engine)
+            elif kind.conn:
+                MetaData(connection)
+            else:
+                MetaData(42)  # type: ignore
+
 
 class ToMetaDataTest(fixtures.TestBase, AssertsCompiledSQL, ComparesTables):
     @testing.requires.check_constraints