Skip to content

feat: support library variables #156

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

Merged
merged 1 commit into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/basic/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ state = PipelineRunState(
])
```

The following `RunParameterType` values are available:

* `RunParameterType.Parameter` to set a pipeline parameter: `@pipeline().parameters.<parameter_name>`
* `RunParameterType.Global` to set a global parameter: `@pipeline().globalParameters.<global_parameter_name>`
* `RunParameterType.System` to set a system parameter: `@pipeline().<system_parameter_name>`
* `RunParameterType.Dataset` to set a dataset: `@dataset().<dataset_name>`
* `RunParameterType.LinkedService` to set a linked service: `@linkedService().<linked_service_name>`
* `RunParameterType.LibraryVariables` to set a library variable: `@pipeline().libraryVariables.<variable_name>`

### Activity results

In the scenario where an activity has an expression that references the output of another activity `activity('another_activity_name').output.some_field`, the `PipelineRunState` can be used to configure the output of the `another_activity_name` activity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ def evaluate(self, expression: str, state: PipelineRunState) -> str:
and not state._contains_parameter_with_type_and_name(RunParameterType.Global, property_name)
):
raise ParameterNotFoundError(RunParameterType.Global, property_name) from e
elif (
f"pipeline().libraryVariables.{property_name}" in expression
and not state._contains_parameter_with_type_and_name(
RunParameterType.LibraryVariables, property_name
)
):
raise ParameterNotFoundError(RunParameterType.LibraryVariables, property_name) from e
elif (
f"pipeline().dataset.{property_name}" in expression
and not state._contains_parameter_with_type_and_name(RunParameterType.Dataset, property_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def evaluate(expression: str, state: PipelineRunState) -> Union[str, int, float,
parameters = {
"globalParameters": {},
"parameters": {},
"libraryVariables": {},
"dataset": {},
"linkedService": {},
}
Expand All @@ -27,6 +28,8 @@ def evaluate(expression: str, state: PipelineRunState) -> Union[str, int, float,
parameters["globalParameters"][parameter.name] = parameter.value
elif parameter.type == RunParameterType.Pipeline:
parameters["parameters"][parameter.name] = parameter.value
elif parameter.type == RunParameterType.LibraryVariables:
parameters["libraryVariables"][parameter.name] = parameter.value
elif parameter.type == RunParameterType.Dataset:
parameters["dataset"][parameter.name] = parameter.value
elif parameter.type == RunParameterType.LinkedService:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class RunParameterType(str, Enum, metaclass=CaseInsensitiveEnumMeta):
Dataset = "Dataset"
LinkedService = "LinkedService"
System = "System"
LibraryVariables = "LibraryVariables"

def __str__(self) -> str:
"""Get the string representation of the enum.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@
"value",
id="pipeline_global_parameters_reference",
),
p(
"@pipeline().libraryVariables.variable",
PipelineRunState(
parameters=[
RunParameter(RunParameterType.LibraryVariables, "variable", "value"),
]
),
"@pipeline().libraryVariables.variable",
"value",
id="pipeline_library_variables_reference",
),
p(
"@variables('variable')",
PipelineRunState(
Expand Down Expand Up @@ -724,6 +735,37 @@ def test_evaluate_system_variable_raises_exception_when_parameter_not_set() -> N
assert str(exinfo.value) == "Parameter: 'RunId' of type 'RunParameterType.System' not found"


def test_evaluate_library_variable() -> None:
# Arrange
expression = "@pipeline().libraryVariables.variable"
expression_runtime = ExpressionRuntime()
state = PipelineRunState(
parameters=[
RunParameter(RunParameterType.LibraryVariables, "variable", "value"),
]
)

# Act
evaluated_value = expression_runtime.evaluate(expression, state)

# Assert
assert evaluated_value == "value"


def test_evaluate_library_variable_raises_exception_when_parameter_not_set() -> None:
# Arrange
expression = "@pipeline().libraryVariables.variable"
expression_runtime = ExpressionRuntime()
state = PipelineRunState()

# Act
with pytest.raises(ParameterNotFoundError) as exinfo:
expression_runtime.evaluate(expression, state)

# Assert
assert str(exinfo.value) == "Parameter: 'variable' of type 'RunParameterType.LibraryVariables' not found"


@pytest.mark.parametrize(
["json_expression", "accessor", "expected"],
[
Expand Down Expand Up @@ -967,6 +1009,7 @@ def test_conditional_expression_with_branching(
[
(RunParameterType.Pipeline, "pipeline().parameters"),
(RunParameterType.Global, "pipeline().globalParameters"),
(RunParameterType.LibraryVariables, "pipeline().libraryVariables"),
(RunParameterType.Dataset, "pipeline().dataset"),
(RunParameterType.LinkedService, "pipeline().linkedService"),
(RunParameterType.System, "pipeline()"),
Expand All @@ -990,6 +1033,7 @@ def test_complex_expression_with_missing_parameter(run_parameter_type: RunParame
"run_parameter_type, parameter_prefix",
[
(RunParameterType.Global, "pipeline().globalParameters"),
(RunParameterType.LibraryVariables, "pipeline().libraryVariables"),
(RunParameterType.Dataset, "pipeline().dataset"),
(RunParameterType.LinkedService, "pipeline().linkedService"),
(RunParameterType.System, "pipeline()"),
Expand Down