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

`OPTIONS` key type for `DBConfig` is potentially incorrect #218

Closed kezabelle closed 11 months ago

kezabelle commented 1 year ago

2.0.0 introduces DBConfig as a TypedDict, specified as:

class DBConfig(TypedDict, total=False):
...
    OPTIONS: Optional[Dict[str, Any]]

As far as I can tell from a cursory reading of parse, it cannot be Optional (None) - options is always at least an empty directory ({}), but it can be NotRequired as a key (which is AFAIK already handled by the class being total=False) because if it is empty it just won't be set.

This came up for me because I have some code which does:

if "OPTIONS" in parsed_db_config:
    parsed_db_config["OPTIONS"].update(...some_stuff)

which gets raised as an error:

error: Item "None" of "Optional[Dict[str, Any]]" has no attribute "update"  [union-attr]

Here's the totality of options being used and then potentially bound to the OPTIONS key: https://github.com/jazzband/dj-database-url/blob/9b0f325910b388832f9bed7f580cc8e8d15cb2ff/dj_database_url/__init__.py#L159-L182

palfrey commented 1 year ago

https://github.com/jazzband/dj-database-url/pull/232 will fix this