Skip to content

Commit 64df00b

Browse files
committed
fix: Don't crash when rendering the source of an object whose lineno is none
Issue-163: mkdocstrings#163
1 parent 0b8a3a9 commit 64df00b

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ Context:
157157
{{ init.relative_filepath }}
158158
{%- endif -%}
159159
</code></summary>
160-
{{ init.source|highlight(language="python", linestart=init.lineno, linenums=True) }}
160+
{{ init.source|highlight(language="python", linestart=init.lineno or 0, linenums=True) }}
161161
</details>
162162
{% endwith %}
163163
{% endif %}
@@ -170,7 +170,7 @@ Context:
170170
{{ class.relative_filepath }}
171171
{%- endif -%}
172172
</code></summary>
173-
{{ class.source|highlight(language="python", linestart=class.lineno, linenums=True) }}
173+
{{ class.source|highlight(language="python", linestart=class.lineno or 0, linenums=True) }}
174174
</details>
175175
{% endif %}
176176
{% endif %}

src/mkdocstrings_handlers/python/templates/material/_base/function.html.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ Context:
133133
{{ function.relative_filepath }}
134134
{%- endif -%}
135135
</code></summary>
136-
{{ function.source|highlight(language="python", linestart=function.lineno, linenums=True) }}
136+
{{ function.source|highlight(language="python", linestart=function.lineno or 0, linenums=True) }}
137137
</details>
138138
{% endif %}
139139
{% endblock source %}

tests/test_handler.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
import os
66
from glob import glob
7+
from textwrap import dedent
78
from typing import TYPE_CHECKING
89

910
import pytest
1011
from griffe.docstrings.dataclasses import DocstringSectionExamples, DocstringSectionKind
12+
from griffe.tests import temporary_visited_module
1113

1214
from mkdocstrings_handlers.python.handler import CollectionError, PythonHandler, get_handler
1315

@@ -145,3 +147,29 @@ def test_extension_paths(tmp_path: Path, expect_change: bool, extension: str | d
145147
raise ValueError("Normalization must not change extension items type")
146148
else:
147149
assert normalized == extension
150+
151+
152+
def test_rendering_object_source_without_lineno(handler: PythonHandler) -> None:
153+
"""Test rendering objects without a line number."""
154+
code = dedent(
155+
"""
156+
'''Module docstring.'''
157+
158+
class Class:
159+
'''Class docstring.'''
160+
161+
def function(self):
162+
'''Function docstring.'''
163+
164+
attribute = 0
165+
'''Attribute docstring.'''
166+
""",
167+
)
168+
with temporary_visited_module(code) as module:
169+
# TODO: Remove once Griffe does that automatically.
170+
module.lines_collection[module.filepath] = code.splitlines() # type: ignore[index]
171+
172+
module["Class"].lineno = None
173+
module["Class.function"].lineno = None
174+
module["attribute"].lineno = None
175+
assert handler.render(module, {"show_source": True})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy