Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 90e01b6

Browse files
committed
Position squiggle for unused type ignore errors
mypy does not report correct column numbers for type comments. Go the extra mile and position `Unused "type: ignore" comment` correctly.
1 parent 5b2de02 commit 90e01b6

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

linter.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import logging
1717
import os
1818
import shutil
19+
import re
1920
import tempfile
2021
import time
2122
import threading
@@ -26,7 +27,8 @@
2627

2728
MYPY = False
2829
if MYPY:
29-
from typing import Dict, DefaultDict, Iterator, List, Optional, Protocol
30+
from typing import Dict, DefaultDict, Iterator, List, Optional, Protocol, Tuple
31+
from SublimeLinter.lint.linter import VirtualView
3032

3133
class TemporaryDirectory(Protocol):
3234
name = None # type: str
@@ -154,6 +156,31 @@ def find_errors(self, output):
154156
errors.append(error)
155157
yield from errors
156158

159+
def reposition_match(self, line, col, m, vv):
160+
# type: (int, Optional[int], LintMatch, VirtualView) -> Tuple[int, int, int]
161+
message = m['message']
162+
if message.startswith('Unused "type: ignore'):
163+
text = vv.select_line(line)
164+
# Search for the type comment on the actual line in the buffer
165+
match = re.search(r"#\s*type:\s*ignore(\[.+])?", text)
166+
if match:
167+
# Probably select the whole type comment
168+
a, b = match.span()
169+
# When we have a specific rule in the error,
170+
# e.g. 'Unused "type: ignore[import]" comment'
171+
match = re.search(r"type:\s*ignore\[([^,]+)]", message)
172+
if match:
173+
# Grab the rulename,
174+
rulename = match.group(1)
175+
try:
176+
# ... and find it in the type comment
177+
a = text[a:b].index(rulename) + a
178+
b = a + len(rulename)
179+
except ValueError:
180+
pass
181+
return line, a, b
182+
return super().reposition_match(line, col, m, vv)
183+
157184

158185
class FakeTemporaryDirectory:
159186
def __init__(self, name):

0 commit comments

Comments
 (0)