Skip to content
New issue

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

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

Already on GitHub? # to your account

add freeze annotation #17374

Merged
merged 7 commits into from
Mar 5, 2025
Merged

add freeze annotation #17374

merged 7 commits into from
Mar 5, 2025

Conversation

zzstoatzz
Copy link
Collaborator

@zzstoatzz zzstoatzz commented Mar 5, 2025

closes #11073

this PR adds a freeze annotation type as described in the linked PR. adding a parameterized type (e.g. freeze[str]) is not necessary

the field's value becomes a read-only Enum with only this member value (which be refined to more specifically patch the parameter schema)

from prefect import flow
from prefect.utilities.annotations import freeze


@flow(log_prints=True)
def test_freeze(value: str) -> str:
    print(f"value: {value}, type: {type(value)}")
    assert isinstance(value, str)
    return value


if __name__ == "__main__":
    test_freeze.serve(parameters={"value": freeze[str]("test")})

image

image

Copy link

codspeed-hq bot commented Mar 5, 2025

CodSpeed Performance Report

Merging #17374 will not alter performance

Comparing freeze (c0c7f92) with main (459cb47)

Summary

✅ 2 untouched benchmarks

@github-actions github-actions bot added the enhancement An improvement of an existing feature label Mar 5, 2025
@zzstoatzz zzstoatzz changed the title [wip] add freeze annotation add freeze annotation Mar 5, 2025
@zzstoatzz zzstoatzz self-assigned this Mar 5, 2025
@zzstoatzz zzstoatzz marked this pull request as ready for review March 5, 2025 17:45
Copy link
Member

@cicdw cicdw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is way better than how I was going about it, nice!

Left a few questions to better understand before approving

self.parameter_openapi_schema is not None
and key in self.parameter_openapi_schema.get("properties", {})
):
self.parameter_openapi_schema["properties"][key]["readOnly"] = True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the UI do anything with this property?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes! you can't edit the value in the UI

and key in self.parameter_openapi_schema.get("properties", {})
):
self.parameter_openapi_schema["properties"][key]["readOnly"] = True
self.parameter_openapi_schema["properties"][key]["enum"] = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this require the raw value to be JSON serializable or otherwise impose any restrictions on the types you can freeze?

Copy link
Collaborator Author

@zzstoatzz zzstoatzz Mar 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm. yes I think values would need to be JSON serializable.. open to suggestions on how to handle that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea that's probably fine for now (maybe we should add a ValueError validation to freeze(...)?) but I can see a world where more complex types are requested

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added in 05f4cab

Copy link
Member

@desertaxle desertaxle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edit: nvm I misunderstood the use case

Comment on lines +143 to +153
@classmethod
def __get_pydantic_core_schema__(
cls, source: type[Any], handler: GetCoreSchemaHandler
) -> core_schema.CoreSchema:
return core_schema.no_info_after_validator_function(
cls, # Use the class itself as the validator
core_schema.any_schema(),
serialization=core_schema.plain_serializer_function_ser_schema(
lambda x: x.unfreeze() # Serialize by unwrapping the value
),
)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't matter as far as its use in RunnerDeployment, but will make it behave more predictably in general

Copy link
Member

@desertaxle desertaxle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@zzstoatzz zzstoatzz merged commit 3fdde94 into main Mar 5, 2025
46 checks passed
@zzstoatzz zzstoatzz deleted the freeze branch March 5, 2025 22:16
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement An improvement of an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Parameter Freezing or Hiding for Deployment Flow Configuration
3 participants