Skip to content

Rich comparison for attrs/dataclass classes? #3632

@hynek

Description

@hynek

Sooo, since I just ran into it myself: when I assert over classes, I obviously get an error similar to X(1) != X(2). Which once you have more than a handful of attributes becomes unreadable.

I usually use asdict temporarily and just do a assert attr.asdict(x) == attr.asdict(y) which will give me a nice diff. I know that other people also already did some hacking on this.

Now with attrs classes (and the upcoming dataclasses), it wouldn’t be too hard to give us nice, rich asserts: Once you detect inequality, you “just” have to check for (equal types obviously, and then) x.__attrs_attrs__, and if it exists pluck out attributes that are used for equality from there and find out which are actually not equal.

And ✨, you can tell the user exactly which attributes failed the equality test. Does that sound interesting for you at all? Now that 3.7 is out, I’m confident people will be interested in such a feature and IIRC, DCs and attrs are implemented similarly so both could/should be implemented at once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy issue that is friendly to new contributortype: enhancementnew feature or API change, should be merged into features branchtype: feature-branchnew feature or API change, should be merged into features branch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions