Repository: litestar-org/polyfactory (python)

Issue:
Bug: Can't consistently override optional fields with nested models
### Description

When trying to build a nested set of models, if one of them is optional / can be `None`, then there's a chance the factory will assign `None` to the corresponding field even if an explicit value is being passed when calling `build` (see example).

### URL to code causing the issue

_No response_

### MCVE

```python
from dataclasses import dataclass
from polyfactory.factories import DataclassFactory

@dataclass
class Bar:
    baz: str

@dataclass
class Foo:
    bar: Bar | None

class FooFactory(DataclassFactory):
    __model__ = Foo

for _ in range(1000):
    foo = FooFactory.build(bar={'baz': 'TEST'})
    # foo.bar should consistently be present
    assert foo.bar is not None
```


### Steps to reproduce

_No response_

### Screenshots

_No response_

### Logs

_No response_

### Release Version

2.10.0

### Platform

- [ ] Linux
- [X] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)

<!-- POLAR PLEDGE BADGE START -->
---
> [!NOTE]  
> While we are open for sponsoring on [GitHub Sponsors](https://github.com/sponsors/litestar-org/) and 
> [OpenCollective](https://opencollective.com/litestar), we also utilize [Polar.sh](https://polar.sh/) to engage in pledge-based sponsorship.
>
> Check out all issues funded or available for funding [on our Polar.sh Litestar dashboard](https://polar.sh/litestar-org)
> * If you would like to see an issue prioritized, make a pledge towards it!
> * We receive the pledge once the issue is completed & verified
> * This, along with engagement in the community, helps us know which features are a priority to our users.

<a href="https://polar.sh/litestar-org/polyfactory/issues/419">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/litestar-org/polyfactory/issues/419/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/litestar-org/polyfactory/issues/419/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->


Pull request description:
fix: respect override of optional nested model fields
<!--
By submitting this pull request, you agree to:
- follow [Litestar's Code of Conduct](https://github.com/litestar-org/.github/blob/main/CODE_OF_CONDUCT.md)
- follow [Litestar's contribution guidelines](https://github.com/litestar-org/.github/blob/main/CONTRIBUTING.md)
- follow the [PSFs's Code of Conduct](https://www.python.org/psf/conduct/)
-->

### Pull Request Checklist

- [x] New code has 100% test coverage
- [ ] (If applicable) The prose documentation has been updated to reflect the changes introduced by this PR
- [ ] (If applicable) The reference documentation has been updated to reflect the changes introduced by this PR
- [x] Pre-Commit Checks were ran and passed
- [x] Tests were ran and passed

### Description
<!--
Please describe your pull request for new release changelog purposes
-->

- Fixes `None` being randomly assigned to optional fields whose type is another model even if attributes for that model are passed to `build` as a `dict`

### Close Issue(s)
<!--
Please add in issue numbers this pull request will close, if applicable
Examples: Fixes #4321 or Closes #1234
-->

- Closes #419

---

I tried to mimic the structure of other tests on the file but open to feedback. The code change makes sense to me (if there are build parameters defined for a given field, don't consider setting it to `None`) but there might be other code paths I'm not considering.

Patch:
diff --git a/polyfactory/factories/base.py b/polyfactory/factories/base.py
index aecb9cf..67ff305 100644
--- a/polyfactory/factories/base.py
+++ b/polyfactory/factories/base.py
@@ -559,7 +559,7 @@ class BaseFactory(ABC, Generic[T]):
         if cls.is_ignored_type(field_meta.annotation):
             return None
 
-        if cls.should_set_none_value(field_meta=field_meta):
+        if field_build_parameters is None and cls.should_set_none_value(field_meta=field_meta):
             return None
 
         unwrapped_annotation = unwrap_annotation(field_meta.annotation, random=cls.__random__)


Test patch:
diff --git a/tests/test_passing_build_args_to_child_factories.py b/tests/test_passing_build_args_to_child_factories.py
index 50cfeb2..1ba4ff2 100644
--- a/tests/test_passing_build_args_to_child_factories.py
+++ b/tests/test_passing_build_args_to_child_factories.py
@@ -3,6 +3,7 @@ from typing import List, Mapping, Optional
 from pydantic import BaseModel
 
 from polyfactory.factories.pydantic_factory import ModelFactory
+from polyfactory.field_meta import FieldMeta
 
 
 class Address(BaseModel):
@@ -176,3 +177,22 @@ def test_factory_with_partial_kwargs_deep_in_tree() -> None:
     build_result = DFactory.build(factory_use_construct=False, **{"c": {"b": {"a": {"name": "test"}}}})
     assert build_result
     assert build_result.c.b.a.name == "test"
+
+
+def test_factory_with_nested_optional_field_overrides_in_dict() -> None:
+    class MyChildModel(BaseModel):
+        name: str
+
+    class MyParentModel(BaseModel):
+        child: Optional[MyChildModel]
+
+    class MyParentModelFactory(ModelFactory):
+        __model__ = MyParentModel
+
+        @classmethod
+        def should_set_none_value(cls, field_meta: FieldMeta) -> bool:
+            return True
+
+    result = MyParentModelFactory.build(child={"name": "test"})
+    assert result.child is not None
+    assert result.child.name == "test"