Skip to content

Stricter equality checks? #1271

Closed
Closed

Description

@averagehat

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 complaints

While 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).

Activity

  1. gvanrossum commented on Mar 4, 2016

    @gvanrossum
    Member
  2. JukkaL commented on Mar 4, 2016

    @JukkaL
    Collaborator

    The operators !=, is and is not are similar. Also, mypy doesn't complain about things like 1 in ['x'] since it doesn't raise a runtime exception.

  3. wcooley commented on Jun 2, 2016

    @wcooley

    Attempting to specify a type other than Any or object with __eq__ results in an error message like this:

    foo.py: note: In class "Foo":
    foo.py:4: error: Argument 1 of "__eq__" incompatible with supertype "object"
    

    (I'm adding this in case someone else is searching for this error.)

  4. dgoldstein0 commented on Dec 21, 2016

    @dgoldstein0

    +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.

  5. ilevkivskyi commented on Oct 24, 2018

    @ilevkivskyi
    Member

    It looks like this appears often, so I raise priority to high.

  6. nharkins commented on Oct 24, 2018

    @nharkins

    +1, would love this feature. just had a bug with a missing parens, i.e. if func != 0:

  7. jstasiak commented on Nov 26, 2018

    @jstasiak
    Contributor

    Kind of similar to the above: I did forget a few times that obj.thing is a method, not a property, wrote if obj.thing: and got an always-on block of code.

  8. efroemling commented on Feb 9, 2019

    @efroemling

    +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'

  9. ilevkivskyi commented on Feb 10, 2019

    @ilevkivskyi
    Member

    @jstasiak Your case is a bit different, it is like #2073

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions