Skip to content

Commit

Permalink
rearrange args
Browse files Browse the repository at this point in the history
  • Loading branch information
kevingrismore committed Jan 31, 2025
1 parent 1536c71 commit b651af7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/integrations/prefect-dbt/prefect_dbt/core/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async def aresolve_profiles_yml(
temp_dir_path = Path(temp_dir)
profiles_yml: dict[str, Any] = load_profiles_yml(profiles_dir)
profiles_yml = await resolve_block_document_references(
profiles_yml, replace_with_env_var_call
profiles_yml, value_transformer=replace_with_env_var_call
)
profiles_yml = await resolve_variables(profiles_yml)

Expand Down Expand Up @@ -142,7 +142,9 @@ def resolve_profiles_yml(
temp_dir_path = Path(temp_dir)
profiles_yml: dict[str, Any] = load_profiles_yml(profiles_dir)
profiles_yml = run_coro_as_sync(
resolve_block_document_references(profiles_yml, replace_with_env_var_call)
resolve_block_document_references(
profiles_yml, value_transformer=replace_with_env_var_call
)
)
profiles_yml = run_coro_as_sync(resolve_variables(profiles_yml))

Expand Down
18 changes: 8 additions & 10 deletions src/prefect/utilities/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,11 @@ def apply_values(
raise ValueError(f"Unexpected template type {type(template).__name__!r}")


def replace_with_value(placeholder: str, value: Any) -> Any:
"""A block reference replacement function that returns the value unchanged."""
return value


@inject_client
async def resolve_block_document_references(
template: T,
replacement_function: Callable[[str, Any], Any] = replace_with_value,
client: Optional["PrefectClient"] = None,
value_transformer: Optional[Callable[[str, Any], Any]] = None,
) -> Union[T, dict[str, Any]]:
"""
Resolve block document references in a template by replacing each reference with
Expand Down Expand Up @@ -266,7 +261,7 @@ async def resolve_block_document_references(
Args:
template: The template to resolve block documents in
replacement_function: A function that takes the block placeholder and the block value and returns replacement text for the template
value_transformer: A function that takes the block placeholder and the block value and returns replacement text for the template
Returns:
The template with block documents resolved
Expand All @@ -284,14 +279,14 @@ async def resolve_block_document_references(
updated_template: dict[str, Any] = {}
for key, value in template.items():
updated_value = await resolve_block_document_references(
value, replacement_function=replacement_function, client=client
value, value_transformer=value_transformer, client=client
)
updated_template[key] = updated_value
return updated_template
elif isinstance(template, list):
return [
await resolve_block_document_references(
item, replacement_function=replacement_function, client=client
item, value_transformer=value_transformer, client=client
)
for item in template
]
Expand Down Expand Up @@ -337,7 +332,10 @@ async def resolve_block_document_references(
)
value = from_dict

return replacement_function(placeholder.full_match, value)
if value_transformer:
value = value_transformer(placeholder.full_match, value)

return value
else:
raise ValueError(
f"Invalid template: {template!r}. Only a single block placeholder is"
Expand Down

0 comments on commit b651af7

Please sign in to comment.