@@ -33,13 +33,15 @@ def with_warn_for_invalid_lines(mappings: Iterator[Binding]) -> Iterator[Binding
33
33
class DotEnv ():
34
34
def __init__ (
35
35
self ,
36
- dotenv_path : Union [str , _PathLike , io .StringIO ],
36
+ dotenv_path : Optional [Union [str , _PathLike ]],
37
+ stream : Optional [IO [str ]] = None ,
37
38
verbose : bool = False ,
38
39
encoding : Union [None , str ] = None ,
39
40
interpolate : bool = True ,
40
41
override : bool = True ,
41
42
) -> 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]]
43
45
self ._dict = None # type: Optional[Dict[str, Optional[str]]]
44
46
self .verbose = verbose # type: bool
45
47
self .encoding = encoding # type: Union[None, str]
@@ -48,14 +50,17 @@ def __init__(
48
50
49
51
@contextmanager
50
52
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 ):
54
54
with io .open (self .dotenv_path , encoding = self .encoding ) as stream :
55
55
yield stream
56
+ elif self .stream is not None :
57
+ yield self .stream
56
58
else :
57
59
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
+ )
59
64
yield io .StringIO ('' )
60
65
61
66
def dict (self ) -> Dict [str , Optional [str ]]:
@@ -290,7 +295,7 @@ def _is_interactive():
290
295
291
296
def load_dotenv (
292
297
dotenv_path : Union [str , _PathLike , None ] = None ,
293
- stream : Optional [io . StringIO ] = None ,
298
+ stream : Optional [IO [ str ] ] = None ,
294
299
verbose : bool = False ,
295
300
override : bool = False ,
296
301
interpolate : bool = True ,
@@ -299,7 +304,8 @@ def load_dotenv(
299
304
"""Parse a .env file and then load all the variables found as environment variables.
300
305
301
306
- *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`.
303
309
- *verbose*: whether to output a warning the .env file is missing. Defaults to
304
310
`False`.
305
311
- *override*: whether to override the system environment variables with the variables
@@ -308,9 +314,12 @@ def load_dotenv(
308
314
309
315
If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
310
316
"""
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
+
312
320
dotenv = DotEnv (
313
- f ,
321
+ dotenv_path = dotenv_path ,
322
+ stream = stream ,
314
323
verbose = verbose ,
315
324
interpolate = interpolate ,
316
325
override = override ,
@@ -321,7 +330,7 @@ def load_dotenv(
321
330
322
331
def dotenv_values (
323
332
dotenv_path : Union [str , _PathLike , None ] = None ,
324
- stream : Optional [io . StringIO ] = None ,
333
+ stream : Optional [IO [ str ] ] = None ,
325
334
verbose : bool = False ,
326
335
interpolate : bool = True ,
327
336
encoding : Optional [str ] = "utf-8" ,
@@ -338,9 +347,12 @@ def dotenv_values(
338
347
339
348
If both `dotenv_path` and `stream`, `find_dotenv()` is used to find the .env file.
340
349
"""
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
+
342
353
return DotEnv (
343
- f ,
354
+ dotenv_path = dotenv_path ,
355
+ stream = stream ,
344
356
verbose = verbose ,
345
357
interpolate = interpolate ,
346
358
override = True ,
0 commit comments