@@ -33,13 +33,15 @@ def with_warn_for_invalid_lines(mappings: Iterator[Binding]) -> Iterator[Binding
3333class DotEnv ():
3434 def __init__ (
3535 self ,
36- dotenv_path : Union [str , _PathLike , io .StringIO ],
36+ dotenv_path : Optional [Union [str , _PathLike ]],
37+ stream : Optional [IO [str ]] = None ,
3738 verbose : bool = False ,
3839 encoding : Union [None , str ] = None ,
3940 interpolate : bool = True ,
4041 override : bool = True ,
4142 ) -> None :
42- self .dotenv_path = dotenv_path # type: Union[str,_PathLike, io.StringIO]
43+ self .dotenv_path = dotenv_path # type: Optional[Union[str, _PathLike]]
44+ self .stream = stream # type: Optional[IO[str]]
4345 self ._dict = None # type: Optional[Dict[str, Optional[str]]]
4446 self .verbose = verbose # type: bool
4547 self .encoding = encoding # type: Union[None, str]
@@ -48,14 +50,17 @@ def __init__(
4850
4951 @contextmanager
5052 def _get_stream (self ) -> Iterator [IO [str ]]:
51- if isinstance (self .dotenv_path , io .StringIO ):
52- yield self .dotenv_path
53- elif os .path .isfile (self .dotenv_path ):
53+ if self .dotenv_path and os .path .isfile (self .dotenv_path ):
5454 with io .open (self .dotenv_path , encoding = self .encoding ) as stream :
5555 yield stream
56+ elif self .stream is not None :
57+ yield self .stream
5658 else :
5759 if self .verbose :
58- logger .info ("Python-dotenv could not find configuration file %s." , self .dotenv_path or '.env' )
60+ logger .info (
61+ "Python-dotenv could not find configuration file %s." ,
62+ self .dotenv_path or '.env' ,
63+ )
5964 yield io .StringIO ('' )
6065
6166 def dict (self ) -> Dict [str , Optional [str ]]:
@@ -290,7 +295,7 @@ def _is_interactive():
290295
291296def load_dotenv (
292297 dotenv_path : Union [str , _PathLike , None ] = None ,
293- stream : Optional [io . StringIO ] = None ,
298+ stream : Optional [IO [ str ] ] = None ,
294299 verbose : bool = False ,
295300 override : bool = False ,
296301 interpolate : bool = True ,
@@ -299,7 +304,8 @@ def load_dotenv(
299304 """Parse a .env file and then load all the variables found as environment variables.
300305
301306 - *dotenv_path*: absolute or relative path to .env file.
302- - *stream*: `StringIO` object with .env content, used if `dotenv_path` is `None`.
307+ - *stream*: Text stream (such as `io.StringIO`) with .env content, used if
308+ `dotenv_path` is `None`.
303309 - *verbose*: whether to output a warning the .env file is missing. Defaults to
304310 `False`.
305311 - *override*: whether to override the system environment variables with the variables
@@ -308,9 +314,12 @@ def load_dotenv(
308314
309315 If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
310316 """
311- f = dotenv_path or stream or find_dotenv ()
317+ if dotenv_path is None and stream is None :
318+ dotenv_path = find_dotenv ()
319+
312320 dotenv = DotEnv (
313- f ,
321+ dotenv_path = dotenv_path ,
322+ stream = stream ,
314323 verbose = verbose ,
315324 interpolate = interpolate ,
316325 override = override ,
@@ -321,7 +330,7 @@ def load_dotenv(
321330
322331def dotenv_values (
323332 dotenv_path : Union [str , _PathLike , None ] = None ,
324- stream : Optional [io . StringIO ] = None ,
333+ stream : Optional [IO [ str ] ] = None ,
325334 verbose : bool = False ,
326335 interpolate : bool = True ,
327336 encoding : Optional [str ] = "utf-8" ,
@@ -338,9 +347,12 @@ def dotenv_values(
338347
339348 If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
340349 """
341- f = dotenv_path or stream or find_dotenv ()
350+ if dotenv_path is None and stream is None :
351+ dotenv_path = find_dotenv ()
352+
342353 return DotEnv (
343- f ,
354+ dotenv_path = dotenv_path ,
355+ stream = stream ,
344356 verbose = verbose ,
345357 interpolate = interpolate ,
346358 override = True ,
0 commit comments