Skip to content

Commit 9227bce

Browse files
authored
Show error codes for some notes (#13880)
This will hint people affected by #13851 what to do to silence this. I am not doing it for all notes as this will cause too much noise (especially for some nicely formatted multi-line notes like possible overloads, incompatible overrides, or protocol mismatches), instead we can select specific codes that we want to show.
1 parent 7cc024a commit 9227bce

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/errors.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
T = TypeVar("T")
1919

20+
# Show error codes for some note-level messages (these usually appear alone
21+
# and not as a comment for a previous error-level message).
22+
SHOW_NOTE_CODES: Final = {codes.ANNOTATION_UNCHECKED}
2023
allowed_duplicates: Final = ["@overload", "Got:", "Expected:"]
2124

2225
# Keep track of the original error code when the error code of a message is changed.
@@ -782,7 +785,11 @@ def format_messages(
782785
s = f"{srcloc}: {severity}: {message}"
783786
else:
784787
s = message
785-
if not self.hide_error_codes and code and severity != "note":
788+
if (
789+
not self.hide_error_codes
790+
and code
791+
and (severity != "note" or code in SHOW_NOTE_CODES)
792+
):
786793
# If note has an error code, it is related to a previous error. Avoid
787794
# displaying duplicate error codes.
788795
s = f"{s} [{code.code}]"

test-data/unit/check-errorcodes.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,10 @@ T = TypeVar("T")
966966
def test(tp: Type[T]) -> T: ...
967967
test(C) # E: Only concrete class can be given where "Type[C]" is expected [type-abstract]
968968

969+
[case testUncheckedAnnotationCodeShown]
970+
def f():
971+
x: int = "no" # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked]
972+
969973
[case testUncheckedAnnotationSuppressed]
970974
# flags: --disable-error-code=annotation-unchecked
971975
def f():

0 commit comments

Comments
 (0)