From 64df00b9b757e9642d65cf425d32f5a2e0d75f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Wed, 22 May 2024 17:21:11 +0200 Subject: [PATCH 1/2] fix: Don't crash when rendering the source of an object whose lineno is none Issue-163: https://github.com/mkdocstrings/python/issues/163 --- .../templates/material/_base/class.html.jinja | 4 +-- .../material/_base/function.html.jinja | 2 +- tests/test_handler.py | 28 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja index bb32a91c..67d00e34 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja @@ -157,7 +157,7 @@ Context: {{ init.relative_filepath }} {%- endif -%} - {{ init.source|highlight(language="python", linestart=init.lineno, linenums=True) }} + {{ init.source|highlight(language="python", linestart=init.lineno or 0, linenums=True) }} {% endwith %} {% endif %} @@ -170,7 +170,7 @@ Context: {{ class.relative_filepath }} {%- endif -%} - {{ class.source|highlight(language="python", linestart=class.lineno, linenums=True) }} + {{ class.source|highlight(language="python", linestart=class.lineno or 0, linenums=True) }} {% endif %} {% endif %} diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/function.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/function.html.jinja index 4f36059e..01f3e74a 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/function.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/function.html.jinja @@ -133,7 +133,7 @@ Context: {{ function.relative_filepath }} {%- endif -%} - {{ function.source|highlight(language="python", linestart=function.lineno, linenums=True) }} + {{ function.source|highlight(language="python", linestart=function.lineno or 0, linenums=True) }} {% endif %} {% endblock source %} diff --git a/tests/test_handler.py b/tests/test_handler.py index 9f833d12..4ec7ee28 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -4,10 +4,12 @@ import os from glob import glob +from textwrap import dedent from typing import TYPE_CHECKING import pytest from griffe.docstrings.dataclasses import DocstringSectionExamples, DocstringSectionKind +from griffe.tests import temporary_visited_module from mkdocstrings_handlers.python.handler import CollectionError, PythonHandler, get_handler @@ -145,3 +147,29 @@ def test_extension_paths(tmp_path: Path, expect_change: bool, extension: str | d raise ValueError("Normalization must not change extension items type") else: assert normalized == extension + + +def test_rendering_object_source_without_lineno(handler: PythonHandler) -> None: + """Test rendering objects without a line number.""" + code = dedent( + """ + '''Module docstring.''' + + class Class: + '''Class docstring.''' + + def function(self): + '''Function docstring.''' + + attribute = 0 + '''Attribute docstring.''' + """, + ) + with temporary_visited_module(code) as module: + # TODO: Remove once Griffe does that automatically. + module.lines_collection[module.filepath] = code.splitlines() # type: ignore[index] + + module["Class"].lineno = None + module["Class.function"].lineno = None + module["attribute"].lineno = None + assert handler.render(module, {"show_source": True}) From 9c7e30448a2dcd9678eaf934f4ae5b19889cf9fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Wed, 22 May 2024 17:22:19 +0200 Subject: [PATCH 2/2] chore: Prepare release 1.10.3 --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 863b1c8f..c2bc4c72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [1.10.3](https://github.com/mkdocstrings/python/releases/tag/1.10.3) - 2024-05-22 + +[Compare with 1.10.2](https://github.com/mkdocstrings/python/compare/1.10.2...1.10.3) + +### Bug Fixes + +- Don't crash when rendering the source of an object whose lineno is none ([64df00b](https://github.com/mkdocstrings/python/commit/64df00b9b757e9642d65cf425d32f5a2e0d75f38) by Timothée Mazzucotelli). [Issue-163](https://github.com/mkdocstrings/python/issues/163) + ## [1.10.2](https://github.com/mkdocstrings/python/releases/tag/1.10.2) - 2024-05-16 [Compare with 1.10.1](https://github.com/mkdocstrings/python/compare/1.10.1...1.10.2)