From: Federico Caselli Date: Tue, 28 Feb 2023 22:14:05 +0000 (+0100) Subject: Validate metadata schema arg X-Git-Tag: rel_2_0_5~12^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=54b4d5ee2465af55863dc1b02d61cee4675db167;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Validate metadata schema arg Validate that when provided the :paramref:`_sql.MetaData.schema` argument of :class:`_sql.MetaData` is a string. Change-Id: I4237232d2ee0f5a4d0b9dbd9af5f5b57abf395b4 --- diff --git a/doc/build/changelog/unreleased_20/metadata_schema.rst b/doc/build/changelog/unreleased_20/metadata_schema.rst new file mode 100644 index 0000000000..88664d1cb0 --- /dev/null +++ b/doc/build/changelog/unreleased_20/metadata_schema.rst @@ -0,0 +1,5 @@ +.. change:: + :tags: schema + + Validate that when provided the :paramref:`_sql.MetaData.schema` + argument of :class:`_sql.MetaData` is a string. diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 20c0341adb..b4263137b3 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -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 = ( diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index bd5920f136..8f6c81f151 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -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