msto / dataclass_io

Read and write dataclasses.
MIT License
1 stars 0 forks source link

Support the simple union type for optionals #14

Open clintval opened 6 months ago

clintval commented 6 months ago

It would be great to support the optional syntax using a union (e.g. T | None). Higher kinds of unions would be tricky and sometimes ambiguous to represent, so this feature request is just for the syntactic sugar of Optional[T].

msto commented 6 months ago

Thinking about this a bit:

When reading, fields could be Optional in two ways - the field may have missing values in some rows, or the field may not exist as a column in the input file. Was there a particular behavior you had in mind?

I can add a missing_data_representation parameter (or similar) to both the reader and writer classes to accommodate empty strings, R's NA, etc. Is there a default value you'd prefer to represent a missing value/ None?

clintval commented 6 months ago

If the column is not found at all, I think that should raise an exception by default.

The missing value could default to the empty string, although I suppose you can parameterize this to support other values like ., NA, N/A. There will be some lossy conversions like str | None where the empty string is a valid string-type and symbol for None. In those deserialization cases I think defaulting to the None case is better.