1"""Classification of possible errors mypy can detect.
2
3These can be used for filtering specific errors.
4"""
5
6from typing import Dict, List
7from typing_extensions import Final
8
9
10# All created error codes are implicitly stored in this list.
11all_error_codes = []  # type: List[ErrorCode]
12
13error_codes = {}  # type: Dict[str, ErrorCode]
14
15
16class ErrorCode:
17    def __init__(self, code: str,
18                 description: str,
19                 category: str,
20                 default_enabled: bool = True) -> None:
21        self.code = code
22        self.description = description
23        self.category = category
24        self.default_enabled = default_enabled
25        error_codes[code] = self
26
27    def __str__(self) -> str:
28        return '<ErrorCode {}>'.format(self.code)
29
30
31ATTR_DEFINED = ErrorCode(
32    'attr-defined', "Check that attribute exists", 'General')  # type: Final
33NAME_DEFINED = ErrorCode(
34    'name-defined', "Check that name is defined", 'General')  # type: Final
35CALL_ARG = ErrorCode(
36    'call-arg', "Check number, names and kinds of arguments in calls", 'General')  # type: Final
37ARG_TYPE = ErrorCode(
38    'arg-type', "Check argument types in calls", 'General')  # type: Final
39CALL_OVERLOAD = ErrorCode(
40    'call-overload', "Check that an overload variant matches arguments", 'General')  # type: Final
41VALID_TYPE = ErrorCode(
42    'valid-type', "Check that type (annotation) is valid", 'General')  # type: Final
43VAR_ANNOTATED = ErrorCode(
44    'var-annotated', "Require variable annotation if type can't be inferred",
45    'General')  # type: Final
46OVERRIDE = ErrorCode(
47    'override', "Check that method override is compatible with base class",
48    'General')  # type: Final
49RETURN = ErrorCode(
50    'return', "Check that function always returns a value", 'General')  # type: Final
51RETURN_VALUE = ErrorCode(
52    'return-value', "Check that return value is compatible with signature",
53    'General')  # type: Final
54ASSIGNMENT = ErrorCode(
55    'assignment', "Check that assigned value is compatible with target", 'General')  # type: Final
56TYPE_ARG = ErrorCode(
57    'type-arg', "Check that generic type arguments are present", 'General')  # type: Final
58TYPE_VAR = ErrorCode(
59    'type-var', "Check that type variable values are valid", 'General')  # type: Final
60UNION_ATTR = ErrorCode(
61    'union-attr', "Check that attribute exists in each item of a union", 'General')  # type: Final
62INDEX = ErrorCode(
63    'index', "Check indexing operations", 'General')  # type: Final
64OPERATOR = ErrorCode(
65    'operator', "Check that operator is valid for operands", 'General')  # type: Final
66LIST_ITEM = ErrorCode(
67    'list-item', "Check list items in a list expression [item, ...]", 'General')  # type: Final
68DICT_ITEM = ErrorCode(
69    'dict-item',
70    "Check dict items in a dict expression {key: value, ...}", 'General')  # type: Final
71TYPEDDICT_ITEM = ErrorCode(
72    'typeddict-item', "Check items when constructing TypedDict", 'General')  # type: Final
73HAS_TYPE = ErrorCode(
74    'has-type', "Check that type of reference can be determined", 'General')  # type: Final
75IMPORT = ErrorCode(
76    'import', "Require that imported module can be found or has stubs", 'General')  # type: Final
77NO_REDEF = ErrorCode(
78    'no-redef', "Check that each name is defined once", 'General')  # type: Final
79FUNC_RETURNS_VALUE = ErrorCode(
80    'func-returns-value', "Check that called function returns a value in value context",
81    'General')  # type: Final
82ABSTRACT = ErrorCode(
83    'abstract', "Prevent instantiation of classes with abstract attributes",
84    'General')  # type: Final
85VALID_NEWTYPE = ErrorCode(
86    'valid-newtype', "Check that argument 2 to NewType is valid", 'General')  # type: Final
87STRING_FORMATTING = ErrorCode(
88    'str-format', "Check that string formatting/interpolation is type-safe",
89    'General')  # type: Final
90STR_BYTES_PY3 = ErrorCode(
91    'str-bytes-safe', "Warn about dangerous coercions related to bytes and string types",
92    'General')  # type: Final
93EXIT_RETURN = ErrorCode(
94    'exit-return', "Warn about too general return type for '__exit__'", 'General')  # type: Final
95
96# These error codes aren't enabled by default.
97NO_UNTYPED_DEF = ErrorCode(
98    'no-untyped-def', "Check that every function has an annotation", 'General')  # type: Final
99NO_UNTYPED_CALL = ErrorCode(
100    'no-untyped-call',
101    "Disallow calling functions without type annotations from annotated functions",
102    'General')  # type: Final
103REDUNDANT_CAST = ErrorCode(
104    'redundant-cast', "Check that cast changes type of expression", 'General')  # type: Final
105COMPARISON_OVERLAP = ErrorCode(
106    'comparison-overlap',
107    "Check that types in comparisons and 'in' expressions overlap", 'General')  # type: Final
108NO_ANY_UNIMPORTED = ErrorCode(
109    'no-any-unimported', 'Reject "Any" types from unfollowed imports', 'General')  # type: Final
110NO_ANY_RETURN = ErrorCode(
111    'no-any-return', 'Reject returning value with "Any" type if return type is not "Any"',
112    'General')  # type: Final
113UNREACHABLE = ErrorCode(
114    'unreachable', "Warn about unreachable statements or expressions", 'General')  # type: Final
115REDUNDANT_EXPR = ErrorCode(
116    'redundant-expr',
117    "Warn about redundant expressions",
118    'General',
119    default_enabled=False)  # type: Final
120NAME_MATCH = ErrorCode(
121    'name-match', "Check that type definition has consistent naming", 'General')  # type: Final
122
123
124# Syntax errors are often blocking.
125SYNTAX = ErrorCode(
126    'syntax', "Report syntax errors", 'General')  # type: Final
127
128# This is a catch-all for remaining uncategorized errors.
129MISC = ErrorCode(
130    'misc', "Miscellaneous other checks", 'General')  # type: Final
131