Skip to content

(fix): handle no docs in source code #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/scanpydoc/rtd_github_links/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ def _get_linenos(obj: _SourceObjectType) -> tuple[int, int] | tuple[None, None]:
"""Get an object’s line numbers."""
try:
lines, start = inspect.getsourcelines(obj)
except TypeError:
# https://docs.python.org/3/library/inspect.html#inspect.getsourcelines
# means an OSError is raised if the source is not found,
# as is the case with collections.abc.Mapping.
# A TypeError indicates a builtin class.
except (TypeError, OSError):
return None, None
else:
return start, start + len(lines) - 1
Expand Down
7 changes: 7 additions & 0 deletions tests/test_rtd_github_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import TYPE_CHECKING
from pathlib import Path, PurePosixPath
from importlib import import_module
from collections.abc import Mapping

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


@pytest.mark.parametrize("cls", [dict, Mapping])
def test_no_line_nos_for_unavailable_source(cls: type) -> None:
start, end = _get_linenos(cls)
assert start is end is None


def test_get_github_url_only_annotation(prefix: PurePosixPath) -> None:
"""Doesn’t really work but shouldn’t crash either."""
url = github_url(f"{_testdata.__name__}.TestCls.test_anno")
Expand Down