dataclass-csv
dataclass-csv copied to clipboard
remapping of csv field -> dataclass field has issues when csv field has name match to dataclass field
I came across this problem when trying to work around #17.
Given a data file with a column 'date', which contains an integer (a unix timestamp), and
a dataclass with a field of date
which is of type datetime.datetime. Since there isn't a way to convert that in dataclass_csv today, I munged the dataclass a bit, made date
an init=False
field, introduced an InitVar of unix_ts
, and used post_init to convert the unix timestamp to a datetime field like so:
@dataclass
class SomeData:
date: datetime.datetime = field(init=False)
unix_ts: InitVar[int]
def __post_init__(self, unix_ts: int):
self.date = datetime.datetime.utcfromtimestamp(unix_ts)
This works exactly as expected: the init signature is looking for unix_ts instead of date, date gets set with the value of unix_ts, all my consumers of the dataclass object don't have to change.
... until I try to load the data from dataclass_csv, at any rate.
reader = DataclassReader(inp, SomeData)
reader.map('date').to('unix_ts')
for record in reader:
...
This generates the standard 'hey, you're telling me to map onto a datetime field, and didn't tell me how to convert:
AttributeError: Unable to parse the datetime string value. Date format not specified. To specify a date format for all datetime fields in the class, use the @dateformat decorator. To define a date format specifically for this field, change its definition to: `date: datetime = field(metadata={'dateformat': <date_format>})`
Okay, fine, I figure I'll open an issue (here it is!), and I'll rename the date
field on my dataclass to something else. Then I run into #9 because despite having init=False, it's still trying to build it into the construction of my class.
So, between #9 and #17, I'm actually stuck at the moment, but while working both of those out, I encountered this issue and thought I'd raise it.