models.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """Models."""
  2. from astroid import Const
  3. from astroid.nodes import Assign, AssignName, ClassDef, FunctionDef
  4. from pylint.checkers import BaseChecker
  5. from pylint.checkers.utils import check_messages
  6. from pylint.interfaces import IAstroidChecker
  7. from pylint_django.__pkginfo__ import BASE_ID
  8. from pylint_django.utils import PY3, node_is_subclass
  9. MESSAGES = {
  10. f"E{BASE_ID}01": (
  11. "__unicode__ on a model must be callable (%s)",
  12. "model-unicode-not-callable",
  13. "Django models require a callable __unicode__ method",
  14. ),
  15. f"W{BASE_ID}01": (
  16. "No __unicode__ method on model (%s)",
  17. "model-missing-unicode",
  18. "Django models should implement a __unicode__ method for string representation",
  19. ),
  20. f"W{BASE_ID}02": (
  21. "Found __unicode__ method on model (%s). Python3 uses __str__.",
  22. "model-has-unicode",
  23. "Django models should not implement a __unicode__ method for string representation when using Python3",
  24. ),
  25. f"W{BASE_ID}03": (
  26. "Model does not explicitly define __unicode__ (%s)",
  27. "model-no-explicit-unicode",
  28. "Django models should implement a __unicode__ method for string representation. "
  29. "A parent class of this model does, but ideally all models should be explicit.",
  30. ),
  31. }
  32. def _is_meta_with_abstract(node):
  33. if isinstance(node, ClassDef) and node.name == "Meta":
  34. for meta_child in node.get_children():
  35. if not isinstance(meta_child, Assign):
  36. continue
  37. if not meta_child.targets[0].name == "abstract":
  38. continue
  39. if not isinstance(meta_child.value, Const):
  40. continue
  41. # TODO: handle tuple assignment?
  42. # eg:
  43. # abstract, something_else = True, 1
  44. if meta_child.value.value:
  45. # this class is abstract
  46. return True
  47. return False
  48. def _has_python_2_unicode_compatible_decorator(node):
  49. if node.decorators is None:
  50. return False
  51. for decorator in node.decorators.nodes:
  52. if getattr(decorator, "name", None) == "python_2_unicode_compatible":
  53. return True
  54. return False
  55. def _is_unicode_or_str_in_python_2_compatibility(method):
  56. if method.name == "__unicode__":
  57. return True
  58. if method.name == "__str__" and _has_python_2_unicode_compatible_decorator(method.parent):
  59. return True
  60. return False
  61. class ModelChecker(BaseChecker):
  62. """Django model checker."""
  63. __implements__ = IAstroidChecker
  64. name = "django-model-checker"
  65. msgs = MESSAGES
  66. @check_messages("model-missing-unicode")
  67. def visit_classdef(self, node):
  68. """Class visitor."""
  69. if not node_is_subclass(node, "django.db.models.base.Model", ".Model"):
  70. # we only care about models
  71. return
  72. for child in node.get_children():
  73. if _is_meta_with_abstract(child):
  74. return
  75. if isinstance(child, Assign):
  76. grandchildren = list(child.get_children())
  77. if not isinstance(grandchildren[0], AssignName):
  78. continue
  79. name = grandchildren[0].name
  80. if name != "__unicode__":
  81. continue
  82. grandchild = grandchildren[1]
  83. assigned = grandchild.inferred()[0]
  84. if assigned.callable():
  85. return
  86. self.add_message(f"E{BASE_ID}01", args=node.name, node=node)
  87. return
  88. if isinstance(child, FunctionDef) and child.name == "__unicode__":
  89. if PY3:
  90. self.add_message(f"W{BASE_ID}02", args=node.name, node=node)
  91. return
  92. # if we get here, then we have no __unicode__ method directly on the class itself
  93. # a different warning is emitted if a parent declares __unicode__
  94. for method in node.methods():
  95. if method.parent != node and _is_unicode_or_str_in_python_2_compatibility(method):
  96. # this happens if a parent declares the unicode method but
  97. # this node does not
  98. self.add_message(f"W{BASE_ID}03", args=node.name, node=node)
  99. return
  100. # if the Django compatibility decorator is used then we don't emit a warning
  101. # see https://github.com/PyCQA/pylint-django/issues/10
  102. if _has_python_2_unicode_compatible_decorator(node):
  103. return
  104. if PY3:
  105. return
  106. self.add_message(f"W{BASE_ID}01", args=node.name, node=node)