Skip to content

Commit

Permalink
fix format of nested dataclasses (#99)
Browse files Browse the repository at this point in the history
* fix format of nested dataclasses

* import sys

* unskip

Co-authored-by: Samuel Colvin <[email protected]>
  • Loading branch information
aliereno and samuelcolvin authored Jul 26, 2022
1 parent 1e3e942 commit 3838cc8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 1 addition & 3 deletions devtools/prettier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
23 changes: 23 additions & 0 deletions tests/test_prettier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))})
Expand Down

0 comments on commit 3838cc8

Please sign in to comment.