jazzband / dj-database-url

Use Database URLs in your Django Application.
https://pypi.org/project/dj-database-url/
BSD 3-Clause "New" or "Revised" License
1.49k stars 205 forks source link

Option value `connect_timeout` returned as string instead of int #222

Closed estahn closed 1 year ago

estahn commented 1 year ago

Version: 1.3.0 and 2.0.0

Example:

import dj_database_url

print(dj_database_url.parse("mysql://user:pw@127.0.0.1:15036/db?connect_timout=3"))

Returns:

{'NAME': 'db', 'USER': 'user', 'PASSWORD': 'pw', 'HOST': '127.0.0.1', 'PORT': 15036, 'CONN_MAX_AGE': 0, 'CONN_HEALTH_CHECKS': False, 'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {'connect_timout': '3'}}

Expected:

{'NAME': 'db', 'USER': 'user', 'PASSWORD': 'pw', 'HOST': '127.0.0.1', 'PORT': 15036, 'CONN_MAX_AGE': 0, 'CONN_HEALTH_CHECKS': False, 'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {'connect_timout': 3}}

Error:

  ...
  File ".venv/lib/python3.9/site-packages/django/db/backends/mysql/base.py", line 247, in get_new_connection
    connection = Database.connect(**conn_params)
  File ".venv/lib/python3.9/site-packages/MySQLdb/__init__.py", line 123, in Connect
    return Connection(*args, **kwargs)
  File ".venv/lib/python3.9/site-packages/MySQLdb/connections.py", line 185, in __init__
    super().__init__(*args, **kwargs2)
TypeError: an integer is required (got type str)

Suggested fix:

try:
   value = int(value)
except (TypeError, ValueError):
   value = value
mattseymour commented 1 year ago

Going to start building a release for this now. v2.1.0

estahn commented 1 year ago

Awesome, thanks @mattseymour