Skip to content

feat: option to change POSIX variable name regex - facilitate passthrough #573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

space88man
Copy link

@space88man space88man commented Aug 16, 2025

Background

There are certain situations where a string ${xxxxxxxx} should not be a candidate for variable substitution. This is very common in Spring Boot applications where ${a.b.c.d} is property placeholder syntax

This PR allows the user to narrow the scope of dotenv.variables._posix_variable so that some strings that resemble POSIX variable substitution can be treated literally.

Before this PR:

# I'm looking at you Spring Boot
APP_SOME_VAR=${app.some.other.var}

# after load_dotenv
# dotenv.variables._posix_variable is quite greedy
APP_SOME_VAR=

After this PR:

# somewhere in  code:
# typical Spring Boot naming convention consists of
# caps, numerals, and underscores
dotenv.set_variable_name_pattern(r"""[A-Z0-9_]+""")

# Spring Boot property placeholder syntax
APP_SOME_VAR=${app.some.other.var}

# after load_dotenv
# literal passthrough
APP_SOME_VAR=${app.some.other.var}

Real-world use case: 12-factor'ing Spring Boot applications where some properties refer to other properties ${a.b.c.d} in their values

Feature Implementation

  • opt-in package level function dotenv.set_variable_name_pattern(....) to set the regex for the naming convention. This is a substring that will be used inside the ${xxxxxx:-yyyyyy} matcher. IOW, this pattern is only used to match the variable name, not the enclosing ${...}
  • revert to default: dotenv.set_variable_name_pattern()
  • added a test for literal passthrough
  • existing tests all passed

This feature does not affect any existing code as it is purely opt-in

Thread Safety

  • this feature sets a module level variable in dotenv.variables so is not thread safe

Deferred Ideas

  • per instance _posix_variable - this would be very intrusive so not in the scope of this feature
  • thread-local _posix_variable - this is a low risk thread-safety enhancement where each thread can set its own _posix_variable

Usage

# before calling load_dotenv or dotenv_values
dotenv.set_variable_name_pattern(r'''[A-Z0-9_]+''') # Spring Boot preset
load_dotenv()
# or
load_dotenv(varname_pattern=r'''[A-Z0-9_]+''')
dotenv_values(varname_pattern=r'''[A-Z0-9_]+''')

# epilogue: reset to package default regex
dotenv.set_variable_name_pattern()

Benefits

  • allow literal passthrough of string segments that look like POSIX substitution

Workaround

Current workaround

## this is horrible
__='${'
APP_SOME_VAR=${__}app.some.other.var}

@space88man space88man changed the title feat: option to change POSIX variable name regix - facilitate passthrough feat: option to change POSIX variable name regex - facilitate passthrough Aug 16, 2025
@space88man space88man force-pushed the feat-var-pattern-regex branch 7 times, most recently from 801b75c to 269d2dc Compare August 16, 2025 06:28
@space88man space88man force-pushed the feat-var-pattern-regex branch from 269d2dc to c34e1a1 Compare August 16, 2025 11:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant