Repository: theskumar/python-dotenv

Issue:
Unexpected Behavior when Parsing Duplicate Key
Hello. I started using this package yesterday and I am sorry if I am missing something simple.
I am trying to read an env file that looks like this:
```
export hello=hi
export greetings=${hello}
export goodbye=bye
export greetings=${goodbye}
```
 To read it, I wrote the following small program:
```python
import os
import dotenv
from pathlib import Path

env_path = './test.env'
dotenv.load_dotenv(dotenv_path=env_path,verbose=True)
print(os.getenv('greetings'))
```
When I run this program, it outputs a single empty line. This happens even when I set override to True. I was expecting the program to output 'bye'. Is this a bug or the expected behavior for handling duplicate keys ? I am currently running 3.7.3 and python-dotenv version 0.10.3. Thank you. 

Discussion:
coveralls: 
[![Coverage Status](https://coveralls.io/builds/31851646/badge)](https://coveralls.io/builds/31851646)

Coverage increased (+0.05%) to 89.855% when pulling **1e99042e5523d97823f1091ae98d5a4badbc2c51 on bbc2:fix-empty-expanded-value-duplicate-key** into **e4bbb8a2aa881409af6fb92933c18e2af6609da8 on theskumar:master**.


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 116d97f..a01d3dc 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,7 +7,9 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
 
 ## [Unreleased]
 
-*No unreleased change at this time.*
+### Fixed
+
+- Fix potentially empty expanded value for duplicate key (#260 by [@bbc]).
 
 ## [0.14.0] - 2020-07-03
 
diff --git a/src/dotenv/main.py b/src/dotenv/main.py
index 8f77e83..607299a 100644
--- a/src/dotenv/main.py
+++ b/src/dotenv/main.py
@@ -18,7 +18,7 @@ logger = logging.getLogger(__name__)
 
 if IS_TYPE_CHECKING:
     from typing import (
-        Dict, Iterator, Match, Optional, Pattern, Union, Text, IO, Tuple
+        Dict, Iterable, Iterator, Match, Optional, Pattern, Union, Text, IO, Tuple
     )
     if sys.version_info >= (3, 6):
         _PathLike = os.PathLike
@@ -83,9 +83,13 @@ class DotEnv():
         if self._dict:
             return self._dict
 
-        values = OrderedDict(self.parse())
-        self._dict = resolve_nested_variables(values) if self.interpolate else values
-        return self._dict
+        if self.interpolate:
+            values = resolve_nested_variables(self.parse())
+        else:
+            values = OrderedDict(self.parse())
+
+        self._dict = values
+        return values
 
     def parse(self):
         # type: () -> Iterator[Tuple[Text, Optional[Text]]]
@@ -211,7 +215,7 @@ def unset_key(dotenv_path, key_to_unset, quote_mode="always"):
 
 
 def resolve_nested_variables(values):
-    # type: (Dict[Text, Optional[Text]]) -> Dict[Text, Optional[Text]]
+    # type: (Iterable[Tuple[Text, Optional[Text]]]) -> Dict[Text, Optional[Text]]
     def _replacement(name, default):
         # type: (Text, Optional[Text]) -> Text
         default = default if default is not None else ""
@@ -229,7 +233,7 @@ def resolve_nested_variables(values):
 
     new_values = {}
 
-    for k, v in values.items():
+    for (k, v) in values:
         new_values[k] = __posix_variable.sub(_re_sub_callback, v) if v is not None else None
 
     return new_values


Test patch:
diff --git a/tests/test_main.py b/tests/test_main.py
index 3a3d059..339d00b 100644
--- a/tests/test_main.py
+++ b/tests/test_main.py
@@ -337,6 +337,8 @@ def test_dotenv_values_file(dotenv_file):
 
         # Re-defined and used in file
         ({"b": "c"}, "b=d\na=${b}", True, {"a": "d", "b": "d"}),
+        ({}, "a=b\na=c\nd=${a}", True, {"a": "c", "d": "c"}),
+        ({}, "a=b\nc=${a}\nd=e\nc=${d}", True, {"a": "b", "c": "e", "d": "e"}),
     ],
 )
 def test_dotenv_values_stream(env, string, interpolate, expected):