Skip to content
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

Add a proper interpolation option #510

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from

Conversation

potasiak
Copy link

Adds a proper interpolation mechanism that can substitute variables in the middle of values of other variables.

The current "proxy" mechanism, where a variable is substituted when set to a name of another variable prefixed with a dollar sign (VAR_A=$VAR_B) is retained, and used by default. The interpolation mechanism has to be explicitly enabled by setting Env.interpolation to True. This is done to ensure backward compatibility with all previous versions, except for v0.11.0 and v0.11.1 that have been yanked from PyPI.

This is meant to be a properly implemented "variable expansion" mechanism that can be toggled on and off, as was intended in #468 and #419.

It does not re-introduce issues that caused #485 and #490.

In addition to the instance-wide Env.interpolation setting, each method that retrieves a value from the environment got optional parameter interpolate that can enable or disable interpolation independently from the Env.interpolation. When interpolate parameter is not passed as an argument, Env.interpolation is used. This will let users handle special cases, where they need to selectively enable or disable the new interpolation mechanism.

When a variable referenced in a value of another variable is not set, an ImproperlyConfigured exception is raised. It can be disabled (missing variables will be ignored) by setting Env.raise_on_missing to False. Could not figure out a better name for now. If you have an idea - let me know.

When a variable references itself in its value, an ImproperlyConfigured exception is raised.

Variables prefixed with \$ instead of $ are ignored.

Every interpolated value is cached (up to 128 recently used variables).

@github-actions github-actions bot changed the base branch from main to develop November 11, 2023 17:00
Copy link

Your PR was set to target main, PRs should be target develop
The base branch of this PR has been automatically changed to develop, please check that there are no merge conflicts.

@potasiak
Copy link
Author

After some more thinking about this, I've started to wonder if doing the interpolation when reading env, instead of on Env.get_value() call, wouldn't be a better idea. Let me know what you think.

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.

3 participants