typing icon indicating copy to clipboard operation
typing copied to clipboard

Annotations for `__eq__`

Open JelleZijlstra opened this issue 7 months ago • 0 comments

It would be useful to have more guidance on how __eq__ methods should be annotated.

A few considerations:

  • Enable type checkers to warn on non-overlapping equality comparisons. The type annotations should ideally allow type checkers to look at a check like A() == B() and verify whether it can ever return True.
  • Some level of support for __eq__ returning a non-boolean (classic example: numpy arrays)
  • How to think about __eq__ methods that return NotImplemented on unrecognized types versus those that throw an error

See also https://discuss.python.org/t/make-type-hints-for-eq-of-primitives-less-strict/34240

Covering this in the typing spec is important because it affects how library authors should annotate __eq__ methods in their libraries if they want all type checkers to understand these methods.

JelleZijlstra avatar Dec 11 '23 05:12 JelleZijlstra