Skip to content

Commit 0730a23

Browse files
Report using column numbers starting with 1
1 parent 20b9062 commit 0730a23

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ are compatible with.*
55

66
## 1.16.2
77

8-
* Improved source locations for errors in docstrings now including column offset.
8+
* Improved source locations for errors in docstrings now including column numbers
9+
(starting at 1).
910

1011
## 1.16.1
1112

src/mkdocstrings_handlers/python_xref/crossref.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def doc_value_offset_to_location(doc: Docstring, offset: int) -> tuple[int,int]:
343343
line and column or else (-1,-1) if it cannot be computed
344344
"""
345345
linenum = -1
346-
colnum = -1
346+
colnum = -2
347347

348348
if doc.lineno is not None:
349349
linenum = doc.lineno # start of the docstring source
@@ -359,8 +359,8 @@ def doc_value_offset_to_location(doc: Docstring, offset: int) -> tuple[int,int]:
359359
# adjust line offset by number of lines removed from front of docstring
360360
lineoffset += leading_space(rawvalue).count("\n")
361361

362-
if lineoffset == 0 and (m := re.match(r"(\s*['\"]{3}\s*)\S", source)):
363-
# is on the same line as opening triple quote
362+
if lineoffset == 0 and (m := re.match(r"(\s*['\"]{1,3}\s*)\S", source)):
363+
# is on the same line as opening quote
364364
colnum = offset + len(m.group(1))
365365
else:
366366
# indentation of first non-empty line in raw and cleaned up strings
@@ -381,7 +381,7 @@ def doc_value_offset_to_location(doc: Docstring, offset: int) -> tuple[int,int]:
381381

382382
linenum += lineoffset
383383

384-
return linenum, colnum
384+
return linenum, colnum + 1
385385

386386

387387
def leading_space(s: str) -> str:

tests/test_crossref.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,11 @@ def test_doc_value_offset_to_location() -> None:
223223
).lstrip("\n"),
224224
)
225225

226-
assert doc_value_offset_to_location(doc1, 0) == (1, 3)
227-
assert doc_value_offset_to_location(doc1, 3) == (1, 6)
228-
assert doc_value_offset_to_location(doc1, 7) == (2, 1)
229-
assert doc_value_offset_to_location(doc1, 15) == (3, 2)
226+
# note columns start with 1
227+
assert doc_value_offset_to_location(doc1, 0) == (1, 4)
228+
assert doc_value_offset_to_location(doc1, 3) == (1, 7)
229+
assert doc_value_offset_to_location(doc1, 7) == (2, 2)
230+
assert doc_value_offset_to_location(doc1, 15) == (3, 3)
230231

231232
doc2 = make_docstring_from_source(
232233
dedent(
@@ -242,9 +243,9 @@ def test_doc_value_offset_to_location() -> None:
242243
lineno=3,
243244
)
244245

245-
assert doc_value_offset_to_location(doc2, 0) == (3, 9)
246-
assert doc_value_offset_to_location(doc2, 6) == (4, 6)
247-
assert doc_value_offset_to_location(doc2, 15) == (5, 8)
246+
assert doc_value_offset_to_location(doc2, 0) == (3, 10)
247+
assert doc_value_offset_to_location(doc2, 6) == (4, 7)
248+
assert doc_value_offset_to_location(doc2, 15) == (5, 9)
248249

249250
# Remove parent so that source is not available
250251
doc2.parent = None
@@ -261,5 +262,5 @@ def test_doc_value_offset_to_location() -> None:
261262
).lstrip("\n"),
262263
)
263264

264-
assert doc_value_offset_to_location(doc3, 0) == (2, 4)
265-
assert doc_value_offset_to_location(doc3, 6) == (3, 2)
265+
assert doc_value_offset_to_location(doc3, 0) == (2, 5)
266+
assert doc_value_offset_to_location(doc3, 6) == (3, 3)

tests/test_integration.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_integration(tmpdir: PathLike) -> None:
8686
assert m[4] == 'myproj.bar.bad'
8787
# Source location not accurate in python 3.7
8888
bad_linenum = int(m[2])
89-
bad_col = int(m[3])
89+
bad_col = int(m[3]) - 1 # 1-based indexing
9090
bar_lines = bar_src_file.read_text().splitlines()
9191
bad_line = bar_lines[bad_linenum - 1]
9292
assert '[bad]' in bad_line

0 commit comments

Comments
 (0)