match_statements_checker.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. """Match statement checker for Python code."""
  5. from __future__ import annotations
  6. from typing import TYPE_CHECKING
  7. import astroid.exceptions
  8. from astroid import nodes
  9. from pylint.checkers import BaseChecker
  10. from pylint.checkers.utils import only_required_for_messages, safe_infer
  11. from pylint.interfaces import HIGH, INFERENCE
  12. if TYPE_CHECKING:
  13. from pylint.lint import PyLinter
  14. # List of builtin classes which match self
  15. # https://docs.python.org/3/reference/compound_stmts.html#class-patterns
  16. MATCH_CLASS_SELF_NAMES = {
  17. "builtins.bool",
  18. "builtins.bytearray",
  19. "builtins.bytes",
  20. "builtins.dict",
  21. "builtins.float",
  22. "builtins.frozenset",
  23. "builtins.int",
  24. "builtins.list",
  25. "builtins.set",
  26. "builtins.str",
  27. "builtins.tuple",
  28. }
  29. class MatchStatementChecker(BaseChecker):
  30. name = "match_statements"
  31. msgs = {
  32. "E1901": (
  33. "The name capture `case %s` makes the remaining patterns unreachable. "
  34. "Use a dotted name (for example an enum) to fix this.",
  35. "bare-name-capture-pattern",
  36. "Emitted when a name capture pattern is used in a match statement "
  37. "and there are case statements below it.",
  38. ),
  39. "E1902": (
  40. "`__match_args__` must be a tuple of strings.",
  41. "invalid-match-args-definition",
  42. "Emitted if `__match_args__` isn't a tuple of strings required for match.",
  43. ),
  44. "E1903": (
  45. "%s expects %d positional sub-patterns (given %d)",
  46. "too-many-positional-sub-patterns",
  47. "Emitted when the number of allowed positional sub-patterns exceeds the "
  48. "number of allowed sub-patterns specified in `__match_args__`.",
  49. ),
  50. "E1904": (
  51. "Multiple sub-patterns for attribute %s",
  52. "multiple-class-sub-patterns",
  53. "Emitted when there is more than one sub-pattern for a specific "
  54. "attribute in a class pattern.",
  55. ),
  56. "R1905": (
  57. "Use '%s() as %s' instead",
  58. "match-class-bind-self",
  59. "Match class patterns are faster if the name binding happens "
  60. "for the whole pattern and any lookup for `__match_args__` "
  61. "can be avoided.",
  62. ),
  63. "R1906": (
  64. "Use keyword attributes instead of positional ones (%s)",
  65. "match-class-positional-attributes",
  66. "Keyword attributes are more explicit and slightly faster "
  67. "since CPython can skip the `__match_args__` lookup.",
  68. ),
  69. }
  70. @only_required_for_messages("invalid-match-args-definition")
  71. def visit_assignname(self, node: nodes.AssignName) -> None:
  72. if (
  73. node.name == "__match_args__"
  74. and isinstance(node.frame(), nodes.ClassDef)
  75. and isinstance(node.parent, nodes.Assign)
  76. and not (
  77. isinstance(node.parent.value, nodes.Tuple)
  78. and all(
  79. isinstance(el, nodes.Const) and isinstance(el.value, str)
  80. for el in node.parent.value.elts
  81. )
  82. )
  83. ):
  84. self.add_message(
  85. "invalid-match-args-definition",
  86. node=node.parent.value,
  87. args=(),
  88. confidence=HIGH,
  89. )
  90. @only_required_for_messages("bare-name-capture-pattern")
  91. def visit_match(self, node: nodes.Match) -> None:
  92. """Check if a name capture pattern prevents the other cases from being
  93. reached.
  94. """
  95. for idx, case in enumerate(node.cases):
  96. match case:
  97. case nodes.MatchCase(
  98. pattern=nodes.MatchAs(
  99. pattern=None, name=nodes.AssignName(name=name)
  100. ),
  101. guard=None,
  102. ) if (
  103. idx < len(node.cases) - 1
  104. ):
  105. self.add_message(
  106. "bare-name-capture-pattern",
  107. node=case.pattern,
  108. args=(name,),
  109. confidence=HIGH,
  110. )
  111. @only_required_for_messages("match-class-bind-self")
  112. def visit_matchas(self, node: nodes.MatchAs) -> None:
  113. match node:
  114. case nodes.MatchAs(
  115. parent=nodes.MatchClass(cls=nodes.Name() as cls_name, patterns=[_]),
  116. name=nodes.AssignName(name=name),
  117. pattern=None,
  118. ):
  119. inferred = safe_infer(cls_name)
  120. if (
  121. isinstance(inferred, nodes.ClassDef)
  122. and inferred.qname() in MATCH_CLASS_SELF_NAMES
  123. ):
  124. self.add_message(
  125. "match-class-bind-self",
  126. node=node,
  127. args=(cls_name.name, name),
  128. confidence=HIGH,
  129. )
  130. @staticmethod
  131. def get_match_args_for_class(node: nodes.NodeNG) -> list[str] | None:
  132. """Infer __match_args__ from class name."""
  133. inferred = safe_infer(node)
  134. if not isinstance(inferred, nodes.ClassDef):
  135. return None
  136. try:
  137. match_args = inferred.getattr("__match_args__")
  138. except astroid.exceptions.NotFoundError:
  139. if inferred.qname() in MATCH_CLASS_SELF_NAMES:
  140. return ["<self>"]
  141. return None
  142. match match_args:
  143. case [
  144. nodes.AssignName(parent=nodes.Assign(value=nodes.Tuple(elts=elts))),
  145. *_,
  146. ] if all(
  147. isinstance(el, nodes.Const) and isinstance(el.value, str) for el in elts
  148. ):
  149. return [el.value for el in elts]
  150. case _:
  151. return None
  152. def check_duplicate_sub_patterns(
  153. self, name: str, node: nodes.NodeNG, *, attrs: set[str], dups: set[str]
  154. ) -> None:
  155. """Track attribute names and emit error if name is given more than once."""
  156. if name in attrs and name not in dups:
  157. dups.add(name)
  158. self.add_message(
  159. "multiple-class-sub-patterns",
  160. node=node,
  161. args=(name,),
  162. confidence=INFERENCE,
  163. )
  164. else:
  165. attrs.add(name)
  166. @only_required_for_messages(
  167. "match-class-positional-attributes",
  168. "multiple-class-sub-patterns",
  169. "too-many-positional-sub-patterns",
  170. )
  171. def visit_matchclass(self, node: nodes.MatchClass) -> None:
  172. attrs: set[str] = set()
  173. dups: set[str] = set()
  174. if (
  175. node.patterns
  176. and (match_args := self.get_match_args_for_class(node.cls)) is not None
  177. ):
  178. if len(node.patterns) > len(match_args):
  179. self.add_message(
  180. "too-many-positional-sub-patterns",
  181. node=node,
  182. args=(node.cls.as_string(), len(match_args), len(node.patterns)),
  183. confidence=INFERENCE,
  184. )
  185. return
  186. inferred = safe_infer(node.cls)
  187. if not (
  188. isinstance(inferred, nodes.ClassDef)
  189. and (
  190. inferred.qname() in MATCH_CLASS_SELF_NAMES
  191. or "tuple" in inferred.basenames
  192. )
  193. ):
  194. attributes = [f"'{attr}'" for attr in match_args[: len(node.patterns)]]
  195. self.add_message(
  196. "match-class-positional-attributes",
  197. node=node,
  198. args=(", ".join(attributes),),
  199. confidence=INFERENCE,
  200. )
  201. for i in range(len(node.patterns)):
  202. name = match_args[i]
  203. self.check_duplicate_sub_patterns(name, node, attrs=attrs, dups=dups)
  204. for kw_name in node.kwd_attrs:
  205. self.check_duplicate_sub_patterns(kw_name, node, attrs=attrs, dups=dups)
  206. def register(linter: PyLinter) -> None:
  207. linter.register_checker(MatchStatementChecker(linter))