Skip to content

feat: optionally pass args to provider function #674

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iloveitaly
Copy link
Contributor

Description

Right now, a provider has no context about the class or field in question. Most of the time, that's fine.

In my scenario, I am generating TypeIDs which are prefixed with a specific string tied to a particular field name (more details.
This requires the context of the field and model to determine a good random ID.

This change passes in the model and field meta into the provider, if the provider function accepts the right arguments with the right names.

Closes

@iloveitaly iloveitaly requested a review from guacs as a code owner March 24, 2025 19:57
@adhtruong
Copy link
Collaborator

Can should_use_default_value be overriden at the factory level instead? If always use a default for this type then should handle this case https://github.com/litestar-org/polyfactory/blob/main/polyfactory/factories/base.py#L951-L963

@iloveitaly
Copy link
Contributor Author

@adhtruong if the default value isn't used, how is the field generated? I don't understand that part of the logic yet.

@adhtruong
Copy link
Collaborator

adhtruong commented Mar 25, 2025

Sorry, I believe I misunderstood this use case.

Guessing this is not expressible in python type system currently? This feels like a specific use case that should be pushed to custom base factory by overriding get_field_value itself if possible

@iloveitaly
Copy link
Contributor Author

Guessing this is not expressible in python type system currently? T

Nope, it's not, since the generated value is dependent on an instance of TypeID specific to that field. You need both the class + field reference in order to pull the right generator from the model config.

This feels like a specific use case that should be pushed to custom base factory by overriding get_field_value itself if possible

Yeah, could definitely see this as something specific to my app, although I'm curious if others have run into a similar problem where a type provider needs context of the class in question to generate a good result.

params = inspect.signature(method).parameters
return len(params) == 2 and "cls" in params and "field_meta" in params

if has_provider_args(provider):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think isinstance(..., Protocol) where `Protocol is a more robust way of doing this check. Then can pass in the field_meta only as method is already bound

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adhtruong could you give me an example / point me to more docs here? I haven't done this sort of thing with protocols before....

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