Dirty-equals

Languages and libraries

testing library

SHOW HN2022

Dirty-equals

Doing dirty (but useful) things with equals

github.com

#python#equality#pytest#assertions

25points
6comments
#1,352of 2,570 in 2022
Top 53%of 2022 by points

How HN took it

QuietFewer than 5 comments about the project

1 positive, 0 neutral or asking, 0 mixed, 1 critical, out of 2 comments about the project.

Design: 0 for, 1 againstIdea: 1 for, 0 against

The thread, comment by comment

Each row is a top-level comment and its replies, in HN's order. Select a cell to open that comment on HN.

positive neutral or mixed critical about another commenter or something else the maker

What HN said

What people liked

  • Useful functionality for flexible comparisons

    From 2 comments
    • ``` assert user_data == { 'id': IsPositiveInt, 'avatar_file': IsStr(regex=r'/[a-z0-9\-] {10}/example\.png'), ``` > Without dirty-equals, you'd have to compare individual fields…
      quietbritishjim on HN
    • this is pretty neat!
      high_byte on HN

Objections

  • Overloading the equals operator is a gimmick and less clear

    From 1 comment
    • ``` assert user_data == { 'id': IsPositiveInt, 'avatar_file': IsStr(regex=r'/[a-z0-9\-] {10}/example\.png'), ``` > Without dirty-equals, you'd have to compare individual fields…
      quietbritishjim on HN

Questions

  • Why not use a separate dirty_check function instead of ==

    From 1 comment
    • ``` assert user_data == { 'id': IsPositiveInt, 'avatar_file': IsStr(regex=r'/[a-z0-9\-] {10}/example\.png'), ``` > Without dirty-equals, you'd have to compare individual fields…
      quietbritishjim on HN

Alternatives named

  • Raku smart-match operator

    From 2 comments
    • Raku has this feature by design. While there technically is a general equality operator `eqv`, you rarely see it used. Instead, you use the matching operator, `~~`, which can…
      pie_flavor on HN
    • The operator is called "smart-match". When you do: ``` $a ~~ $b ``` You are in fact doing: ``` $b.ACCEPTS($a) ``` So any object can determine how it will interpret any other…
      lizmat on HN

Category, keywords, summaries and labels on this page were made by AI (Thread Reader v2) and not checked by hand. Each point links to the comments it came from.

Add a badge to your READMEShown on HackerShow
[![Shown on HackerShow](https://hackershow.org/badge.svg)](https://hackershow.org/show/30471395)