From: Daniele Varrazzo Date: Thu, 18 Apr 2024 01:38:17 +0000 (+0200) Subject: fix: fix handling of floating point values for connect_timeout X-Git-Tag: 3.2.0~40 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F796%2Fhead;p=thirdparty%2Fpsycopg.git fix: fix handling of floating point values for connect_timeout --- diff --git a/docs/news.rst b/docs/news.rst index 3b0c06ffe..494ebaa19 100644 --- a/docs/news.rst +++ b/docs/news.rst @@ -50,6 +50,8 @@ Psycopg 3.1.19 ^^^^^^^^^^^^^^ - Fix excessive stripping of error message prefixes (:ticket:`#752`). +- Allow to specify the ``connect_timeout`` connection parameter as float + (:ticket:`#796`). Current release diff --git a/psycopg/psycopg/conninfo.py b/psycopg/psycopg/conninfo.py index 1401426b2..f40b58a6b 100644 --- a/psycopg/psycopg/conninfo.py +++ b/psycopg/psycopg/conninfo.py @@ -138,9 +138,9 @@ def timeout_from_conninfo(params: ConnDict) -> int: if value is None: value = _DEFAULT_CONNECT_TIMEOUT try: - timeout = int(value) + timeout = int(float(value)) except ValueError: - raise e.ProgrammingError(f"bad value for connect_timeout: {value!r}") + raise e.ProgrammingError(f"bad value for connect_timeout: {value!r}") from None if timeout <= 0: # The sync connect function will stop on the default socket timeout diff --git a/tests/test_conninfo.py b/tests/test_conninfo.py index badd5d92c..8e64fe3a5 100644 --- a/tests/test_conninfo.py +++ b/tests/test_conninfo.py @@ -96,6 +96,7 @@ def test_no_munging(): ("connect_timeout=0", _DEFAULT_CONNECT_TIMEOUT, None), ("connect_timeout=1", 2, None), ("connect_timeout=10", 10, None), + ("connect_timeout=5.0", 5, None), ("", 15, {"PGCONNECT_TIMEOUT": "15"}), ], )