1010from typing import IO , Dict , Iterable , Iterator , Mapping , Optional , Tuple , Union
1111
1212from .parser import Binding , parse_stream
13- from .variables import parse_variables
13+ from .variables import parse_variables , set_variable_name_pattern
1414
1515# A type alias for a string path to be used for the paths in this file.
1616# These paths may flow to `open()` and `shutil.move()`; `shutil.move()`
@@ -341,6 +341,7 @@ def load_dotenv(
341341 override : bool = False ,
342342 interpolate : bool = True ,
343343 encoding : Optional [str ] = "utf-8" ,
344+ varname_pattern : Optional [str ] = None ,
344345) -> bool :
345346 """Parse a .env file and then load all the variables found as environment variables.
346347
@@ -352,6 +353,8 @@ def load_dotenv(
352353 override: Whether to override the system environment variables with the variables
353354 from the `.env` file.
354355 encoding: Encoding to be used to read the file.
356+ varname_pattern: Optional regex pattern to restrict variable names.
357+ If `None`, the existing pattern is used. The pattern set here is persistent.
355358 Returns:
356359 Bool: True if at least one environment variable is set else False
357360
@@ -380,6 +383,9 @@ def load_dotenv(
380383 override = override ,
381384 encoding = encoding ,
382385 )
386+
387+ if varname_pattern is not None :
388+ set_variable_name_pattern (varname_pattern )
383389 return dotenv .set_as_environment_variables ()
384390
385391
@@ -389,6 +395,7 @@ def dotenv_values(
389395 verbose : bool = False ,
390396 interpolate : bool = True ,
391397 encoding : Optional [str ] = "utf-8" ,
398+ varname_pattern : Optional [str ] = None ,
392399) -> Dict [str , Optional [str ]]:
393400 """
394401 Parse a .env file and return its content as a dict.
@@ -402,13 +409,17 @@ def dotenv_values(
402409 stream: `StringIO` object with .env content, used if `dotenv_path` is `None`.
403410 verbose: Whether to output a warning if the .env file is missing.
404411 encoding: Encoding to be used to read the file.
412+ varname_pattern: Optional regex pattern to restrict variable names.
413+ If `None`, the existing pattern is used. The pattern set here is persistent.
405414
406415 If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
407416 .env file.
408417 """
409418 if dotenv_path is None and stream is None :
410419 dotenv_path = find_dotenv ()
411420
421+ if varname_pattern is not None :
422+ set_variable_name_pattern (varname_pattern )
412423 return DotEnv (
413424 dotenv_path = dotenv_path ,
414425 stream = stream ,
0 commit comments