diff --git a/.editorconfig b/.editorconfig deleted file mode 100644 index e2764043..00000000 --- a/.editorconfig +++ /dev/null @@ -1,17 +0,0 @@ -# see: http://editorconfig.org - -root = true - -[*] -charset = utf-8 -end_of_line = lf -insert_final_newline = true -trim_trailing_whitespace = true - -[*.{py,rst,ini}] -indent_style = space -indent_size = 4 - -[*.yml] -indent_style = space -indent_size = 2 diff --git a/src/dotenv/main.py b/src/dotenv/main.py index 491634d9..e8b9fa81 100644 --- a/src/dotenv/main.py +++ b/src/dotenv/main.py @@ -5,7 +5,7 @@ import stat import sys import tempfile -from collections import OrderedDict +from collections import ChainMap, OrderedDict from contextlib import contextmanager from typing import IO, Dict, Iterable, Iterator, Mapping, Optional, Tuple, Union @@ -290,20 +290,26 @@ def resolve_variables( values: Iterable[Tuple[str, Optional[str]]], override: bool, ) -> Mapping[str, Optional[str]]: + """ + Resolve interpolated variables from a .env file, + iterating over tuples with pairs that represent the key and value of each variable in the .env file. + + If override is True, .env variables take priority over system environment variables. + """ + # test new_values: Dict[str, Optional[str]] = {} + env: ChainMap[str, Optional[str]] = ( + ChainMap(new_values, os.environ) # type: ignore + if override + else ChainMap(os.environ, new_values) # type: ignore + ) + for name, value in values: if value is None: result = None else: atoms = parse_variables(value) - env: Dict[str, Optional[str]] = {} - if override: - env.update(os.environ) # type: ignore - env.update(new_values) - else: - env.update(new_values) - env.update(os.environ) # type: ignore result = "".join(atom.resolve(env) for atom in atoms) new_values[name] = result