Stricter equality checks? #1271
Copy link
Copy link
Closed
Description
It would be nice if there was a way to enforce a constraint on __eq__ -- maybe a warning? I almost never want to do the following
def test_operator(): # type: () -> None
1 == "1"
# produces no complaintsWhile that check may make sense in a world of vanilla python, I'd argue it doesn't make much sense within mypy's world (especially if the arguments are explicitly typed as different types; i.e., one is not a subtype of the other).
Reactions are currently unavailable
Activity
gvanrossum commentedon Mar 4, 2016
JukkaL commentedon Mar 4, 2016
The operators
!=,isandis notare similar. Also, mypy doesn't complain about things like1 in ['x']since it doesn't raise a runtime exception.wcooley commentedon Jun 2, 2016
Attempting to specify a type other than
Anyorobjectwith__eq__results in an error message like this:(I'm adding this in case someone else is searching for this error.)
dgoldstein0 commentedon Dec 21, 2016
+1 would love to see this - had to troubleshoot a bug today that turned out to a stray
,making an equality check fail because one side had accidentally become a tuple.ilevkivskyi commentedon Oct 24, 2018
It looks like this appears often, so I raise priority to high.
nharkins commentedon Oct 24, 2018
+1, would love this feature. just had a bug with a missing parens, i.e.
if func != 0:jstasiak commentedon Nov 26, 2018
Kind of similar to the above: I did forget a few times that
obj.thingis a method, not a property, wroteif obj.thing:and got an always-on block of code.efroemling commentedon Feb 9, 2019
+1 would love to see this too. I'm replacing a bunch of old string values with more type-safe enum objects and would love to get errors in places where my code needs updating, such as: if obj.enum_value == 'my_old_hard_coded_string_value'
ilevkivskyi commentedon Feb 10, 2019
@jstasiak Your case is a bit different, it is like #2073
Add flag to prohibit equality checks between non-overlapping checks (#…