@@ -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 ]]:
@@ -304,7 +309,7 @@ def _is_interactive():
304309
305310def load_dotenv (
306311 dotenv_path : Union [str , _PathLike , None ] = None ,
307- stream : Optional [io . StringIO ] = None ,
312+ stream : Optional [IO [ str ] ] = None ,
308313 verbose : bool = False ,
309314 override : bool = False ,
310315 interpolate : bool = True ,
@@ -313,7 +318,8 @@ def load_dotenv(
313318 """Parse a .env file and then load all the variables found as environment variables.
314319
315320 - *dotenv_path*: absolute or relative path to .env file.
316- - *stream*: `StringIO` object with .env content, used if `dotenv_path` is `None`.
321+ - *stream*: Text stream (such as `io.StringIO`) with .env content, used if
322+ `dotenv_path` is `None`.
317323 - *verbose*: whether to output a warning the .env file is missing. Defaults to
318324 `False`.
319325 - *override*: whether to override the system environment variables with the variables
@@ -322,9 +328,12 @@ def load_dotenv(
322328
323329 If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
324330 """
325- f = dotenv_path or stream or find_dotenv ()
331+ if dotenv_path is None and stream is None :
332+ dotenv_path = find_dotenv ()
333+
326334 dotenv = DotEnv (
327- f ,
335+ dotenv_path = dotenv_path ,
336+ stream = stream ,
328337 verbose = verbose ,
329338 interpolate = interpolate ,
330339 override = override ,
@@ -335,7 +344,7 @@ def load_dotenv(
335344
336345def dotenv_values (
337346 dotenv_path : Union [str , _PathLike , None ] = None ,
338- stream : Optional [io . StringIO ] = None ,
347+ stream : Optional [IO [ str ] ] = None ,
339348 verbose : bool = False ,
340349 interpolate : bool = True ,
341350 encoding : Optional [str ] = "utf-8" ,
@@ -352,9 +361,12 @@ def dotenv_values(
352361
353362 If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
354363 """
355- f = dotenv_path or stream or find_dotenv ()
364+ if dotenv_path is None and stream is None :
365+ dotenv_path = find_dotenv ()
366+
356367 return DotEnv (
357- f ,
368+ dotenv_path = dotenv_path ,
369+ stream = stream ,
358370 verbose = verbose ,
359371 interpolate = interpolate ,
360372 override = True ,
0 commit comments