Description
When a field's value is None in the input JSON, dataclasses-json raises a warning about non-optional types but doesn't call the field's decoder. This prevents proper handling of None values through decoders.
Current Workaround
Currently, we have to handle None values in post_init, which is not ideal as it:
- Splits the field's value handling logic
- Requires additional boilerplate code
- Makes the decoder configuration ineffective for None values
Suggested Solution
The library should call field decoders before handling None values, allowing the decoders to handle the None case appropriately.
Code snippet that reproduces the issue
@dataclass_json
@dataclass
class SimpleTest:
test_list: List[str] = field(
default_factory=list,
metadata=config(
decoder=lambda x: print(f"decoder called with {x}") or ([] if x is None else x)
)
)
test_data = {"test_list": None}
result = SimpleTest.from_dict(test_data)
Describe the results you expected
The library:
- Detects the None value
- Raises a warning: "'NoneType' object value of non-optional type test_list detected when decoding SimpleTest"
- Sets the field to None
- Never calls the decoder
Expected Behavior
The decoder should be called even for None values, allowing proper handling of such cases. This would allow users to define how None values should be handled at the field level through decoders.
Python version you are using
3.9.12
Environment description
dataclasses-json version: 0.6.7
Description
When a field's value is
Nonein the input JSON, dataclasses-json raises a warning about non-optional types but doesn't call the field's decoder. This prevents proper handling of None values through decoders.Current Workaround
Currently, we have to handle None values in post_init, which is not ideal as it:
Suggested Solution
The library should call field decoders before handling None values, allowing the decoders to handle the None case appropriately.
Code snippet that reproduces the issue
Describe the results you expected
The library:
Expected Behavior
The decoder should be called even for None values, allowing proper handling of such cases. This would allow users to define how None values should be handled at the field level through decoders.
Python version you are using
3.9.12
Environment description
dataclasses-json version: 0.6.7