Skip to content

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 vkcku 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.

@github-actions
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

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!

Co-authored-by: Andrew Truong <[email protected]>
iloveitaly pushed a commit to iloveitaly/polyfactory that referenced this pull request Nov 14, 2025
- Update comment in base.py to say "callable" instead of "raw lambda"
  to accurately reflect that the code handles all callables, not just lambdas
- Complete the incomplete comment in test_nones.py
- Add comprehensive tests comparing AlwaysNone with plain None assignment
- Add test demonstrating NeverNone behavior with __allow_none_optionals__

These changes address @adhtruong's review feedback while documenting
that plain `name = None` works similarly to `AlwaysNone()`, though
AlwaysNone provides more explicit intent and is processed differently
internally. NeverNone provides unique value for per-field control of
optional field generation.
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