From dc1f16c16552ce7acdfdd298a370be8d03964fc7 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 4 Sep 2021 11:29:10 -0400 Subject: [PATCH] remove erroneous None check from _assert_str Fixed issue in ``URL`` where validation of "drivername" would not appropriately respond to the ``None`` value where a string were expected. Fixes: #6983 Change-Id: If546c373a60533779595a9e393ea9a59a9b8a96f --- doc/build/changelog/unreleased_14/6983.rst | 6 +++++ lib/sqlalchemy/engine/url.py | 3 --- test/engine/test_parseconnect.py | 30 +++++++++++++++++----- 3 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 doc/build/changelog/unreleased_14/6983.rst diff --git a/doc/build/changelog/unreleased_14/6983.rst b/doc/build/changelog/unreleased_14/6983.rst new file mode 100644 index 0000000000..2aa31e8eba --- /dev/null +++ b/doc/build/changelog/unreleased_14/6983.rst @@ -0,0 +1,6 @@ +.. change:: + :tags: bug, engine + :tickets: 6983 + + Fixed issue in ``URL`` where validation of "drivername" would not + appropriately respond to the ``None`` value where a string were expected. diff --git a/lib/sqlalchemy/engine/url.py b/lib/sqlalchemy/engine/url.py index b924f1c730..1b96c3c2e1 100644 --- a/lib/sqlalchemy/engine/url.py +++ b/lib/sqlalchemy/engine/url.py @@ -154,9 +154,6 @@ class URL( @classmethod def _assert_str(cls, v, paramname): - if v is None: - return v - if not isinstance(v, compat.string_types): raise TypeError("%s must be a string" % paramname) return v diff --git a/test/engine/test_parseconnect.py b/test/engine/test_parseconnect.py index ea949dfd64..9acedaa852 100644 --- a/test/engine/test_parseconnect.py +++ b/test/engine/test_parseconnect.py @@ -16,6 +16,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 expect_deprecated from sqlalchemy.testing.mock import call from sqlalchemy.testing.mock import MagicMock from sqlalchemy.testing.mock import Mock @@ -275,20 +276,35 @@ class URLTest(fixtures.TestBase): url.make_url("drivername:///?%s" % expected), ) - @testing.combinations( - "username", - "host", - "database", - ) - def test_only_str_constructor(self, argname): + @testing.combinations("username", "host", "database", argnames="argname") + @testing.combinations((35.8), (True,), argnames="value") + def test_only_str_constructor(self, argname, value): assert_raises_message( TypeError, "%s must be a string" % argname, url.URL.create, "somedriver", - **{argname: 35.8} + **{argname: value} ) + @testing.combinations("username", "host", "database", argnames="argname") + def test_none_ok(self, argname): + u1 = url.URL.create("drivername", **{argname: None}) + is_(getattr(u1, argname), None) + + @testing.combinations((35.8), (True,), (None,), argnames="value") + def test_only_str_drivername_no_none(self, value): + assert_raises_message( + TypeError, "drivername must be a string", url.URL.create, value + ) + + @testing.combinations((35.8), (True,), (None,), argnames="value") + def test_only_str_drivername_no_none_legacy(self, value): + with expect_deprecated(r"Calling URL\(\) directly"): + assert_raises_message( + TypeError, "drivername must be a string", url.URL, value + ) + @testing.combinations( "username", "host", -- 2.47.2