theskumar / python-dotenv

Reads key-value pairs from a .env file and can set them as environment variables. It helps in developing applications following the 12-factor principles.
https://saurabh-kumar.com/python-dotenv/
BSD 3-Clause "New" or "Revised" License
7.49k stars 425 forks source link

dotenv_values returns an OrderedDict #355

Open kwisatz opened 3 years ago

kwisatz commented 3 years ago

From this example the value returned by dotenv_values is supposed to be a dict, but in reality (python 3.9) the value is of type OrderedDict:

from dotenv import dotenv_values

config = dotenv_values(".env")  # config = {"USER": "foo", "EMAIL": "foo@example.org"}

This is what I get:

from dotenv import dotenv_values

config = dotenv_values('.env') # config = OrderedDict([('consumer_key', '****'), ('consumer_secret', '****'), ('access_token', '****'), ('access_secret', '****')])

dict(config) # {'consumer_key': '****', 'consumer_secret': '****', 'access_token': '****', 'access_secret': '****'}
bbc2 commented 2 years ago

Technically, an OrderedDict is a dict (isinstance(OrderedDict(), dict) is True), but I agree this may be confusing.

We used to need the OrderedDict for Python 2, but we don't need it anymore now that we only support Python 3, so I'll look into using a regular dict instead.

johnziebro commented 2 years ago

One caveat; in Python 3, if using Jupyter iPython kernel, insert ordering of a regular dict is not respected, so OrderedDict is still useful. I experienced this issue recently. Ref: https://gandenberger.org/2018/03/10/ordered-dicts-vs-ordereddict/

ethsanders commented 2 years ago

While this is probably more an issue of IPython, it should be considered.

cosama commented 2 years ago

I just checked this in a jupyter notebook and it doesn't seem to be an issue anymore. I think with @johnziebro point mute, moving to dict would be reasonable. I haven't seen OrderedDict in quite a while and was surprised getting one here.