54
54
)
55
55
from polyfactory .exceptions import ConfigurationException , MissingBuildKwargException , ParameterException
56
56
from polyfactory .field_meta import Null
57
- from polyfactory .fields import Fixture , Ignore , NeverNone , PostGenerated , Require , Use
57
+ from polyfactory .fields import AlwaysNone , Fixture , Ignore , NeverNone , PostGenerated , Require , Use
58
58
from polyfactory .utils .helpers import (
59
59
flatten_annotation ,
60
60
get_collection_type ,
@@ -949,6 +949,10 @@ def should_set_none_value(cls, field_meta: FieldMeta) -> bool:
949
949
"""
950
950
field_value = hasattr (cls , field_meta .name ) and getattr (cls , field_meta .name )
951
951
never_none = field_value and isinstance (field_value , NeverNone )
952
+ always_none = field_value and isinstance (field_value , AlwaysNone )
953
+
954
+ if always_none :
955
+ return True
952
956
953
957
return (
954
958
cls .__allow_none_optionals__
@@ -1026,7 +1030,7 @@ def _check_declared_fields_exist_in_model(cls) -> None:
1026
1030
f"{ field_name } is declared on the factory { cls .__name__ } "
1027
1031
f" but it is not part of the model { cls .__model__ .__name__ } "
1028
1032
)
1029
- if isinstance (field_value , (Use , PostGenerated , Ignore , Require , NeverNone )):
1033
+ if isinstance (field_value , (Use , PostGenerated , Ignore , Require , NeverNone , AlwaysNone )):
1030
1034
raise ConfigurationException (error_message )
1031
1035
1032
1036
@classmethod
@@ -1047,9 +1051,12 @@ def process_kwargs(cls, **kwargs: Any) -> dict[str, Any]:
1047
1051
if cls .should_set_field_value (field_meta , ** kwargs ) and not cls .should_use_default_value (field_meta ):
1048
1052
field_value = getattr (cls , field_meta .name , None )
1049
1053
1050
- # TODO why do we need the BaseFactory check here, only dunder methods which are ignored would trigger this?
1051
- # NeverNone should be treated as a normally-generated field
1052
- if field_value and not hasattr (BaseFactory , field_meta .name ) and not isinstance (field_value , NeverNone ):
1054
+ # NeverNone & AlwaysNone should be treated as a normally-generated field, since this changes logic
1055
+ # within get_field_value.
1056
+ excluded_field_value = field_value and isinstance (field_value , (NeverNone , AlwaysNone ))
1057
+
1058
+ # TODO why do we need the BaseFactory check here, only dunder methods which are ignored would trigger this? # noqa: FIX002
1059
+ if field_value and not hasattr (BaseFactory , field_meta .name ) and not excluded_field_value :
1053
1060
if isinstance (field_value , Ignore ):
1054
1061
continue
1055
1062
0 commit comments