Skip to content

Commit

Permalink
Don't include no-init fields in pretty printing
Browse files Browse the repository at this point in the history
  • Loading branch information
DRMacIver committed Oct 11, 2024
1 parent 714d5ac commit d4535da
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 6 additions & 2 deletions hypothesis-python/src/hypothesis/vendor/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,18 @@ def pretty(self, obj):
obj,
self,
cycle,
[at.name for at in cls.__attrs_attrs__],
[at.name for at in cls.__attrs_attrs__ if at.init],
)
if hasattr(cls, "__dataclass_fields__"):
return pprint_fields(
obj,
self,
cycle,
list(cls.__dataclass_fields__),
[
k
for k, v in cls.__dataclass_fields__.items()
if v.init
],
)
# Now check for object-specific printers which show how this
# object was constructed (a Hypothesis special feature).
Expand Down
28 changes: 27 additions & 1 deletion hypothesis-python/tests/cover/test_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import struct
import warnings
from collections import Counter, OrderedDict, defaultdict, deque
from dataclasses import dataclass
from dataclasses import dataclass, field
from enum import Enum, Flag
from functools import partial

Expand Down Expand Up @@ -846,3 +846,29 @@ def test_handles_cycles_in_dataclass():
x.x = x

assert pretty.pretty(x) == "SomeDataClass(x=SomeDataClass(...))"


@dataclass
class DataClassWithNoInitField:
x: int
y: int = field(init=False)


def test_does_not_include_no_init_fields_in_dataclass_printing():
record = DataClassWithNoInitField(x=1)
assert pretty.pretty(record) == "DataClassWithNoInitField(x=1)"
record.y = 1
assert pretty.pretty(record) == "DataClassWithNoInitField(x=1)"


@attrs.define
class AttrsClassWithNoInitField:
x: int
y: int = attrs.field(init=False)


def test_does_not_include_no_init_fields_in_attrs_printing():
record = AttrsClassWithNoInitField(x=1)
assert pretty.pretty(record) == "AttrsClassWithNoInitField(x=1)"
record.y = 1
assert pretty.pretty(record) == "AttrsClassWithNoInitField(x=1)"

0 comments on commit d4535da

Please sign in to comment.