From e1a5a00e9855c60ac53b9cf7386358bfccf0fca8 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Wed, 12 Oct 2022 21:59:49 +0100 Subject: [PATCH] Show error codes for some notes --- mypy/errors.py | 9 ++++++++- test-data/unit/check-errorcodes.test | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/mypy/errors.py b/mypy/errors.py index 53c00a8b368b..bfc44a858010 100644 --- a/mypy/errors.py +++ b/mypy/errors.py @@ -17,6 +17,9 @@ T = TypeVar("T") +# Show error codes for some note-level messages (these usually appear alone +# and not as a comment for a previous error-level message). +SHOW_NOTE_CODES: Final = {codes.ANNOTATION_UNCHECKED} allowed_duplicates: Final = ["@overload", "Got:", "Expected:"] # Keep track of the original error code when the error code of a message is changed. @@ -782,7 +785,11 @@ def format_messages( s = f"{srcloc}: {severity}: {message}" else: s = message - if not self.hide_error_codes and code and severity != "note": + if ( + not self.hide_error_codes + and code + and (severity != "note" or code in SHOW_NOTE_CODES) + ): # If note has an error code, it is related to a previous error. Avoid # displaying duplicate error codes. s = f"{s} [{code.code}]" diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index 055ee922f1a8..0ab9c02a373b 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -966,6 +966,10 @@ T = TypeVar("T") def test(tp: Type[T]) -> T: ... test(C) # E: Only concrete class can be given where "Type[C]" is expected [type-abstract] +[case testUncheckedAnnotationCodeShown] +def f(): + x: int = "no" # N: By default the bodies of untyped functions are not checked, consider using --check-untyped-defs [annotation-unchecked] + [case testUncheckedAnnotationSuppressed] # flags: --disable-error-code=annotation-unchecked def f():