Skip to content

Commit 33124c3

Browse files
(fix): handle no docs in source code (#214)
Co-authored-by: Philipp A. <[email protected]>
1 parent 334cbe0 commit 33124c3

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/scanpydoc/rtd_github_links/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ def _get_linenos(obj: _SourceObjectType) -> tuple[int, int] | tuple[None, None]:
171171
"""Get an object’s line numbers."""
172172
try:
173173
lines, start = inspect.getsourcelines(obj)
174-
except TypeError:
174+
# https://docs.python.org/3/library/inspect.html#inspect.getsourcelines
175+
# means an OSError is raised if the source is not found,
176+
# as is the case with collections.abc.Mapping.
177+
# A TypeError indicates a builtin class.
178+
except (TypeError, OSError):
175179
return None, None
176180
else:
177181
return start, start + len(lines) - 1

tests/test_rtd_github_links.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import TYPE_CHECKING
99
from pathlib import Path, PurePosixPath
1010
from importlib import import_module
11+
from collections.abc import Mapping
1112

1213
import pytest
1314
from sphinx.config import Config
@@ -149,6 +150,12 @@ def test_as_function(
149150
assert github_url(f"scanpydoc.{module}.{name}") == f"{prefix}/{obj_path}#L{s}-L{e}"
150151

151152

153+
@pytest.mark.parametrize("cls", [dict, Mapping])
154+
def test_no_line_nos_for_unavailable_source(cls: type) -> None:
155+
start, end = _get_linenos(cls)
156+
assert start is end is None
157+
158+
152159
def test_get_github_url_only_annotation(prefix: PurePosixPath) -> None:
153160
"""Doesn’t really work but shouldn’t crash either."""
154161
url = github_url(f"{_testdata.__name__}.TestCls.test_anno")

0 commit comments

Comments
 (0)