@@ -50,7 +50,7 @@ class DotEnv():
5050 def __init__ (self , dotenv_path , verbose = False , encoding = None ):
5151 # type: (Union[Text, _PathLike, _StringIO], bool, Union[None, Text]) -> None
5252 self .dotenv_path = dotenv_path # type: Union[Text,_PathLike, _StringIO]
53- self ._dict = None # type: Optional[Dict[Text, Text]]
53+ self ._dict = None # type: Optional[Dict[Text, Optional[ Text] ]]
5454 self .verbose = verbose # type: bool
5555 self .encoding = encoding # type: Union[None, Text]
5656
@@ -68,7 +68,7 @@ def _get_stream(self):
6868 yield StringIO ('' )
6969
7070 def dict (self ):
71- # type: () -> Dict[Text, Text]
71+ # type: () -> Dict[Text, Optional[ Text] ]
7272 """Return dotenv as dict"""
7373 if self ._dict :
7474 return self ._dict
@@ -78,10 +78,10 @@ def dict(self):
7878 return self ._dict
7979
8080 def parse (self ):
81- # type: () -> Iterator[Tuple[Text, Text]]
81+ # type: () -> Iterator[Tuple[Text, Optional[ Text] ]]
8282 with self ._get_stream () as stream :
8383 for mapping in with_warn_for_invalid_lines (parse_stream (stream )):
84- if mapping .key is not None and mapping . value is not None :
84+ if mapping .key is not None :
8585 yield mapping .key , mapping .value
8686
8787 def set_as_environment_variables (self , override = False ):
@@ -92,7 +92,8 @@ def set_as_environment_variables(self, override=False):
9292 for k , v in self .dict ().items ():
9393 if k in os .environ and not override :
9494 continue
95- os .environ [to_env (k )] = to_env (v )
95+ if v is not None :
96+ os .environ [to_env (k )] = to_env (v )
9697
9798 return True
9899
@@ -197,7 +198,7 @@ def unset_key(dotenv_path, key_to_unset, quote_mode="always"):
197198
198199
199200def resolve_nested_variables (values ):
200- # type: (Dict[Text, Text]) -> Dict[Text, Text]
201+ # type: (Dict[Text, Optional[ Text]] ) -> Dict[Text, Optional[ Text] ]
201202 def _replacement (name ):
202203 # type: (Text) -> Text
203204 """
@@ -206,7 +207,7 @@ def _replacement(name):
206207 then look into the dotenv variables
207208 """
208209 ret = os .getenv (name , new_values .get (name , "" ))
209- return ret
210+ return ret # type: ignore
210211
211212 def _re_sub_callback (match_object ):
212213 # type: (Match[Text]) -> Text
@@ -219,7 +220,7 @@ def _re_sub_callback(match_object):
219220 new_values = {}
220221
221222 for k , v in values .items ():
222- new_values [k ] = __posix_variable .sub (_re_sub_callback , v )
223+ new_values [k ] = __posix_variable .sub (_re_sub_callback , v ) if v is not None else None
223224
224225 return new_values
225226
@@ -301,6 +302,6 @@ def load_dotenv(dotenv_path=None, stream=None, verbose=False, override=False, **
301302
302303
303304def dotenv_values (dotenv_path = None , stream = None , verbose = False , ** kwargs ):
304- # type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, Union[None, Text]) -> Dict[Text, Text]
305+ # type: (Union[Text, _PathLike, None], Optional[_StringIO], bool, Union[None, Text]) -> Dict[Text, Optional[ Text] ]
305306 f = dotenv_path or stream or find_dotenv ()
306307 return DotEnv (f , verbose = verbose , ** kwargs ).dict ()
0 commit comments