Skip to content

Commit

Permalink
Fix CustomPath deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Fatal1ty committed Dec 22, 2020
1 parent 6d548b0 commit 5928334
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
4 changes: 3 additions & 1 deletion mashumaro/serializer/base/metaprogramming.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,10 @@ def inner_expr(arg_num=0, v_name='value'):
return f'pathlib.PureWindowsPath({value_name})'
elif issubclass(origin_type, pathlib.PurePath):
return f'pathlib.PurePath({value_name})'
else:
elif origin_type is os.PathLike:
return f'pathlib.PurePath({value_name})'
else:
return f'{type_name(origin_type)}({value_name})'
elif issubclass(origin_type, enum.Enum):
return f'{value_name} if use_enum ' \
f'else {type_name(origin_type)}({value_name})'
Expand Down
2 changes: 1 addition & 1 deletion tests/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ def __str__(self):
return self._path

def __eq__(self, other):
return str(self).replace('\\', '/') == str(other).replace('\\', '/')
return isinstance(other, CustomPath) and self._path == other._path
12 changes: 1 addition & 11 deletions tests/test_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Fixture:
(decimal.Decimal, Fixture.DECIMAL, Fixture.DECIMAL_STR),
(fractions.Fraction, Fixture.FRACTION, Fixture.FRACTION_STR),
(MutableString, Fixture.MUTABLE_STRING, Fixture.MUTABLE_STRING_STR),
(CustomPath, Fixture.CUSTOM_PATH, Fixture.CUSTOM_PATH_STR),
]

if os.name == 'posix':
Expand Down Expand Up @@ -806,14 +807,3 @@ class DataClass(DataClassDictMixin):

with pytest.raises(InvalidFieldValue):
DataClass.from_dict({'x': 'bad_value'})


def test_custom_pathlike_type():

@dataclass
class DataClass(DataClassDictMixin):
x: CustomPath

instance = DataClass(x=Fixture.CUSTOM_PATH)
assert instance.to_dict() == {'x': Fixture.CUSTOM_PATH_STR}
assert DataClass.from_dict({'x': Fixture.CUSTOM_PATH_STR}) == instance

0 comments on commit 5928334

Please sign in to comment.