@@ -40,6 +40,7 @@ def __init__(
4040 encoding : Optional [str ] = None ,
4141 interpolate : bool = True ,
4242 override : bool = True ,
43+ secret_key : str = None
4344 ) -> None :
4445 self .dotenv_path : Optional [StrPath ] = dotenv_path
4546 self .stream : Optional [IO [str ]] = stream
@@ -48,6 +49,7 @@ def __init__(
4849 self .encoding : Optional [str ] = encoding
4950 self .interpolate : bool = interpolate
5051 self .override : bool = override
52+ self .secret_key = secret_key
5153
5254 @contextmanager
5355 def _get_stream (self ) -> Iterator [IO [str ]]:
@@ -82,7 +84,7 @@ def dict(self) -> Dict[str, Optional[str]]:
8284
8385 def parse (self ) -> Iterator [Tuple [str , Optional [str ]]]:
8486 with self ._get_stream () as stream :
85- for mapping in with_warn_for_invalid_lines (parse_stream (stream )):
87+ for mapping in with_warn_for_invalid_lines (parse_stream (stream , self . secret_key )):
8688 if mapping .key is not None :
8789 yield mapping .key , mapping .value
8890
@@ -156,6 +158,7 @@ def set_key(
156158 quote_mode : str = "always" ,
157159 export : bool = False ,
158160 encoding : Optional [str ] = "utf-8" ,
161+ secret_key : str = None ,
159162) -> Tuple [Optional [bool ], str , str ]:
160163 """
161164 Adds or Updates a key/value to the given .env
@@ -182,7 +185,7 @@ def set_key(
182185 with rewrite (dotenv_path , encoding = encoding ) as (source , dest ):
183186 replaced = False
184187 missing_newline = False
185- for mapping in with_warn_for_invalid_lines (parse_stream (source )):
188+ for mapping in with_warn_for_invalid_lines (parse_stream (source , secret_key )):
186189 if mapping .key == key_to_set :
187190 dest .write (line_out )
188191 replaced = True
@@ -202,6 +205,7 @@ def unset_key(
202205 key_to_unset : str ,
203206 quote_mode : str = "always" ,
204207 encoding : Optional [str ] = "utf-8" ,
208+ secret_key : str = None
205209) -> Tuple [Optional [bool ], str ]:
206210 """
207211 Removes a given key from the given `.env` file.
@@ -215,7 +219,7 @@ def unset_key(
215219
216220 removed = False
217221 with rewrite (dotenv_path , encoding = encoding ) as (source , dest ):
218- for mapping in with_warn_for_invalid_lines (parse_stream (source )):
222+ for mapping in with_warn_for_invalid_lines (parse_stream (source , secret_key )):
219223 if mapping .key == key_to_unset :
220224 removed = True
221225 else :
@@ -329,6 +333,7 @@ def load_dotenv(
329333 override : bool = False ,
330334 interpolate : bool = True ,
331335 encoding : Optional [str ] = "utf-8" ,
336+ secret_key : Optional [str ] = os .environ .get ('DOTENV_SECRET' ),
332337) -> bool :
333338 """Parse a .env file and then load all the variables found as environment variables.
334339
@@ -358,6 +363,7 @@ def load_dotenv(
358363 interpolate = interpolate ,
359364 override = override ,
360365 encoding = encoding ,
366+ secret_key = secret_key
361367 )
362368 return dotenv .set_as_environment_variables ()
363369
@@ -368,6 +374,7 @@ def dotenv_values(
368374 verbose : bool = False ,
369375 interpolate : bool = True ,
370376 encoding : Optional [str ] = "utf-8" ,
377+ secret_key : Optional [str ] = None
371378) -> Dict [str , Optional [str ]]:
372379 """
373380 Parse a .env file and return its content as a dict.
@@ -395,4 +402,5 @@ def dotenv_values(
395402 interpolate = interpolate ,
396403 override = True ,
397404 encoding = encoding ,
405+ secret_key = secret_key
398406 ).dict ()
0 commit comments