Skip to content

Commit 0e8e135

Browse files
dosisodilevkivskyi
authored andcommitted
Fixed indicator position in code with tabs (#8307)
1 parent 2d3a1bf commit 0e8e135

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

mypy/errors.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,17 @@ def format_messages(self, error_info: List[ErrorInfo],
451451
# Add source code fragment and a location marker.
452452
if severity == 'error' and source_lines and line > 0:
453453
source_line = source_lines[line - 1]
454+
source_line_expanded = source_line.expandtabs()
454455
if column < 0:
455456
# Something went wrong, take first non-empty column.
456457
column = len(source_line) - len(source_line.lstrip())
458+
459+
# Shifts column after tab expansion
460+
column = len(source_line[:column].expandtabs())
461+
457462
# Note, currently coloring uses the offset to detect source snippets,
458463
# so these offsets should not be arbitrary.
459-
a.append(' ' * DEFAULT_SOURCE_OFFSET + source_line)
464+
a.append(' ' * DEFAULT_SOURCE_OFFSET + source_line_expanded)
460465
a.append(' ' * (DEFAULT_SOURCE_OFFSET + column) + '^')
461466
return a
462467

test-data/unit/cmdline.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,16 @@ some_file.py:1: error: invalid syntax [syntax]
10361036
^
10371037
== Return code: 2
10381038

1039+
[case testTabRenderingUponError]
1040+
# cmd: mypy --pretty tabs.py
1041+
[file tabs.py]
1042+
def test_tabs() -> str:
1043+
return None
1044+
[out]
1045+
tabs.py:2: error: Incompatible return value type (got "None", expected "str")
1046+
return None
1047+
^
1048+
10391049
[case testSpecialTypeshedGenericNote]
10401050
# cmd: mypy --disallow-any-generics --python-version=3.6 test.py
10411051
[file test.py]

test-data/unit/fine-grained-blockers.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def f() -> None: pass
4040
==
4141
a.py:1: error: invalid syntax [syntax]
4242
def f(x: int) ->
43-
^
43+
^
4444
==
4545
main:3: error: Too few arguments for "f" [call-arg]
4646
a.f()

0 commit comments

Comments
 (0)