dfurtado / dataclass-csv

Map CSV to Data Classes
Other
195 stars 21 forks source link

datetime fields with default values trigger an error #20

Closed zivanfi closed 4 years ago

zivanfi commented 4 years ago

I ran into a problem with dataclasses with fields that have default values. I managed to reproduce the issue with a small change in one of the tests: Modify UserWithDateFormatDecorator in tests/mocks.py to contain a datetime with a default value:

@dateformat('%Y-%m-%d')
@dataclasses.dataclass
class UserWithDateFormatDecorator:
    name: str
    create_date: datetime
    date_with_default: datetime = datetime.now()

Executing the test shows the error:

TypeError: strptime() argument 1 must be str, not datetime.datetime

Apparently, dataclass-csv tries to parse the default value as if it were an input string from the CSV, although in reality it is already a datetime (and can't be changed to a str either).

dfurtado commented 4 years ago

@zivanfi very good catch!!! Need to be fixed for sure! I can look into it since I'm working with a custom date parser decorator. BTW I released the new version with your PR today!

zivanfi commented 4 years ago

That's good news, thanks for the release!