Skip to content

feat: NeverNone to always generate an optional field, and AlwaysNone #677

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 4 commits into
base: main
Choose a base branch
from

Conversation

iloveitaly
Copy link
Contributor

@iloveitaly iloveitaly commented Mar 25, 2025

Description

Right now, there is not an ergonomic way to mark a single field as "required" in the sense
that polyfactory should always generate a value for it, even if it's marked as optional.

Curious what you think about the approach, I still need to:

  • write documentation
  • write tests

Closes

#655

@iloveitaly iloveitaly requested a review from guacs as a code owner March 25, 2025 13:26
@iloveitaly iloveitaly changed the title feat: NeverNone to always generate an optional field feat: NeverNone to always generate an optional field, and AlwaysNone Apr 15, 2025
@iloveitaly iloveitaly force-pushed the never-none branch 2 times, most recently from a86e363 to 4d86cc1 Compare April 17, 2025 14:12
@iloveitaly
Copy link
Contributor Author

@adhtruong fixed up tests for this! Take a look when you can.

Copy link

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

"""A factory field that marks as always generated, even if it's an optional."""


class AlwaysNone:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this required? Does

from dataclasses import dataclass

from polyfactory.factories.dataclass_factory import DataclassFactory


@dataclass
class A:
    a: int | None


class Factory(DataclassFactory[A]):
    a = None


print(Factory.batch(size=10))

work for this use case? Could this be documented

# within get_field_value.
excluded_field_value = has_field_value and isinstance(field_value, (NeverNone, AlwaysNone))

# TODO why do we need the BaseFactory check here, only dunder methods which are ignored would trigger this? # noqa: FIX002
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
# TODO why do we need the BaseFactory check here, only dunder methods which are ignored would trigger this? # noqa: FIX002
# Avoid considering fields on base field in case of clash

or if fine to remove check then can do that

@@ -334,6 +334,7 @@ def _handle_factory_field( # noqa: PLR0911
if isinstance(field_value, Fixture):
return field_value.to_value()

# if a raw lambda is passed, invoke it
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: this applies to all callables rather than just lambdas. Can this be updated?

@iloveitaly
Copy link
Contributor Author

Just a friendly reminder about this pull request! Please let me know if there's anything else needed here.

@WilliamDEdwards
Copy link

This would also be useful to me!

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.

3 participants