From 5928334c0b371f41b2a0c8718bc5061e29460329 Mon Sep 17 00:00:00 2001 From: Alexander Tikhonov Date: Tue, 22 Dec 2020 12:34:56 +0300 Subject: [PATCH] Fix CustomPath deserialization --- mashumaro/serializer/base/metaprogramming.py | 4 +++- tests/entities.py | 2 +- tests/test_data_types.py | 12 +----------- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/mashumaro/serializer/base/metaprogramming.py b/mashumaro/serializer/base/metaprogramming.py index fc4f017d..33906987 100644 --- a/mashumaro/serializer/base/metaprogramming.py +++ b/mashumaro/serializer/base/metaprogramming.py @@ -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})' diff --git a/tests/entities.py b/tests/entities.py index fa12df38..885cb6b5 100644 --- a/tests/entities.py +++ b/tests/entities.py @@ -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 diff --git a/tests/test_data_types.py b/tests/test_data_types.py index f774aa75..b33a1f62 100644 --- a/tests/test_data_types.py +++ b/tests/test_data_types.py @@ -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': @@ -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