Skip to content

Commit 2c6c395

Browse files
authored
Make --pretty work better on multi-line issues (#20056)
We can just print the first line for things where there's multiple lines being the issue. Ideally, we would print the first line, last line, and explicitly elide the lines in between. That's for a future change! (maybe) Fixes #18522. NOTE: this is an aesthetic thing, there's no right formatting. So I would be fine if this is closed because others think it looks worse.
1 parent 94d0d7e commit 2c6c395

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

mypy/errors.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,9 @@ def format_messages(
10031003
marker = "^"
10041004
if end_line == line and end_column > column:
10051005
marker = f'^{"~" * (end_column - column - 1)}'
1006+
elif end_line != line:
1007+
# just highlight the first line instead
1008+
marker = f'^{"~" * (len(source_line_expanded) - column - 1)}'
10061009
a.append(" " * (DEFAULT_SOURCE_OFFSET + column) + marker)
10071010
return a
10081011

test-data/unit/check-unreachable-code.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,3 +1619,14 @@ reveal_type(bar().attr) # N: Revealed type is "Never"
16191619
1 # not unreachable
16201620
reveal_type(foo().attr) # N: Revealed type is "Never"
16211621
1 # E: Statement is unreachable
1622+
1623+
[case testUnreachableStatementPrettyHighlighting]
1624+
# flags: --warn-unreachable --pretty
1625+
def x() -> None:
1626+
assert False
1627+
if 5:
1628+
pass
1629+
[out]
1630+
main:4: error: Statement is unreachable
1631+
if 5:
1632+
^~~~~

test-data/unit/daemon.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Daemon stopped
122122
Daemon started
123123
foo.py:1: error: Function is missing a return type annotation
124124
def f():
125-
^
125+
^~~~~~~~
126126
foo.py:1: note: Use "-> None" if function does not return a value
127127
Found 1 error in 1 file (checked 1 source file)
128128
== Return code: 1

0 commit comments

Comments
 (0)