Skip to content

Commit

Permalink
Handle default factory in convert_dataclass (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey-berenda authored Jul 24, 2019
1 parent 24c6365 commit a61c488
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
10 changes: 5 additions & 5 deletions tests/converters/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class User:
id: Id
status: Status
birthday: datetime.date
emails: typing.List[str]
contact: Contact
emails: typing.List[str] = dataclasses.field(default_factory=list)
created_at: typing.Optional[datetime.datetime] = None
name: str = 'test name'

Expand Down Expand Up @@ -83,8 +83,8 @@ class Profile:
Id(1),
Status.SUPER,
datetime.date(year=2017, day=21, month=12),
['test@test.ru'],
Contact({123, 456}),
['test@test.ru'],
datetime.datetime(year=2001, month=2, day=3, hour=4, minute=5, second=6, tzinfo=tzutc()),
'name',
),
Expand All @@ -94,7 +94,6 @@ class Profile:
'id': 1,
'status': 'super',
'birthday': '2017-12-21',
'emails': ['test@test.ru'],
'contact': {
'phones': ['123', '456'],
},
Expand All @@ -103,8 +102,9 @@ class Profile:
Id(1),
Status.SUPER,
datetime.date(year=2017, day=21, month=12),
['test@test.ru'],
Contact({123, 456}),
[],
name='test name',
),
),
),
Expand Down Expand Up @@ -303,7 +303,7 @@ def test_convert_dataclass(data, type_, expected_instance):
},
User,
{
'non_field_error': 'Missing fields: "id", "status", "birthday", "emails"',
'non_field_error': 'Missing fields: "id", "status", "birthday"',
'contact': {
'phones': 'Cannot convert "123" to set',
},
Expand Down
2 changes: 1 addition & 1 deletion winter/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.14.0'
__version__ = '1.14.1'
2 changes: 2 additions & 0 deletions winter/converters/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ def _convert_dataclass_field(value, field: dataclasses.Field):
return convert(value, field.type)
if field.default is not dataclasses.MISSING:
return field.default
if field.default_factory is not dataclasses.MISSING:
return field.default_factory()
elif is_optional(field.type):
return None
else:
Expand Down

0 comments on commit a61c488

Please sign in to comment.