-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Refactor Client's Block CRUD methods into Mixin #16512
base: main
Are you sure you want to change the base?
Conversation
src/prefect/utilities/generics.py
Outdated
ListValidator = SchemaValidator( | ||
schema=core_schema.list_schema( | ||
items_schema=core_schema.dict_schema( | ||
keys_schema=core_schema.str_schema(), values_schema=core_schema.any_schema() | ||
) | ||
) | ||
) | ||
|
||
|
||
def validate_list(model: type[T], input: Any) -> list[T]: | ||
return [model.model_validate(item) for item in ListValidator.validate_python(input)] | ||
|
||
|
||
def ListAdapter(model: type[T]) -> Callable[[Any], list[T]]: | ||
return lambda input: validate_list(model, input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just have an adapter like TypeAdapter(list[T])
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had an early version of this that made a type adapter for every list, but the memory profile was bigger than I liked. tl;dr you basically have to pay the size of the ListSchemaValidator per object if you do it that way, which felt like a high tax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that makes sense. unrelated, can we use partial
instead of lambda
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep!
Related to #16472 this PR builds on (and follows the patterns of) #16496 to reduce the memory footprint of importing PrefectClient from 34MB to 29.9MB. This represents a 10% reduction in footprint from #16496 and 12% reduction from where we started.