errorcodes.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. """Classification of possible errors mypy can detect.
  2. These can be used for filtering specific errors.
  3. """
  4. from __future__ import annotations
  5. from collections import defaultdict
  6. from typing import Final
  7. from mypy_extensions import mypyc_attr
  8. error_codes: dict[str, ErrorCode] = {}
  9. sub_code_map: dict[str, set[str]] = defaultdict(set)
  10. @mypyc_attr(allow_interpreted_subclasses=True)
  11. class ErrorCode:
  12. def __init__(
  13. self,
  14. code: str,
  15. description: str,
  16. category: str,
  17. default_enabled: bool = True,
  18. sub_code_of: ErrorCode | None = None,
  19. ) -> None:
  20. self.code = code
  21. self.description = description
  22. self.category = category
  23. self.default_enabled = default_enabled
  24. self.sub_code_of = sub_code_of
  25. if sub_code_of is not None:
  26. assert sub_code_of.sub_code_of is None, "Nested subcategories are not supported"
  27. sub_code_map[sub_code_of.code].add(code)
  28. error_codes[code] = self
  29. def __str__(self) -> str:
  30. return f"<ErrorCode {self.code}>"
  31. def __eq__(self, other: object) -> bool:
  32. if not isinstance(other, ErrorCode):
  33. return False
  34. return self.code == other.code
  35. def __hash__(self) -> int:
  36. return hash((self.code,))
  37. ATTR_DEFINED: Final = ErrorCode("attr-defined", "Check that attribute exists", "General")
  38. NAME_DEFINED: Final = ErrorCode("name-defined", "Check that name is defined", "General")
  39. CALL_ARG: Final[ErrorCode] = ErrorCode(
  40. "call-arg", "Check number, names and kinds of arguments in calls", "General"
  41. )
  42. ARG_TYPE: Final = ErrorCode("arg-type", "Check argument types in calls", "General")
  43. CALL_OVERLOAD: Final = ErrorCode(
  44. "call-overload", "Check that an overload variant matches arguments", "General"
  45. )
  46. VALID_TYPE: Final[ErrorCode] = ErrorCode(
  47. "valid-type", "Check that type (annotation) is valid", "General"
  48. )
  49. VAR_ANNOTATED: Final = ErrorCode(
  50. "var-annotated", "Require variable annotation if type can't be inferred", "General"
  51. )
  52. OVERRIDE: Final = ErrorCode(
  53. "override", "Check that method override is compatible with base class", "General"
  54. )
  55. RETURN: Final[ErrorCode] = ErrorCode(
  56. "return", "Check that function always returns a value", "General"
  57. )
  58. RETURN_VALUE: Final[ErrorCode] = ErrorCode(
  59. "return-value", "Check that return value is compatible with signature", "General"
  60. )
  61. ASSIGNMENT: Final[ErrorCode] = ErrorCode(
  62. "assignment", "Check that assigned value is compatible with target", "General"
  63. )
  64. METHOD_ASSIGN: Final[ErrorCode] = ErrorCode(
  65. "method-assign",
  66. "Check that assignment target is not a method",
  67. "General",
  68. sub_code_of=ASSIGNMENT,
  69. )
  70. TYPE_ARG: Final = ErrorCode("type-arg", "Check that generic type arguments are present", "General")
  71. TYPE_VAR: Final = ErrorCode("type-var", "Check that type variable values are valid", "General")
  72. UNION_ATTR: Final = ErrorCode(
  73. "union-attr", "Check that attribute exists in each item of a union", "General"
  74. )
  75. INDEX: Final = ErrorCode("index", "Check indexing operations", "General")
  76. OPERATOR: Final = ErrorCode("operator", "Check that operator is valid for operands", "General")
  77. LIST_ITEM: Final = ErrorCode(
  78. "list-item", "Check list items in a list expression [item, ...]", "General"
  79. )
  80. DICT_ITEM: Final = ErrorCode(
  81. "dict-item", "Check dict items in a dict expression {key: value, ...}", "General"
  82. )
  83. TYPEDDICT_ITEM: Final = ErrorCode(
  84. "typeddict-item", "Check items when constructing TypedDict", "General"
  85. )
  86. TYPEDDICT_UNKNOWN_KEY: Final = ErrorCode(
  87. "typeddict-unknown-key",
  88. "Check unknown keys when constructing TypedDict",
  89. "General",
  90. sub_code_of=TYPEDDICT_ITEM,
  91. )
  92. HAS_TYPE: Final = ErrorCode(
  93. "has-type", "Check that type of reference can be determined", "General"
  94. )
  95. IMPORT: Final = ErrorCode(
  96. "import", "Require that imported module can be found or has stubs", "General"
  97. )
  98. NO_REDEF: Final = ErrorCode("no-redef", "Check that each name is defined once", "General")
  99. FUNC_RETURNS_VALUE: Final = ErrorCode(
  100. "func-returns-value", "Check that called function returns a value in value context", "General"
  101. )
  102. ABSTRACT: Final = ErrorCode(
  103. "abstract", "Prevent instantiation of classes with abstract attributes", "General"
  104. )
  105. TYPE_ABSTRACT: Final = ErrorCode(
  106. "type-abstract", "Require only concrete classes where Type[...] is expected", "General"
  107. )
  108. VALID_NEWTYPE: Final = ErrorCode(
  109. "valid-newtype", "Check that argument 2 to NewType is valid", "General"
  110. )
  111. STRING_FORMATTING: Final = ErrorCode(
  112. "str-format", "Check that string formatting/interpolation is type-safe", "General"
  113. )
  114. STR_BYTES_PY3: Final = ErrorCode(
  115. "str-bytes-safe", "Warn about implicit coercions related to bytes and string types", "General"
  116. )
  117. EXIT_RETURN: Final = ErrorCode(
  118. "exit-return", "Warn about too general return type for '__exit__'", "General"
  119. )
  120. LITERAL_REQ: Final = ErrorCode("literal-required", "Check that value is a literal", "General")
  121. UNUSED_COROUTINE: Final = ErrorCode(
  122. "unused-coroutine", "Ensure that all coroutines are used", "General"
  123. )
  124. # TODO: why do we need the explicit type here? Without it mypyc CI builds fail with
  125. # mypy/message_registry.py:37: error: Cannot determine type of "EMPTY_BODY" [has-type]
  126. EMPTY_BODY: Final[ErrorCode] = ErrorCode(
  127. "empty-body",
  128. "A dedicated error code to opt out return errors for empty/trivial bodies",
  129. "General",
  130. )
  131. SAFE_SUPER: Final = ErrorCode(
  132. "safe-super", "Warn about calls to abstract methods with empty/trivial bodies", "General"
  133. )
  134. TOP_LEVEL_AWAIT: Final = ErrorCode(
  135. "top-level-await", "Warn about top level await expressions", "General"
  136. )
  137. # These error codes aren't enabled by default.
  138. NO_UNTYPED_DEF: Final[ErrorCode] = ErrorCode(
  139. "no-untyped-def", "Check that every function has an annotation", "General"
  140. )
  141. NO_UNTYPED_CALL: Final = ErrorCode(
  142. "no-untyped-call",
  143. "Disallow calling functions without type annotations from annotated functions",
  144. "General",
  145. )
  146. REDUNDANT_CAST: Final = ErrorCode(
  147. "redundant-cast", "Check that cast changes type of expression", "General"
  148. )
  149. ASSERT_TYPE: Final = ErrorCode("assert-type", "Check that assert_type() call succeeds", "General")
  150. COMPARISON_OVERLAP: Final = ErrorCode(
  151. "comparison-overlap", "Check that types in comparisons and 'in' expressions overlap", "General"
  152. )
  153. NO_ANY_UNIMPORTED: Final = ErrorCode(
  154. "no-any-unimported", 'Reject "Any" types from unfollowed imports', "General"
  155. )
  156. NO_ANY_RETURN: Final = ErrorCode(
  157. "no-any-return",
  158. 'Reject returning value with "Any" type if return type is not "Any"',
  159. "General",
  160. )
  161. UNREACHABLE: Final = ErrorCode(
  162. "unreachable", "Warn about unreachable statements or expressions", "General"
  163. )
  164. ANNOTATION_UNCHECKED = ErrorCode(
  165. "annotation-unchecked", "Notify about type annotations in unchecked functions", "General"
  166. )
  167. POSSIBLY_UNDEFINED: Final[ErrorCode] = ErrorCode(
  168. "possibly-undefined",
  169. "Warn about variables that are defined only in some execution paths",
  170. "General",
  171. default_enabled=False,
  172. )
  173. REDUNDANT_EXPR: Final = ErrorCode(
  174. "redundant-expr", "Warn about redundant expressions", "General", default_enabled=False
  175. )
  176. TRUTHY_BOOL: Final[ErrorCode] = ErrorCode(
  177. "truthy-bool",
  178. "Warn about expressions that could always evaluate to true in boolean contexts",
  179. "General",
  180. default_enabled=False,
  181. )
  182. TRUTHY_FUNCTION: Final[ErrorCode] = ErrorCode(
  183. "truthy-function",
  184. "Warn about function that always evaluate to true in boolean contexts",
  185. "General",
  186. )
  187. TRUTHY_ITERABLE: Final[ErrorCode] = ErrorCode(
  188. "truthy-iterable",
  189. "Warn about Iterable expressions that could always evaluate to true in boolean contexts",
  190. "General",
  191. default_enabled=False,
  192. )
  193. NAME_MATCH: Final = ErrorCode(
  194. "name-match", "Check that type definition has consistent naming", "General"
  195. )
  196. NO_OVERLOAD_IMPL: Final = ErrorCode(
  197. "no-overload-impl",
  198. "Check that overloaded functions outside stub files have an implementation",
  199. "General",
  200. )
  201. IGNORE_WITHOUT_CODE: Final = ErrorCode(
  202. "ignore-without-code",
  203. "Warn about '# type: ignore' comments which do not have error codes",
  204. "General",
  205. default_enabled=False,
  206. )
  207. UNUSED_AWAITABLE: Final = ErrorCode(
  208. "unused-awaitable",
  209. "Ensure that all awaitable values are used",
  210. "General",
  211. default_enabled=False,
  212. )
  213. REDUNDANT_SELF_TYPE = ErrorCode(
  214. "redundant-self",
  215. "Warn about redundant Self type annotations on method first argument",
  216. "General",
  217. default_enabled=False,
  218. )
  219. USED_BEFORE_DEF: Final[ErrorCode] = ErrorCode(
  220. "used-before-def", "Warn about variables that are used before they are defined", "General"
  221. )
  222. UNUSED_IGNORE: Final = ErrorCode(
  223. "unused-ignore", "Ensure that all type ignores are used", "General", default_enabled=False
  224. )
  225. EXPLICIT_OVERRIDE_REQUIRED: Final = ErrorCode(
  226. "explicit-override",
  227. "Require @override decorator if method is overriding a base class method",
  228. "General",
  229. default_enabled=False,
  230. )
  231. # Syntax errors are often blocking.
  232. SYNTAX: Final[ErrorCode] = ErrorCode("syntax", "Report syntax errors", "General")
  233. # This is an internal marker code for a whole-file ignore. It is not intended to
  234. # be user-visible.
  235. FILE: Final = ErrorCode("file", "Internal marker for a whole file being ignored", "General")
  236. del error_codes[FILE.code]
  237. # This is a catch-all for remaining uncategorized errors.
  238. MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")