-
-
Notifications
You must be signed in to change notification settings - Fork 106
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
base: main
Are you sure you want to change the base?
Conversation
a86e363 to
4d86cc1
Compare
|
@adhtruong fixed up tests for this! Take a look when you can. |
|
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: |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
|
Just a friendly reminder about this pull request! Please let me know if there's anything else needed here. |
|
This would also be useful to me! |
Co-authored-by: Andrew Truong <[email protected]>
- 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.
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:
Closes
#655