From 5313c3dfe44236f5f62cebe424498d5783d67783 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 15 Jul 2026 12:25:44 +0000 Subject: [PATCH 1/7] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20topmatter=20config=20?= =?UTF-8?q?merging,=20and=20source=20lines=20in=20included=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two fixes: - `merge_file_level` re-assigned the raw topmatter value after validation, clobbering values set by converting validators - most visibly, a `heading_slug_func` preset name crashed at every heading with "'str' object is not callable", and `enable_extensions` survived as a raw list instead of a set. Validated values are now kept, and fields marked global-only are rejected in topmatter with a `myst.topmatter` warning instead of being silently (mis)applied. - every warning inside an `{include}`d markdown file was attributed one line too low: the include directive passed `startline + 1` to the nested renderer, double-counting its 0-based to 1-based conversion. Additionally `start-after` advanced the line offset by *characters* rather than lines, and a negative `start-line` was passed through unresolved; both now map exactly. Closes #1165, closes #1167 --- myst_parser/config/main.py | 16 +++- myst_parser/mocking.py | 13 ++- tests/test_docutils.py | 96 +++++++++++++++++++ .../fixtures/mock_include_errors.md | 2 +- 4 files changed, 121 insertions(+), 6 deletions(-) diff --git a/myst_parser/config/main.py b/myst_parser/config/main.py index bd19dc7a..1c909cc9 100644 --- a/myst_parser/config/main.py +++ b/myst_parser/config/main.py @@ -585,6 +585,14 @@ def merge_file_level( old_value, field = fields[name] + if field.metadata.get("global_only"): + warning( + MystWarnings.MD_TOPMATTER, + f"'{name}' is only allowed at the global level, ignoring", + ) + continue + + before = getattr(new, name) try: validate_field(new, field, value) except Exception as exc: @@ -592,9 +600,11 @@ def merge_file_level( continue if field.metadata.get("merge_topmatter"): - value = {**old_value, **value} - - setattr(new, name, value) + setattr(new, name, {**old_value, **value}) + elif getattr(new, name) is before: + # a converting validator (e.g. list -> set) assigns the converted + # value itself; do not clobber that with the raw value + setattr(new, name, value) return new diff --git a/myst_parser/mocking.py b/myst_parser/mocking.py index b65888e7..09119ba4 100644 --- a/myst_parser/mocking.py +++ b/myst_parser/mocking.py @@ -429,6 +429,10 @@ def run(self) -> list[nodes.Element]: # get required section of text startline = self.options.get("start-line", None) endline = self.options.get("end-line", None) + if startline is not None and startline < 0: + # resolve to the actual 0-based line, for source-line reporting + # (slicing semantics are unchanged by this) + startline = max(0, len(file_content.splitlines()) + startline) file_content = "\n".join(file_content.splitlines()[startline:endline]) startline = startline or 0 for split_on_type in ["start-after", "end-before"]: @@ -442,7 +446,9 @@ def run(self) -> list[nodes.Element]: f'Directive "{self.name}"; option "{split_on_type}": text not found "{split_on}".', ) if split_on_type == "start-after": - startline += split_index + len(split_on) + # advance by the number of *lines* removed, so that + # source-line reporting stays aligned + startline += file_content.count("\n", 0, split_index + len(split_on)) file_content = file_content[split_index + len(split_on) :] else: file_content = file_content[:split_index] @@ -514,9 +520,12 @@ def run(self) -> list[nodes.Element]: source_dir, path.parent, ) + # ``nested_render_text`` expects the 0-based source line of the + # first line of the text (the final 0-based to 1-based conversion + # happens in ``_render_tokens``), which is exactly ``startline`` self.renderer.nested_render_text( file_content, - startline + 1, + startline, heading_offset=self.options.get("heading-offset", 0), ) finally: diff --git a/tests/test_docutils.py b/tests/test_docutils.py index 890badbc..ace905ff 100644 --- a/tests/test_docutils.py +++ b/tests/test_docutils.py @@ -3,6 +3,7 @@ import io import sys from dataclasses import dataclass, field, fields +from pathlib import Path from textwrap import dedent from typing import Literal @@ -573,6 +574,101 @@ def test_include_literal_line(tmp_path): assert literal_block.line == 1 +def test_include_source_line_attribution(tmp_path): + """Warnings inside an ``{include}``\\ d file report the exact source line. + + Regression: every line was attributed one too low (``startline + 1`` + double-counted with the renderer's 0-based to 1-based conversion), and + ``start-after`` advanced the line offset by *characters*. + """ + + def role_warning_location(main_source: str) -> str: + stream = io.StringIO() + publish_doctree( + main_source, + parser=Parser(), + source_path=str(tmp_path / "main.md"), + settings_overrides={"warning_stream": stream}, + ) + warnings_ = [ + line for line in stream.getvalue().splitlines() if "unknownrole" in line + ] + assert len(warnings_) == 1, stream.getvalue() + # `path:line:` prefix of the docutils warning + path, line, _ = warnings_[0].split(":", 2) + return f"{Path(path).name}:{line}" + + inc = tmp_path / "inc.md" + inc.write_text("line one\n\n{unknownrole}`x`\n") + assert role_warning_location(f"```{{include}} {inc}\n```\n") == "inc.md:3" + + inc2 = tmp_path / "inc2.md" + inc2.write_text("skip1\nskip2\npara\n\n{unknownrole}`y`\n") + assert ( + role_warning_location(f"```{{include}} {inc2}\n:start-line: 2\n```\n") + == "inc2.md:5" + ) + + inc3 = tmp_path / "inc3.md" + inc3.write_text("intro\n\n\n{unknownrole}`z`\n") + assert ( + role_warning_location( + f"```{{include}} {inc3}\n:start-after: \n```\n" + ) + == "inc3.md:4" + ) + + # nested includes: the innermost file's own lines + b = tmp_path / "b.md" + b.write_text("first\n\n{unknownrole}`b`\n") + a = tmp_path / "a.md" + a.write_text(f"a para\n\n```{{include}} {b}\n```\n") + assert role_warning_location(f"```{{include}} {a}\n```\n") == "b.md:3" + + +def test_topmatter_global_only_and_validated_values(): + """Topmatter: global-only fields warn and are ignored; converting + validators' values survive the merge. + + Regression: ``merge_file_level`` re-assigned the raw topmatter value over + the value a converting validator had set, so e.g. a + ``heading_slug_func`` preset name crashed at each heading + (``'str' object is not callable``). + """ + from myst_parser.config.main import MdParserConfig, merge_file_level + + warnings_: list[str] = [] + config = merge_file_level( + MdParserConfig(), + { + "myst": { + "heading_slug_func": "github", # global-only + "enable_extensions": ["colon_fence"], # converting validator + "title_to_header": True, # plain local field + } + }, + lambda _, msg: warnings_.append(msg), + ) + assert warnings_ == [ + "'heading_slug_func' is only allowed at the global level, ignoring" + ] + assert config.heading_slug_func is None + assert config.enable_extensions == {"colon_fence"} + assert isinstance(config.enable_extensions, set) + assert config.title_to_header is True + + # end-to-end: no per-heading crash, a single topmatter warning + stream = io.StringIO() + publish_doctree( + source="---\nmyst:\n heading_slug_func: github\n---\n\n# Hello World\n", + parser=Parser(), + settings_overrides={"warning_stream": stream}, + ) + output = stream.getvalue() + assert "is not callable" not in output + assert "[myst.topmatter]" in output + + def test_text_node_line_stamping(): """Text nodes carry their enclosing block's line, not a stale one. diff --git a/tests/test_renderers/fixtures/mock_include_errors.md b/tests/test_renderers/fixtures/mock_include_errors.md index 6e443cb6..a8d7171e 100644 --- a/tests/test_renderers/fixtures/mock_include_errors.md +++ b/tests/test_renderers/fixtures/mock_include_errors.md @@ -19,5 +19,5 @@ Error in include file: ```{include} bad.md ``` . -tmpdir/bad.md:2: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +tmpdir/bad.md:1: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] . From fee644b2a400774b4a999b0cc73c9050efacfd89 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 15 Jul 2026 12:44:41 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=94=A7=20MAINTAIN:=20note=20the=20con?= =?UTF-8?q?verter=20contract=20in=20merge=5Ffile=5Flevel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- myst_parser/config/main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/myst_parser/config/main.py b/myst_parser/config/main.py index 1c909cc9..a915a3da 100644 --- a/myst_parser/config/main.py +++ b/myst_parser/config/main.py @@ -604,6 +604,8 @@ def merge_file_level( elif getattr(new, name) is before: # a converting validator (e.g. list -> set) assigns the converted # value itself; do not clobber that with the raw value + # (note for validator authors: converters must assign a *new* + # object, never normalise the existing value in place) setattr(new, name, value) return new From b879d032d20b0c3a1c9a8d0554e029c062dfad50 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Mon, 9 Nov 2026 20:31:06 +0100 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=94=A7=20MAINTAIN:=20drop=20the=20top?= =?UTF-8?q?matter=20half=20(#1167=20now=20lands=20via=20#1197)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Revert the `merge_file_level` change and its test: #1167 (global-only fields and converting validators in topmatter) is fixed by #1197, so this PR keeps only the `{include}` line attribution fix (#1165, #1216). --- myst_parser/config/main.py | 18 +++------------- tests/test_docutils.py | 43 -------------------------------------- 2 files changed, 3 insertions(+), 58 deletions(-) diff --git a/myst_parser/config/main.py b/myst_parser/config/main.py index a915a3da..bd19dc7a 100644 --- a/myst_parser/config/main.py +++ b/myst_parser/config/main.py @@ -585,14 +585,6 @@ def merge_file_level( old_value, field = fields[name] - if field.metadata.get("global_only"): - warning( - MystWarnings.MD_TOPMATTER, - f"'{name}' is only allowed at the global level, ignoring", - ) - continue - - before = getattr(new, name) try: validate_field(new, field, value) except Exception as exc: @@ -600,13 +592,9 @@ def merge_file_level( continue if field.metadata.get("merge_topmatter"): - setattr(new, name, {**old_value, **value}) - elif getattr(new, name) is before: - # a converting validator (e.g. list -> set) assigns the converted - # value itself; do not clobber that with the raw value - # (note for validator authors: converters must assign a *new* - # object, never normalise the existing value in place) - setattr(new, name, value) + value = {**old_value, **value} + + setattr(new, name, value) return new diff --git a/tests/test_docutils.py b/tests/test_docutils.py index ef3e08d9..9a18c138 100644 --- a/tests/test_docutils.py +++ b/tests/test_docutils.py @@ -628,49 +628,6 @@ def role_warning_location(main_source: str) -> str: assert role_warning_location(f"```{{include}} {a}\n```\n") == "b.md:3" -def test_topmatter_global_only_and_validated_values(): - """Topmatter: global-only fields warn and are ignored; converting - validators' values survive the merge. - - Regression: ``merge_file_level`` re-assigned the raw topmatter value over - the value a converting validator had set, so e.g. a - ``heading_slug_func`` preset name crashed at each heading - (``'str' object is not callable``). - """ - from myst_parser.config.main import MdParserConfig, merge_file_level - - warnings_: list[str] = [] - config = merge_file_level( - MdParserConfig(), - { - "myst": { - "heading_slug_func": "github", # global-only - "enable_extensions": ["colon_fence"], # converting validator - "title_to_header": True, # plain local field - } - }, - lambda _, msg: warnings_.append(msg), - ) - assert warnings_ == [ - "'heading_slug_func' is only allowed at the global level, ignoring" - ] - assert config.heading_slug_func is None - assert config.enable_extensions == {"colon_fence"} - assert isinstance(config.enable_extensions, set) - assert config.title_to_header is True - - # end-to-end: no per-heading crash, a single topmatter warning - stream = io.StringIO() - publish_doctree( - source="---\nmyst:\n heading_slug_func: github\n---\n\n# Hello World\n", - parser=Parser(), - settings_overrides={"warning_stream": stream}, - ) - output = stream.getvalue() - assert "is not callable" not in output - assert "[myst.topmatter]" in output - - def test_text_node_line_stamping(): """Text nodes carry their enclosing block's line, not a stale one. From f5e1b94cb2f671a57c2ee2de72c244d9a8b76263 Mon Sep 17 00:00:00 2001 From: dchaudhari7177 Date: Sat, 8 Aug 2026 16:39:06 +0530 Subject: [PATCH 4/7] Fix line attribution inside included markdown being off by one MockIncludeDirective.run passed `startline + 1` to nested_render_text, but that lineno is added to the 0-based token maps *before* _render_tokens applies its own 0-based to 1-based conversion. The offset of the first included line is `startline`, already 0-based, so every warning raised inside an included file was reported one line too low. The existing "Error in include file" fixture encoded the off-by-one: bad.md holds the role on line 1 and the expectation read `bad.md:2`. Fixes #1165 (cherry picked from commit 8cee240db7079bc35bad21de87b193796dc0fa9e) --- .../fixtures/mock_include_errors.md | 43 +++++++++++++++++++ .../test_renderers/test_include_directive.py | 9 ++++ 2 files changed, 52 insertions(+) diff --git a/tests/test_renderers/fixtures/mock_include_errors.md b/tests/test_renderers/fixtures/mock_include_errors.md index a8d7171e..13820b8e 100644 --- a/tests/test_renderers/fixtures/mock_include_errors.md +++ b/tests/test_renderers/fixtures/mock_include_errors.md @@ -21,3 +21,46 @@ Error in include file: . tmpdir/bad.md:1: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] . + +Error line in include file: +. +```{include} bad_line3.md +``` +. +tmpdir/bad_line3.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file, after content in the parent: +. +para + +```{include} bad_line3.md +``` +. +tmpdir/bad_line3.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file with start-line: +. +```{include} bad_skipped.md +:start-line: 2 +``` +. +tmpdir/bad_skipped.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in include file with front matter: +. +```{include} bad_frontmatter.md +``` +. +tmpdir/bad_frontmatter.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error line in nested include file: +. +```{include} bad_outer.md +``` +. +tmpdir/bad_inner.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. diff --git a/tests/test_renderers/test_include_directive.py b/tests/test_renderers/test_include_directive.py index 9f03d633..d8501b04 100644 --- a/tests/test_renderers/test_include_directive.py +++ b/tests/test_renderers/test_include_directive.py @@ -41,6 +41,15 @@ def test_errors(file_params, tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) tmp_path.joinpath("bad.md").write_text("{a}`b`") + # The role sits on line 3, so a warning reported against any other line is + # the include's own line attribution being off. + tmp_path.joinpath("bad_line3.md").write_text("line one\n\n{a}`b`\n") + tmp_path.joinpath("bad_skipped.md").write_text("skipped\nskipped2\n\n{a}`b`\n") + tmp_path.joinpath("bad_frontmatter.md").write_text("---\nx: 1\n---\n\n{a}`b`\n") + tmp_path.joinpath("bad_inner.md").write_text("inner one\n\n{a}`b`\n") + tmp_path.joinpath("bad_outer.md").write_text( + "outer line\n\n```{include} bad_inner.md\n```\n" + ) report_stream = StringIO() publish_doctree( From 291194fa09a1c81bfe3123fcb2aa7e1fb355838f Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Mon, 9 Nov 2026 20:31:46 +0100 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=A7=AA=20TEST:=20add=20#1217's=20`:st?= =?UTF-8?q?art-after:`=20include=20cases,=20expecting=20line=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The two cases from #1217 (`:start-after:` alone, and with `:start-line:`), with the expectation corrected from `bad_after.md:5` to `:4`: the `:5` encoded the separate #1165 off-by-one, and the paragraph holding the role starts on line 4 of the file. Co-authored-by: vishalkaay --- .../fixtures/mock_include_errors.md | 19 +++++++++++++++++++ .../test_renderers/test_include_directive.py | 3 +++ 2 files changed, 22 insertions(+) diff --git a/tests/test_renderers/fixtures/mock_include_errors.md b/tests/test_renderers/fixtures/mock_include_errors.md index 13820b8e..77e4317d 100644 --- a/tests/test_renderers/fixtures/mock_include_errors.md +++ b/tests/test_renderers/fixtures/mock_include_errors.md @@ -64,3 +64,22 @@ Error line in nested include file: . tmpdir/bad_inner.md:3: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] . + +Error in include file with start-after: +. +```{include} bad_after.md +:start-after: START-HERE +``` +. +tmpdir/bad_after.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with start-line and start-after: +. +```{include} bad_after.md +:start-line: 1 +:start-after: START-HERE +``` +. +tmpdir/bad_after.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. diff --git a/tests/test_renderers/test_include_directive.py b/tests/test_renderers/test_include_directive.py index d8501b04..cc84c62f 100644 --- a/tests/test_renderers/test_include_directive.py +++ b/tests/test_renderers/test_include_directive.py @@ -50,6 +50,9 @@ def test_errors(file_params, tmp_path, monkeypatch): tmp_path.joinpath("bad_outer.md").write_text( "outer line\n\n```{include} bad_inner.md\n```\n" ) + tmp_path.joinpath("bad_after.md").write_text( + "line one\nline two\nSTART-HERE\nline four\n{a}`b`" + ) report_stream = StringIO() publish_doctree( From a99583b53b187b65851793c4b4f56ead687aefdb Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Mon, 9 Nov 2026 20:32:22 +0100 Subject: [PATCH 6/7] =?UTF-8?q?=F0=9F=A7=AA=20TEST:=20pin=20`{include}`=20?= =?UTF-8?q?lines=20for=20the=20remaining=20option=20edge=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixture cases for each line-attribution branch in `MockIncludeDirective`: a multi-line `start-after` marker (YAML option form), negative `start-line` (`-2`, and `-100` beyond the file's length), a CRLF file with `start-after`, `start-after` with `end-before`, a marker that ends the file (no content, no warning), and a nested include using `start-line` followed by a warning in the outer file. Each fix hunk is load-bearing: without the negative `start-line` resolution 2 cases fail, without the newline count for `start-after` 5 fail, and with `startline + 1` restored 14 fail. --- .../fixtures/mock_include_errors.md | 65 +++++++++++++++++++ .../test_renderers/test_include_directive.py | 13 ++++ 2 files changed, 78 insertions(+) diff --git a/tests/test_renderers/fixtures/mock_include_errors.md b/tests/test_renderers/fixtures/mock_include_errors.md index 77e4317d..40d0d6db 100644 --- a/tests/test_renderers/fixtures/mock_include_errors.md +++ b/tests/test_renderers/fixtures/mock_include_errors.md @@ -83,3 +83,68 @@ Error in include file with start-line and start-after: . tmpdir/bad_after.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] . + +Error in include file with a multi-line start-after marker: +. +```{include} bad_marker2.md +--- +start-after: "\n" +--- +``` +. +tmpdir/bad_marker2.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with a negative start-line: +. +```{include} bad_negative.md +:start-line: -2 +``` +. +tmpdir/bad_negative.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with a negative start-line beyond its length: +. +```{include} bad_negative.md +:start-line: -100 +``` +. +tmpdir/bad_negative.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in CRLF include file with start-after: +. +```{include} bad_crlf.md +:start-after: MARK +``` +. +tmpdir/bad_crlf.md:4: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +Error in include file with start-after and end-before: +. +```{include} bad_between.md +:start-after: START +:end-before: END +``` +. +tmpdir/bad_between.md:5: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. + +No error when the start-after marker ends the file: +. +```{include} bad_between.md +:start-after: {c}`d` +``` +. +. + +Errors in a nested include file with start-line, and in the outer file after it: +. +```{include} bad_outer2.md +``` +. +tmpdir/bad_inner2.md:4: (WARNING/2) Unknown interpreted text role "c". [myst.role_unknown] +tmpdir/bad_outer2.md:7: (WARNING/2) Unknown interpreted text role "a". [myst.role_unknown] +. diff --git a/tests/test_renderers/test_include_directive.py b/tests/test_renderers/test_include_directive.py index cc84c62f..96ede2ba 100644 --- a/tests/test_renderers/test_include_directive.py +++ b/tests/test_renderers/test_include_directive.py @@ -53,6 +53,19 @@ def test_errors(file_params, tmp_path, monkeypatch): tmp_path.joinpath("bad_after.md").write_text( "line one\nline two\nSTART-HERE\nline four\n{a}`b`" ) + tmp_path.joinpath("bad_marker2.md").write_text( + "x\n\n\n\n{a}`b`\n" + ) + tmp_path.joinpath("bad_negative.md").write_text("a\nb\nc\n\n{a}`b`\n") + # bytes, so the CRLF line endings are kept on every platform + tmp_path.joinpath("bad_crlf.md").write_bytes(b"a\r\nMARK\r\n\r\n{a}`b`\r\n") + tmp_path.joinpath("bad_between.md").write_text( + "a\nSTART\nmid\n\n{a}`b`\nEND\n{c}`d`\n" + ) + tmp_path.joinpath("bad_inner2.md").write_text("skip\ninner\n\n{c}`d`\n") + tmp_path.joinpath("bad_outer2.md").write_text( + "outer\n\n```{include} bad_inner2.md\n:start-line: 1\n```\n\n{a}`b`\n" + ) report_stream = StringIO() publish_doctree( From f6c95d57343895835e3802ecdb6322340f658901 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Mon, 9 Nov 2026 20:32:52 +0100 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=A7=AA=20TEST:=20drop=20the=20Python?= =?UTF-8?q?=20include-line=20test=20in=20favour=20of=20the=20fixtures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `test_include_source_line_attribution` split the warning on `":"`, which breaks on Windows paths (`C:\...`), and its first failing assert hid the rest. Every case it checked (plain, `start-line`, `start-after`, nested) is now a data-driven case in `mock_include_errors.md`. --- tests/test_docutils.py | 53 ------------------------------------------ 1 file changed, 53 deletions(-) diff --git a/tests/test_docutils.py b/tests/test_docutils.py index 9a18c138..e19a7640 100644 --- a/tests/test_docutils.py +++ b/tests/test_docutils.py @@ -4,7 +4,6 @@ import re import sys from dataclasses import dataclass, field, fields -from pathlib import Path from textwrap import dedent from typing import Literal @@ -576,58 +575,6 @@ def test_include_literal_line(tmp_path): assert literal_block.line == 1 -def test_include_source_line_attribution(tmp_path): - """Warnings inside an ``{include}``\\ d file report the exact source line. - - Regression: every line was attributed one too low (``startline + 1`` - double-counted with the renderer's 0-based to 1-based conversion), and - ``start-after`` advanced the line offset by *characters*. - """ - - def role_warning_location(main_source: str) -> str: - stream = io.StringIO() - publish_doctree( - main_source, - parser=Parser(), - source_path=str(tmp_path / "main.md"), - settings_overrides={"warning_stream": stream}, - ) - warnings_ = [ - line for line in stream.getvalue().splitlines() if "unknownrole" in line - ] - assert len(warnings_) == 1, stream.getvalue() - # `path:line:` prefix of the docutils warning - path, line, _ = warnings_[0].split(":", 2) - return f"{Path(path).name}:{line}" - - inc = tmp_path / "inc.md" - inc.write_text("line one\n\n{unknownrole}`x`\n") - assert role_warning_location(f"```{{include}} {inc}\n```\n") == "inc.md:3" - - inc2 = tmp_path / "inc2.md" - inc2.write_text("skip1\nskip2\npara\n\n{unknownrole}`y`\n") - assert ( - role_warning_location(f"```{{include}} {inc2}\n:start-line: 2\n```\n") - == "inc2.md:5" - ) - - inc3 = tmp_path / "inc3.md" - inc3.write_text("intro\n\n\n{unknownrole}`z`\n") - assert ( - role_warning_location( - f"```{{include}} {inc3}\n:start-after: \n```\n" - ) - == "inc3.md:4" - ) - - # nested includes: the innermost file's own lines - b = tmp_path / "b.md" - b.write_text("first\n\n{unknownrole}`b`\n") - a = tmp_path / "a.md" - a.write_text(f"a para\n\n```{{include}} {b}\n```\n") - assert role_warning_location(f"```{{include}} {a}\n```\n") == "b.md:3" - - def test_text_node_line_stamping(): """Text nodes carry their enclosing block's line, not a stale one.