Skip to content
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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

aaazzam
Copy link
Collaborator

@aaazzam aaazzam commented Dec 26, 2024

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.

Comment on lines 8 to 22
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)
Copy link
Collaborator

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])?

Copy link
Collaborator Author

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.

Copy link
Collaborator

@zzstoatzz zzstoatzz Dec 26, 2024

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yep!

@github-actions github-actions bot added the bug Something isn't working label Dec 26, 2024
@aaazzam aaazzam marked this pull request as ready for review December 26, 2024 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants