From: Daniele Varrazzo Date: Sat, 23 May 2020 01:29:15 +0000 (+1200) Subject: Acept string subtypes as connection string X-Git-Tag: 3.0.dev0~497 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=eb6913c1d6adbc1943effd3e635ce0ce42583732;p=thirdparty%2Fpsycopg.git Acept string subtypes as connection string --- 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")