python-dotenv icon indicating copy to clipboard operation
python-dotenv copied to clipboard

dotenv_values returns an OrderedDict

Open kwisatz opened this issue 3 years ago • 4 comments

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": "[email protected]"}

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': '****'}

kwisatz avatar Aug 31 '21 15:08 kwisatz

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.

bbc2 avatar Oct 23 '21 09:10 bbc2

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/

johnziebro avatar Oct 28 '21 17:10 johnziebro

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

ethsanders avatar Nov 15 '21 18:11 ethsanders

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.

cosama avatar May 06 '22 05:05 cosama