File tree Expand file tree Collapse file tree
src/sphinx_autodoc_typehints
test-intersphinx-external-role
test-intersphinx-missing-inventory Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1515from docutils .parsers .rst import Directive , directives
1616from docutils .utils import new_document
1717from sphinx .parsers import RSTParser
18+ from sphinx .util import logging
1819
1920from sphinx_autodoc_typehints ._parser import _RstSnippetParser
2021
@@ -112,7 +113,10 @@ def _safe_directive_lookup(
112113 # dispatcher layered on top of it, losing the external+ roles (#753)
113114 directives .directive = _safe_directive_lookup # type: ignore[assignment]
114115 try :
115- _RstSnippetParser ().parse (inputstr , doc )
116+ # Whatever this parse has to say about the docstring, the real parse says again with the
117+ # right line number, so its warnings are only duplicates
118+ with logging .suppress_logging ():
119+ _RstSnippetParser ().parse (inputstr , doc )
116120 finally :
117121 directives .directive = original_lookup
118122 return doc
Original file line number Diff line number Diff line change 66
77from docutils .utils import new_document
88from sphinx .parsers import RSTParser
9- from sphinx .util .docutils import sphinx_domains
109
1110if TYPE_CHECKING :
1211 import optparse
@@ -23,9 +22,9 @@ def decorate(_content: StringList) -> None: # ty: ignore[invalid-method-overrid
2322
2423
2524def parse (inputstr : str , settings : Values | optparse .Values ) -> nodes .document :
26- """Parse inputstr and return a docutils document."""
25+ """Parse inputstr and return a docutils document. Callers must already be inside ``sphinx_domains``. """
2726 doc = new_document ("" , settings = settings ) # ty: ignore[invalid-argument-type]
28- with sphinx_domains ( settings . env ):
29- parser = _RstSnippetParser ( )
30- parser .parse (inputstr , doc )
27+ # Entering sphinx_domains again shadows the intersphinx dispatcher layered on top of it,
28+ # losing the external+ roles the read phase resolves (#753 )
29+ _RstSnippetParser () .parse (inputstr , doc )
3130 return doc
Original file line number Diff line number Diff line change 33import pathlib
44import sys
55import zlib
6+ from typing import TYPE_CHECKING , Any
67
78sys .path .insert (0 , str (pathlib .Path (__file__ ).parent ))
89
10+ if TYPE_CHECKING :
11+ from sphinx .config import Config
12+
913master_doc = "index"
1014
1115extensions = [
2327 b"# The remainder of this file is compressed using zlib.\n " + zlib .compress (b"index std:doc -1 index.html Demo\n " )
2428)
2529intersphinx_mapping = {"demo" : ("https://example.org/demo/" , str (_INVENTORY ))}
30+
31+
32+ def typehints_formatter (annotation : Any , config : Config ) -> str | None : # ruff:ignore[unused-function-argument]
33+ """Render one annotation as an intersphinx role, which the type role parses on its own."""
34+ return ":external+demo:doc:`the demo docs <index>`" if annotation is bool else None
Original file line number Diff line number Diff line change 11from __future__ import annotations
22
33
4- def probe (x : int ) -> int :
4+ def probe (x : int , flag : bool ) -> int :
55 """
66 Summarize.
77
88 :param x: see :external+demo:doc:`the demo docs <index>`.
9+ :param flag: a flag whose type renders as a role.
910 """
10- return x
11+ return x if flag else - x
Original file line number Diff line number Diff line change 1+ from __future__ import annotations
2+
3+ import pathlib
4+ import sys
5+
6+ sys .path .insert (0 , str (pathlib .Path (__file__ ).parent ))
7+
8+ master_doc = "index"
9+
10+ extensions = [
11+ "sphinx.ext.autodoc" ,
12+ "sphinx.ext.intersphinx" ,
13+ "sphinx_autodoc_typehints" ,
14+ ]
15+ intersphinx_mapping : dict [str , tuple [str , str ]] = {}
Original file line number Diff line number Diff line change 1+ from __future__ import annotations
2+
3+
4+ def probe (x : int ) -> int :
5+ """
6+ Summarize.
7+
8+ :param x: see :external+nope:doc:`the missing docs <index>`.
9+ """
10+ return x
Original file line number Diff line number Diff line change 1+ Demo
2+ ====
3+
4+ .. autofunction :: demo_missing.probe
Original file line number Diff line number Diff line change @@ -74,7 +74,15 @@ def run(self) -> list:
7474def test_intersphinx_role_resolves_during_snippet_parse (
7575 app : SphinxTestApp , status : StringIO , warning : StringIO
7676) -> None :
77- """The snippet parse must not shadow the intersphinx role dispatcher (#753)."""
77+ """Neither the snippet parse nor the type role may shadow the intersphinx dispatcher (#753)."""
7878 app .build ()
7979 assert "build succeeded" in status .getvalue ()
80- assert "unknown role name" not in warning .getvalue ()
80+ assert not warning .getvalue ()
81+
82+
83+ @pytest .mark .sphinx ("text" , testroot = "intersphinx-missing-inventory" )
84+ def test_snippet_parse_stays_quiet (app : SphinxTestApp , status : StringIO , warning : StringIO ) -> None :
85+ """A reference the role cannot resolve is reported by the real parse alone (#753)."""
86+ app .build ()
87+ assert "build succeeded" in status .getvalue ()
88+ assert warning .getvalue ().count ("inventory for external cross-reference not found" ) == 1
You can’t perform that action at this time.
0 commit comments