From eb6913c1d6adbc1943effd3e635ce0ce42583732 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 23 May 2020 13:29:15 +1200 Subject: [PATCH] Acept string subtypes as connection string --- psycopg3/conninfo.py | 5 +++-- tests/test_async_connection.py | 8 ++++++++ tests/test_connection.py | 8 ++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/psycopg3/conninfo.py b/psycopg3/conninfo.py index 0fdce34c4..f5f07f361 100644 --- a/psycopg3/conninfo.py +++ b/psycopg3/conninfo.py @@ -14,10 +14,11 @@ def make_conninfo(conninfo: str = "", **kwargs: Any) -> str: if not conninfo and not kwargs: return "" - # If no kwarg is specified don't mung the conninfo but check if it's correct + # If no kwarg specified don't mung the conninfo but check if it's correct. + # Make sure to return a string, not a subtypep, to avoid making Liskov sad. if not kwargs: _parse_conninfo(conninfo) - return conninfo + return str(conninfo) # Override the conninfo with the parameters # Drop the None arguments diff --git a/tests/test_async_connection.py b/tests/test_async_connection.py index fa84c2bdd..4ea0ee068 100644 --- a/tests/test_async_connection.py +++ b/tests/test_async_connection.py @@ -24,6 +24,14 @@ async def test_connect_bad(): await AsyncConnection.connect("dbname=nosuchdb") +async def test_connect_str_subclass(dsn): + class MyString(str): + pass + + conn = await AsyncConnection.connect(MyString(dsn)) + assert conn.status == conn.ConnStatus.OK + + async def test_close(aconn): assert not aconn.closed await aconn.close() diff --git a/tests/test_connection.py b/tests/test_connection.py index 794295b55..0a83d2f9a 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -16,6 +16,14 @@ def test_connect(dsn): assert conn.status == conn.ConnStatus.OK +def test_connect_str_subclass(dsn): + class MyString(str): + pass + + conn = Connection.connect(MyString(dsn)) + assert conn.status == conn.ConnStatus.OK + + def test_connect_bad(): with pytest.raises(psycopg3.OperationalError): Connection.connect("dbname=nosuchdb") -- 2.47.3