22
22
logger = logging .getLogger (__name__ )
23
23
24
24
25
- def with_warn_for_invalid_lines (mappings : Iterator [Binding ]) -> Iterator [Binding ]:
25
+ class DotEnvParseError (Exception ):
26
+ def __init__ (self , message ):
27
+ super ().__init__ (message )
28
+
29
+
30
+ def with_warn_for_invalid_lines (mappings : Iterator [Binding ], raise_on_error : bool = False ) -> Iterator [Binding ]:
26
31
for mapping in mappings :
27
32
if mapping .error :
28
- logger .warning (
29
- "Python-dotenv could not parse statement starting at line %s" ,
30
- mapping .original .line ,
31
- )
33
+ if raise_on_error :
34
+ raise DotEnvParseError (
35
+ "Python-dotenv could not parse statement starting at line %s" %
36
+ mapping .original .line ,
37
+ )
38
+ else :
39
+ logger .warning (
40
+ "Python-dotenv could not parse statement starting at line %s" ,
41
+ mapping .original .line ,
42
+ )
32
43
yield mapping
33
44
34
45
@@ -41,6 +52,7 @@ def __init__(
41
52
encoding : Optional [str ] = None ,
42
53
interpolate : bool = True ,
43
54
override : bool = True ,
55
+ raise_on_error : bool = False ,
44
56
) -> None :
45
57
self .dotenv_path : Optional [StrPath ] = dotenv_path
46
58
self .stream : Optional [IO [str ]] = stream
@@ -49,6 +61,7 @@ def __init__(
49
61
self .encoding : Optional [str ] = encoding
50
62
self .interpolate : bool = interpolate
51
63
self .override : bool = override
64
+ self .raise_on_error : bool = raise_on_error
52
65
53
66
@contextmanager
54
67
def _get_stream (self ) -> Iterator [IO [str ]]:
@@ -81,7 +94,7 @@ def dict(self) -> Dict[str, Optional[str]]:
81
94
82
95
def parse (self ) -> Iterator [Tuple [str , Optional [str ]]]:
83
96
with self ._get_stream () as stream :
84
- for mapping in with_warn_for_invalid_lines (parse_stream (stream )):
97
+ for mapping in with_warn_for_invalid_lines (parse_stream (stream ), self . raise_on_error ):
85
98
if mapping .key is not None :
86
99
yield mapping .key , mapping .value
87
100
@@ -325,6 +338,7 @@ def load_dotenv(
325
338
override : bool = False ,
326
339
interpolate : bool = True ,
327
340
encoding : Optional [str ] = "utf-8" ,
341
+ raise_on_error : bool = False ,
328
342
) -> bool :
329
343
"""Parse a .env file and then load all the variables found as environment variables.
330
344
@@ -354,6 +368,7 @@ def load_dotenv(
354
368
interpolate = interpolate ,
355
369
override = override ,
356
370
encoding = encoding ,
371
+ raise_on_error = raise_on_error ,
357
372
)
358
373
return dotenv .set_as_environment_variables ()
359
374
@@ -364,6 +379,7 @@ def dotenv_values(
364
379
verbose : bool = False ,
365
380
interpolate : bool = True ,
366
381
encoding : Optional [str ] = "utf-8" ,
382
+ raise_on_error : bool = False ,
367
383
) -> Dict [str , Optional [str ]]:
368
384
"""
369
385
Parse a .env file and return its content as a dict.
@@ -391,4 +407,5 @@ def dotenv_values(
391
407
interpolate = interpolate ,
392
408
override = True ,
393
409
encoding = encoding ,
410
+ raise_on_error = raise_on_error ,
394
411
).dict ()
0 commit comments