From 13be1aa9b9a4a15e64035f7dcb0121be54ec872e Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Tue, 15 Aug 2023 21:28:05 +1000 Subject: [PATCH] fix: parse options with numerical values as int (#225) * fix: parse options with numerical values as int fixes #222 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- dj_database_url/__init__.py | 5 ++++- tests/test_dj_database_url.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/dj_database_url/__init__.py b/dj_database_url/__init__.py index ba8e013..f580f90 100644 --- a/dj_database_url/__init__.py +++ b/dj_database_url/__init__.py @@ -162,7 +162,10 @@ def parse( options["ssl"] = {"ca": values[-1]} continue - options[key] = values[-1] + try: + options[key] = int(values[-1]) + except (TypeError, ValueError): + options[key] = values[-1] if ssl_require: options["sslmode"] = "require" diff --git a/tests/test_dj_database_url.py b/tests/test_dj_database_url.py index 3953e63..5728a4b 100644 --- a/tests/test_dj_database_url.py +++ b/tests/test_dj_database_url.py @@ -585,6 +585,13 @@ class DatabaseTestSuite(unittest.TestCase): url = dj_database_url.config(ssl_require=True) assert url["OPTIONS"] == {'sslmode': 'require'} + def test_options_int_values(self): + """Ensure that options with integer values are parsed correctly.""" + url = dj_database_url.parse( + "mysql://user:pw@127.0.0.1:15036/db?connect_timout=3" + ) + assert url["OPTIONS"] == {'connect_timout': 3} + if __name__ == "__main__": unittest.main()