dict_init_mutate.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt
  4. """Check for use of dictionary mutation after initialization."""
  5. from __future__ import annotations
  6. from typing import TYPE_CHECKING
  7. from astroid import nodes
  8. from pylint.checkers import BaseChecker
  9. from pylint.checkers.utils import only_required_for_messages
  10. from pylint.interfaces import HIGH
  11. if TYPE_CHECKING:
  12. from pylint.lint.pylinter import PyLinter
  13. class DictInitMutateChecker(BaseChecker):
  14. name = "dict-init-mutate"
  15. msgs = {
  16. "C3401": (
  17. "Declare all known key/values when initializing the dictionary.",
  18. "dict-init-mutate",
  19. "Dictionaries can be initialized with a single statement "
  20. "using dictionary literal syntax.",
  21. )
  22. }
  23. @only_required_for_messages("dict-init-mutate")
  24. def visit_assign(self, node: nodes.Assign) -> None:
  25. """
  26. Detect dictionary mutation immediately after initialization.
  27. At this time, detecting nested mutation is not supported.
  28. """
  29. if not isinstance(node.value, nodes.Dict):
  30. return
  31. dict_name = node.targets[0]
  32. if len(node.targets) != 1 or not isinstance(dict_name, nodes.AssignName):
  33. return
  34. first_sibling = node.next_sibling()
  35. if (
  36. not first_sibling
  37. or not isinstance(first_sibling, nodes.Assign)
  38. or len(first_sibling.targets) != 1
  39. ):
  40. return
  41. sibling_target = first_sibling.targets[0]
  42. if not isinstance(sibling_target, nodes.Subscript):
  43. return
  44. sibling_name = sibling_target.value
  45. if not isinstance(sibling_name, nodes.Name):
  46. return
  47. if sibling_name.name == dict_name.name:
  48. self.add_message("dict-init-mutate", node=node, confidence=HIGH)
  49. def register(linter: PyLinter) -> None:
  50. linter.register_checker(DictInitMutateChecker(linter))