dataclasses: compare=False Fields Still Show Up in repr() (tested)
field(compare=False) lets you exclude a field from the auto-generated __eq__ -- useful for things like a cache timestamp or a mutable log buffer that shouldn't affect identity. We measured whether excluding a field from equality also excludes it from the auto-generated repr(), since both are generated from the same field list.
1. By default, every field counts toward both equality and repr
from dataclasses import dataclass
@dataclass
class User:
id: int
name: str
a = User(1, "alice")
b = User(1, "bob")
print(a == b)
print(a)
False
User(id=1, name='alice')
Measured: without any field options, name is part of both __eq__ and __repr__ -- two instances with different names compare unequal, exactly as expected.
2. field(compare=False) removes it from == but not from repr()
from dataclasses import dataclass, field
@dataclass
class User:
id: int
name: str = field(compare=False)
a = User(1, "alice")
b = User(1, "bob")
print(a == b)
print(a)
print(b)
True
User(id=1, name='alice')
User(id=1, name='bob')
Measured: a == b is now True -- name no longer affects equality, so these two objects with different names are considered the same. But repr(a) and repr(b) still print their own distinct name values. compare and repr are independent field options; setting one has no effect on the other. Anything that dedupes by == or inserts into a set/dict keyed by these objects will silently keep whichever instance arrived first and drop the other as a "duplicate" -- while a log line printing repr() of the kept object shows one name, and the dropped object (if logged separately before being discarded) shows a different one, with nothing in the output explaining why they were treated as equal.
3. The fix: set repr=False too if the field shouldn't influence identity
from dataclasses import dataclass, field
@dataclass
class User:
id: int
name: str = field(compare=False, repr=False)
a = User(1, "alice")
b = User(1, "bob")
print(a == b)
print(a)
print(b)
True
User(id=1)
User(id=1)
Measured: adding repr=False alongside compare=False makes both instances print identically (User(id=1)), matching the fact that they already compare equal. If a field is excluded from equality because it genuinely shouldn't define the object's identity, exclude it from repr() too -- otherwise logs and debugger output will show "different" objects that your own code already treats as the same thing.
Frequently asked questions
Does field(compare=False) in a Python dataclass also remove the field from repr()?
No -- we measured a dataclass with compare=False on a field still printing that field's value via repr(), even though the field no longer affects == between two instances.
Can two unequal-looking dataclass instances compare as equal in Python?
Yes -- we measured two @dataclass instances with different repr() output (different field values) comparing as == True, because the differing field had compare=False set, excluding it from the generated __eq__.
How do I fully exclude a dataclass field from both equality and string representation?
Set both compare=False and repr=False on the same field() call -- we confirmed this makes two otherwise-equal instances print identically, matching their equality result.
Are compare and repr independent options on a dataclasses.field()?
Yes -- we measured that setting compare=False alone has no effect on whether the field appears in repr(); each field() option (compare, repr, hash, init) only controls the one generated method it names.
What's the risk of using field(compare=False) without also setting repr=False?
Deduplication logic using == or set/dict membership will silently treat instances with different values in that field as the same object, while logs or debugger output using repr() will still show the differing values -- we measured this exact mismatch, which can look like a bug in whatever discarded the 'duplicate'.
Written with AI assistance. Measured on 2026-10-04 with Python 3.13.5's built-in dataclasses module: defined a dataclass with one field marked compare=False, created two instances that differ only in that field, and checked both == and repr() on them, plus whether the field still appears in __eq__ by default without compare=False. PlainHeaders is our extension; Python's dataclasses module is built in.