python.lang.best-practice.missing-hash-with-eq.missing-hash-with-eq

profile photo of returntocorpreturntocorp
Author
6,393
Download Count*

Class $A has defined __eq__ which means it should also have defined __hash__;

Run Locally

Run in CI

Defintion

rules:
  - id: missing-hash-with-eq
    patterns:
      - pattern-not-inside: |
          class A(...):
              ...
              def __hash__(self):
                  ...
              ...
              def __eq__(self, $O):
                  ...
      - pattern: |
          class A(...):
            ...
            def __eq__(self, $O): ...
            ...
    message: "Class `$A` has defined `__eq__` which means it should also have
      defined `__hash__`; "
    languages:
      - python
    severity: WARNING
    metadata:
      category: best-practice
      technology:
        - python
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

missing-hash-with-eq.py


# ruleid:missing-hash-with-eq
class A:
    def __eq__(self, someother):
        pass


# ok:missing-hash-with-eq
class A2:
    def __eq__(self, someother):
        pass

    def __hash__(self):
        pass