Skip to content

Conversation

@iloveitaly
Copy link
Contributor

Description

Two additional hooks:

  • post_create. Helpful for writing custom logic to run after a object is fully generated. There's not a great place to put this logic right now.
  • post_generate. There are field-level post_generate methods, but nothing that can use the fully-generated model kwargs and run custom logic

I can write some additional tests for this, but wanted to get some initial feedback first.

@iloveitaly iloveitaly requested a review from vkcku as a code owner March 20, 2025 21:34
@adhtruong
Copy link
Collaborator

Can the above functionality be achieved by overriding build and process_kwargs directly?

@iloveitaly
Copy link
Contributor Author

Can the above functionality be achieved by overriding build and process_kwargs directly?

It can, but:

  1. Overriding build requires you to call super() properly and type the params specific to the factory base class you are using
  2. Overriding either of these requires understanding the internals of polyfactory. Using either of the proposed hooks requires no knowledge of polyfactory or changes as polyfactory continues to change
  3. It's not self-documenting. Providing specific hooks makes it more clear exactly what you are trying to do

Copy link
Collaborator

@adhtruong adhtruong left a comment

Choose a reason for hiding this comment

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

The above feels like could be handled with docs if wanted to handle that case? Not opposed to adding though depending what other @litestar-org/maintainers think.

@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/polyfactory-docs-preview/667

@iloveitaly
Copy link
Contributor Author

@adhtruong addressed PR feedback!

@iloveitaly
Copy link
Contributor Author

@adhtruong anything else you need done here besides removing conflicts?

@adhtruong
Copy link
Collaborator

adhtruong commented Sep 29, 2025

@iloveitaly changes and tests look good. Happy to merge once conflicts are resolved and documentation is added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants