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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions polyfactory/factories/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,15 @@ def get_field_value( # noqa: C901, PLR0911, PLR0912
)

if provider := cls.get_provider_map().get(unwrapped_annotation):
provider_signature = inspect.signature(provider)

def has_provider_args(method: Callable):
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....

return provider(cls, field_meta)

return provider()

if isinstance(unwrapped_annotation, TypeVar):
Expand Down
Loading