Expressions and variable interpolation¶
Tip
Code for examples on this page can be found in examples/22_expressions.
confarg can process expressions, which are declared within ${...}.
These expressions allow you to deduce configuration values from other parts of the configuration.
Suppose we have a configuration composed of two floating point values:
You can set the value of value2 based on an expression relying on value1, for example.
The expression does not need to come after the variable it references.
Having value1 referring to value2 works too.
It's OK to have expressions relying on variables that are themselves expressions. For example, if we now have three input float values, the following configuration is valid.
$ uv run three_floats.py --config chained_expressions.yaml
Config(value1=4.5, value2=3.0, value3=2.0)
If your chain of expressions contains a loop, confarg will complain.
The expressions can reach across definition boundaries.
For example, a configuration file can define value1, and we can define value2 based on an expression over value1 from the command line.
$ uv run two_floats.py --config cli_expression.yaml --value2 '${value1 * 1.5}'
Config(value1=3.0, value2=4.5)
The opposite also works: we can define value1 as an expression of value2, which is defined late on the command line.
Even if value2 is already defined in the configuration file, the final value of value2 is taken into account in the expression.
$ uv run two_floats.py --config derived_value1.yaml
Config(value1=4.5, value2=3.0)
$ uv run two_floats.py --config derived_value1.yaml --value2 6
Config(value1=9.0, value2=6.0)
Referencing a whole section¶
An expression references a path, and that path does not have to point at a single value.
${database} substitutes the whole section.
$ uv run struct_ref.py --config section_reference.yaml
Config(database=Database(host='localhost', port=5432),
service=Service(database=Database(host='localhost', port=5432)))
same object: False
Note the last line.
service.database is a new Database built from the same data — it is not database itself.
Expressions are resolved over the configuration data, before any object is constructed, so each place that references a section gets its own object.
Changing one afterwards does not change the other, and confarg offers no way to share a single instance between two fields.
Referencing a whole section is a starting point, not a verbatim copy: you can override parts of it by naming the fields you want to keep.