errorcodes.py 8.8 KB

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