Skip to content

Commit f955623

Browse files
[refactor] Update errorcodes.py: harmonize all of the type annotations (#19880)
This completes an in-line todo from 3 years ago relating to a weakness that mypy no longer has (edit: I did not read carefully enough. But I did eventually stumble upon a sufficient workaround for mypyc/mypyc#1142), and removes code with no downside.
1 parent 4301be1 commit f955623

File tree

2 files changed

+26
-32
lines changed

2 files changed

+26
-32
lines changed

mypy/errorcodes.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,28 @@ def __hash__(self) -> int:
5252

5353
ATTR_DEFINED: Final = ErrorCode("attr-defined", "Check that attribute exists", "General")
5454
NAME_DEFINED: Final = ErrorCode("name-defined", "Check that name is defined", "General")
55-
CALL_ARG: Final[ErrorCode] = ErrorCode(
55+
CALL_ARG: Final = ErrorCode(
5656
"call-arg", "Check number, names and kinds of arguments in calls", "General"
5757
)
5858
ARG_TYPE: Final = ErrorCode("arg-type", "Check argument types in calls", "General")
5959
CALL_OVERLOAD: Final = ErrorCode(
6060
"call-overload", "Check that an overload variant matches arguments", "General"
6161
)
62-
VALID_TYPE: Final[ErrorCode] = ErrorCode(
63-
"valid-type", "Check that type (annotation) is valid", "General"
64-
)
62+
VALID_TYPE: Final = ErrorCode("valid-type", "Check that type (annotation) is valid", "General")
6563
VAR_ANNOTATED: Final = ErrorCode(
6664
"var-annotated", "Require variable annotation if type can't be inferred", "General"
6765
)
6866
OVERRIDE: Final = ErrorCode(
6967
"override", "Check that method override is compatible with base class", "General"
7068
)
71-
RETURN: Final[ErrorCode] = ErrorCode(
72-
"return", "Check that function always returns a value", "General"
73-
)
74-
RETURN_VALUE: Final[ErrorCode] = ErrorCode(
69+
RETURN: Final = ErrorCode("return", "Check that function always returns a value", "General")
70+
RETURN_VALUE: Final = ErrorCode(
7571
"return-value", "Check that return value is compatible with signature", "General"
7672
)
77-
ASSIGNMENT: Final[ErrorCode] = ErrorCode(
73+
ASSIGNMENT: Final = ErrorCode(
7874
"assignment", "Check that assigned value is compatible with target", "General"
7975
)
80-
METHOD_ASSIGN: Final[ErrorCode] = ErrorCode(
76+
METHOD_ASSIGN: Final = ErrorCode(
8177
"method-assign",
8278
"Check that assignment target is not a method",
8379
"General",
@@ -143,9 +139,7 @@ def __hash__(self) -> int:
143139
UNUSED_COROUTINE: Final = ErrorCode(
144140
"unused-coroutine", "Ensure that all coroutines are used", "General"
145141
)
146-
# TODO: why do we need the explicit type here? Without it mypyc CI builds fail with
147-
# mypy/message_registry.py:37: error: Cannot determine type of "EMPTY_BODY" [has-type]
148-
EMPTY_BODY: Final[ErrorCode] = ErrorCode(
142+
EMPTY_BODY: Final = ErrorCode(
149143
"empty-body",
150144
"A dedicated error code to opt out return errors for empty/trivial bodies",
151145
"General",
@@ -160,7 +154,7 @@ def __hash__(self) -> int:
160154
"await-not-async", 'Warn about "await" outside coroutine ("async def")', "General"
161155
)
162156
# These error codes aren't enabled by default.
163-
NO_UNTYPED_DEF: Final[ErrorCode] = ErrorCode(
157+
NO_UNTYPED_DEF: Final = ErrorCode(
164158
"no-untyped-def", "Check that every function has an annotation", "General"
165159
)
166160
NO_UNTYPED_CALL: Final = ErrorCode(
@@ -186,13 +180,13 @@ def __hash__(self) -> int:
186180
UNREACHABLE: Final = ErrorCode(
187181
"unreachable", "Warn about unreachable statements or expressions", "General"
188182
)
189-
ANNOTATION_UNCHECKED = ErrorCode(
183+
ANNOTATION_UNCHECKED: Final = ErrorCode(
190184
"annotation-unchecked", "Notify about type annotations in unchecked functions", "General"
191185
)
192-
TYPEDDICT_READONLY_MUTATED = ErrorCode(
186+
TYPEDDICT_READONLY_MUTATED: Final = ErrorCode(
193187
"typeddict-readonly-mutated", "TypedDict's ReadOnly key is mutated", "General"
194188
)
195-
POSSIBLY_UNDEFINED: Final[ErrorCode] = ErrorCode(
189+
POSSIBLY_UNDEFINED: Final = ErrorCode(
196190
"possibly-undefined",
197191
"Warn about variables that are defined only in some execution paths",
198192
"General",
@@ -201,18 +195,18 @@ def __hash__(self) -> int:
201195
REDUNDANT_EXPR: Final = ErrorCode(
202196
"redundant-expr", "Warn about redundant expressions", "General", default_enabled=False
203197
)
204-
TRUTHY_BOOL: Final[ErrorCode] = ErrorCode(
198+
TRUTHY_BOOL: Final = ErrorCode(
205199
"truthy-bool",
206200
"Warn about expressions that could always evaluate to true in boolean contexts",
207201
"General",
208202
default_enabled=False,
209203
)
210-
TRUTHY_FUNCTION: Final[ErrorCode] = ErrorCode(
204+
TRUTHY_FUNCTION: Final = ErrorCode(
211205
"truthy-function",
212206
"Warn about function that always evaluate to true in boolean contexts",
213207
"General",
214208
)
215-
TRUTHY_ITERABLE: Final[ErrorCode] = ErrorCode(
209+
TRUTHY_ITERABLE: Final = ErrorCode(
216210
"truthy-iterable",
217211
"Warn about Iterable expressions that could always evaluate to true in boolean contexts",
218212
"General",
@@ -238,13 +232,13 @@ def __hash__(self) -> int:
238232
"General",
239233
default_enabled=False,
240234
)
241-
REDUNDANT_SELF_TYPE = ErrorCode(
235+
REDUNDANT_SELF_TYPE: Final = ErrorCode(
242236
"redundant-self",
243237
"Warn about redundant Self type annotations on method first argument",
244238
"General",
245239
default_enabled=False,
246240
)
247-
USED_BEFORE_DEF: Final[ErrorCode] = ErrorCode(
241+
USED_BEFORE_DEF: Final = ErrorCode(
248242
"used-before-def", "Warn about variables that are used before they are defined", "General"
249243
)
250244
UNUSED_IGNORE: Final = ErrorCode(
@@ -262,7 +256,7 @@ def __hash__(self) -> int:
262256
"General",
263257
default_enabled=False,
264258
)
265-
MUTABLE_OVERRIDE: Final[ErrorCode] = ErrorCode(
259+
MUTABLE_OVERRIDE: Final = ErrorCode(
266260
"mutable-override",
267261
"Reject covariant overrides for mutable attributes",
268262
"General",
@@ -274,42 +268,41 @@ def __hash__(self) -> int:
274268
"General",
275269
default_enabled=False,
276270
)
277-
METACLASS: Final[ErrorCode] = ErrorCode("metaclass", "Ensure that metaclass is valid", "General")
271+
METACLASS: Final = ErrorCode("metaclass", "Ensure that metaclass is valid", "General")
278272

279273
# Syntax errors are often blocking.
280-
SYNTAX: Final[ErrorCode] = ErrorCode("syntax", "Report syntax errors", "General")
274+
SYNTAX: Final = ErrorCode("syntax", "Report syntax errors", "General")
281275

282276
# This is an internal marker code for a whole-file ignore. It is not intended to
283277
# be user-visible.
284278
FILE: Final = ErrorCode("file", "Internal marker for a whole file being ignored", "General")
285279
del error_codes[FILE.code]
286280

287281
# This is a catch-all for remaining uncategorized errors.
288-
MISC: Final[ErrorCode] = ErrorCode("misc", "Miscellaneous other checks", "General")
282+
MISC: Final = ErrorCode("misc", "Miscellaneous other checks", "General")
289283

290-
OVERLOAD_CANNOT_MATCH: Final[ErrorCode] = ErrorCode(
284+
OVERLOAD_CANNOT_MATCH: Final = ErrorCode(
291285
"overload-cannot-match",
292286
"Warn if an @overload signature can never be matched",
293287
"General",
294288
sub_code_of=MISC,
295289
)
296290

297-
298-
OVERLOAD_OVERLAP: Final[ErrorCode] = ErrorCode(
291+
OVERLOAD_OVERLAP: Final = ErrorCode(
299292
"overload-overlap",
300293
"Warn if multiple @overload variants overlap in unsafe ways",
301294
"General",
302295
sub_code_of=MISC,
303296
)
304297

305-
PROPERTY_DECORATOR = ErrorCode(
298+
PROPERTY_DECORATOR: Final = ErrorCode(
306299
"prop-decorator",
307300
"Decorators on top of @property are not supported",
308301
"General",
309302
sub_code_of=MISC,
310303
)
311304

312-
NARROWED_TYPE_NOT_SUBTYPE: Final[ErrorCode] = ErrorCode(
305+
NARROWED_TYPE_NOT_SUBTYPE: Final = ErrorCode(
313306
"narrowed-type-not-subtype",
314307
"Warn if a TypeIs function's narrowed type is not a subtype of the original type",
315308
"General",

mypy/message_registry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
from typing import Final, NamedTuple
1212

1313
from mypy import errorcodes as codes
14+
from mypy.errorcodes import ErrorCode
1415

1516

1617
class ErrorMessage(NamedTuple):
1718
value: str
18-
code: codes.ErrorCode | None = None
19+
code: ErrorCode | None = None
1920

2021
def format(self, *args: object, **kwargs: object) -> ErrorMessage:
2122
return ErrorMessage(self.value.format(*args, **kwargs), code=self.code)

0 commit comments

Comments
 (0)