eq_without_hash.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. """This is the remnant of the python3 checker.
  5. It was removed because the transition from python 2 to python3 is
  6. behind us, but some checks are still useful in python3 after all.
  7. See https://github.com/pylint-dev/pylint/issues/5025
  8. """
  9. from astroid import nodes
  10. from pylint import checkers, interfaces
  11. from pylint.checkers import utils
  12. from pylint.lint import PyLinter
  13. class EqWithoutHash(checkers.BaseChecker):
  14. name = "eq-without-hash"
  15. msgs = {
  16. "W1641": (
  17. "Implementing __eq__ without also implementing __hash__",
  18. "eq-without-hash",
  19. "Used when a class implements __eq__ but not __hash__. Objects get "
  20. "None as their default __hash__ implementation if they also implement __eq__.",
  21. ),
  22. }
  23. @utils.only_required_for_messages("eq-without-hash")
  24. def visit_classdef(self, node: nodes.ClassDef) -> None:
  25. locals_and_methods = set(node.locals).union(x.name for x in node.mymethods())
  26. if "__eq__" in locals_and_methods and "__hash__" not in locals_and_methods:
  27. self.add_message("eq-without-hash", node=node, confidence=interfaces.HIGH)
  28. def register(linter: PyLinter) -> None:
  29. linter.register_checker(EqWithoutHash(linter))