diff --git a/devtools/prettier.py b/devtools/prettier.py index d4e2b1f..1154668 100644 --- a/devtools/prettier.py +++ b/devtools/prettier.py @@ -230,9 +230,7 @@ def _format_bytearray(self, value: 'Any', _: str, indent_current: int, indent_ne self._str_lines(lines, indent_current, indent_new) def _format_dataclass(self, value: 'Any', _: str, indent_current: int, indent_new: int): - from dataclasses import asdict - - self._format_fields(value, asdict(value).items(), indent_current, indent_new) + self._format_fields(value, value.__dict__.items(), indent_current, indent_new) def _format_sqlalchemy_class(self, value: 'Any', _: str, indent_current: int, indent_new: int): fields = [ diff --git a/tests/test_prettier.py b/tests/test_prettier.py index 7ec2b66..6dea882 100644 --- a/tests/test_prettier.py +++ b/tests/test_prettier.py @@ -240,6 +240,29 @@ class FooDataclass: )""" +def test_nested_dataclasses(): + @dataclass + class FooDataclass: + x: int + + @dataclass + class BarDataclass: + a: float + b: FooDataclass + + f = FooDataclass(123) + b = BarDataclass(10.0, f) + v = pformat(b) + print(v) + assert v == """\ +BarDataclass( + a=10.0, + b=FooDataclass( + x=123, + ), +)""" + + @pytest.mark.skipif(numpy is None, reason='numpy not installed') def test_indent_numpy(): v = pformat({'numpy test': numpy.array(range(20))})