Skip to content

Commit 3838cc8

Browse files
fix format of nested dataclasses (#99)
* fix format of nested dataclasses * import sys * unskip Co-authored-by: Samuel Colvin <[email protected]>
1 parent 1e3e942 commit 3838cc8

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

devtools/prettier.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,7 @@ def _format_bytearray(self, value: 'Any', _: str, indent_current: int, indent_ne
230230
self._str_lines(lines, indent_current, indent_new)
231231

232232
def _format_dataclass(self, value: 'Any', _: str, indent_current: int, indent_new: int):
233-
from dataclasses import asdict
234-
235-
self._format_fields(value, asdict(value).items(), indent_current, indent_new)
233+
self._format_fields(value, value.__dict__.items(), indent_current, indent_new)
236234

237235
def _format_sqlalchemy_class(self, value: 'Any', _: str, indent_current: int, indent_new: int):
238236
fields = [

tests/test_prettier.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,29 @@ class FooDataclass:
240240
)"""
241241

242242

243+
def test_nested_dataclasses():
244+
@dataclass
245+
class FooDataclass:
246+
x: int
247+
248+
@dataclass
249+
class BarDataclass:
250+
a: float
251+
b: FooDataclass
252+
253+
f = FooDataclass(123)
254+
b = BarDataclass(10.0, f)
255+
v = pformat(b)
256+
print(v)
257+
assert v == """\
258+
BarDataclass(
259+
a=10.0,
260+
b=FooDataclass(
261+
x=123,
262+
),
263+
)"""
264+
265+
243266
@pytest.mark.skipif(numpy is None, reason='numpy not installed')
244267
def test_indent_numpy():
245268
v = pformat({'numpy test': numpy.array(range(20))})

0 commit comments

Comments
 (0)