diff --git a/mypy/errors.py b/mypy/errors.py index 1b092fb50e4a..69e4fb4cf065 100644 --- a/mypy/errors.py +++ b/mypy/errors.py @@ -1003,6 +1003,9 @@ def format_messages( marker = "^" if end_line == line and end_column > column: marker = f'^{"~" * (end_column - column - 1)}' + elif end_line != line: + # just highlight the first line instead + marker = f'^{"~" * (len(source_line_expanded) - column - 1)}' a.append(" " * (DEFAULT_SOURCE_OFFSET + column) + marker) return a diff --git a/test-data/unit/check-unreachable-code.test b/test-data/unit/check-unreachable-code.test index 645f81e89ca1..7e00671dfd11 100644 --- a/test-data/unit/check-unreachable-code.test +++ b/test-data/unit/check-unreachable-code.test @@ -1619,3 +1619,14 @@ reveal_type(bar().attr) # N: Revealed type is "Never" 1 # not unreachable reveal_type(foo().attr) # N: Revealed type is "Never" 1 # E: Statement is unreachable + +[case testUnreachableStatementPrettyHighlighting] +# flags: --warn-unreachable --pretty +def x() -> None: + assert False + if 5: + pass +[out] +main:4: error: Statement is unreachable + if 5: + ^~~~~ diff --git a/test-data/unit/daemon.test b/test-data/unit/daemon.test index 295eb4000d81..c02f78be1834 100644 --- a/test-data/unit/daemon.test +++ b/test-data/unit/daemon.test @@ -122,7 +122,7 @@ Daemon stopped Daemon started foo.py:1: error: Function is missing a return type annotation def f(): - ^ + ^~~~~~~~ foo.py:1: note: Use "-> None" if function does not return a value Found 1 error in 1 file (checked 1 source file) == Return code: 1