From d4535dab4f4b72fe82e90f3ddc78e49e8ed3c0a2 Mon Sep 17 00:00:00 2001 From: "David R. MacIver" Date: Fri, 11 Oct 2024 11:25:35 +0100 Subject: [PATCH] Don't include no-init fields in pretty printing --- .../src/hypothesis/vendor/pretty.py | 8 ++++-- hypothesis-python/tests/cover/test_pretty.py | 28 ++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/hypothesis-python/src/hypothesis/vendor/pretty.py b/hypothesis-python/src/hypothesis/vendor/pretty.py index 2f85bf6c4d..8501d5d827 100644 --- a/hypothesis-python/src/hypothesis/vendor/pretty.py +++ b/hypothesis-python/src/hypothesis/vendor/pretty.py @@ -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). diff --git a/hypothesis-python/tests/cover/test_pretty.py b/hypothesis-python/tests/cover/test_pretty.py index 64c412034e..9178c98f87 100644 --- a/hypothesis-python/tests/cover/test_pretty.py +++ b/hypothesis-python/tests/cover/test_pretty.py @@ -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 @@ -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)"