Skip to content

Commit d59b158

Browse files
👌 Make ExtensionMetadata type public and use it in internal extensions (sphinx-doc#12153)
This type alias has now been fully documented for public consumption. This will be beneficial to the sphinx ecosystem, to aide/encourage extension developers to provide the correct metadata. Co-authored-by: Bénédikt Tran <[email protected]>
1 parent ace9d97 commit d59b158

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+200
-122
lines changed

‎CHANGES.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ Deprecated
2222
Features added
2323
--------------
2424

25+
* Add public type alias :class:`sphinx.util.typing.ExtensionMetadata`.
26+
This can be used by extension developers
27+
to annotate the return type of their ``setup`` function.
28+
Patch by Chris Sewell.
29+
2530
* #12133: Allow ``external`` roles to reference object types
2631
(rather than role names). Patch by Chris Sewell.
2732

‎doc/development/tutorials/examples/helloworld.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from docutils import nodes
22
from docutils.parsers.rst import Directive
33

4+
from sphinx.application import Sphinx
5+
from sphinx.util.typing import ExtensionMetadata
6+
47

58
class HelloWorld(Directive):
69
def run(self):
710
paragraph_node = nodes.paragraph(text='Hello World!')
811
return [paragraph_node]
912

1013

11-
def setup(app):
14+
def setup(app: Sphinx) -> ExtensionMetadata:
1215
app.add_directive('helloworld', HelloWorld)
1316

1417
return {

‎doc/development/tutorials/examples/recipe.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
from docutils.parsers.rst import directives
44

55
from sphinx import addnodes
6+
from sphinx.application import Sphinx
67
from sphinx.directives import ObjectDescription
78
from sphinx.domains import Domain, Index
89
from sphinx.roles import XRefRole
910
from sphinx.util.nodes import make_refnode
11+
from sphinx.util.typing import ExtensionMetadata
1012

1113

1214
class RecipeDirective(ObjectDescription):
@@ -153,7 +155,7 @@ def add_recipe(self, signature, ingredients):
153155
self.data['recipes'].append((name, signature, 'Recipe', self.env.docname, anchor, 0))
154156

155157

156-
def setup(app):
158+
def setup(app: Sphinx) -> ExtensionMetadata:
157159
app.add_domain(RecipeDomain)
158160

159161
return {

‎doc/development/tutorials/examples/todo.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from docutils import nodes
22
from docutils.parsers.rst import Directive
33

4+
from sphinx.application import Sphinx
45
from sphinx.locale import _
56
from sphinx.util.docutils import SphinxDirective
7+
from sphinx.util.typing import ExtensionMetadata
68

79

810
class todo(nodes.Admonition, nodes.Element):
@@ -111,7 +113,7 @@ def process_todo_nodes(app, doctree, fromdocname):
111113
node.replace_self(content)
112114

113115

114-
def setup(app):
116+
def setup(app: Sphinx) -> ExtensionMetadata:
115117
app.add_config_value('todo_include_todos', False, 'html')
116118

117119
app.add_node(todolist)

‎doc/extdev/utils.rst‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ Utility components
3535

3636
.. autoclass:: sphinx.events.EventManager
3737
:members:
38+
39+
Utility types
40+
-------------
41+
42+
.. autoclass:: sphinx.util.typing.ExtensionMetadata
43+
:members:

‎sphinx/addnodes.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from docutils.nodes import Element
1313

1414
from sphinx.application import Sphinx
15+
from sphinx.util.typing import ExtensionMetadata
1516

1617
# deprecated name -> (object to return, canonical path or empty string)
1718
_DEPRECATED_OBJECTS = {
@@ -573,7 +574,7 @@ class manpage(nodes.Inline, nodes.FixedTextElement):
573574
"""Node for references to manpages."""
574575

575576

576-
def setup(app: Sphinx) -> dict[str, Any]:
577+
def setup(app: Sphinx) -> ExtensionMetadata:
577578
app.add_node(toctree)
578579

579580
app.add_node(desc)

‎sphinx/builders/changes.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
if TYPE_CHECKING:
2020
from sphinx.application import Sphinx
21+
from sphinx.util.typing import ExtensionMetadata
2122

2223
logger = logging.getLogger(__name__)
2324

@@ -155,7 +156,7 @@ def finish(self) -> None:
155156
pass
156157

157158

158-
def setup(app: Sphinx) -> dict[str, Any]:
159+
def setup(app: Sphinx) -> ExtensionMetadata:
159160
app.add_builder(ChangesBuilder)
160161

161162
return {

‎sphinx/builders/dirhtml.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
from __future__ import annotations
44

55
from os import path
6-
from typing import TYPE_CHECKING, Any
6+
from typing import TYPE_CHECKING
77

88
from sphinx.builders.html import StandaloneHTMLBuilder
99
from sphinx.util import logging
1010
from sphinx.util.osutil import SEP, os_path
1111

1212
if TYPE_CHECKING:
1313
from sphinx.application import Sphinx
14+
from sphinx.util.typing import ExtensionMetadata
1415

1516
logger = logging.getLogger(__name__)
1617

@@ -42,7 +43,7 @@ def get_outfilename(self, pagename: str) -> str:
4243
return outfilename
4344

4445

45-
def setup(app: Sphinx) -> dict[str, Any]:
46+
def setup(app: Sphinx) -> ExtensionMetadata:
4647
app.setup_extension('sphinx.builders.html')
4748

4849
app.add_builder(DirectoryHTMLBuilder)

‎sphinx/builders/dummy.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Any
5+
from typing import TYPE_CHECKING
66

77
from sphinx.builders import Builder
88
from sphinx.locale import __
@@ -11,6 +11,7 @@
1111
from docutils.nodes import Node
1212

1313
from sphinx.application import Sphinx
14+
from sphinx.util.typing import ExtensionMetadata
1415

1516

1617
class DummyBuilder(Builder):
@@ -38,7 +39,7 @@ def finish(self) -> None:
3839
pass
3940

4041

41-
def setup(app: Sphinx) -> dict[str, Any]:
42+
def setup(app: Sphinx) -> ExtensionMetadata:
4243
app.add_builder(DummyBuilder)
4344

4445
return {

‎sphinx/builders/epub3.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
if TYPE_CHECKING:
2424
from sphinx.application import Sphinx
25+
from sphinx.util.typing import ExtensionMetadata
2526

2627
logger = logging.getLogger(__name__)
2728

@@ -257,7 +258,7 @@ def convert_epub_css_files(app: Sphinx, config: Config) -> None:
257258
config.epub_css_files = epub_css_files # type: ignore[attr-defined]
258259

259260

260-
def setup(app: Sphinx) -> dict[str, Any]:
261+
def setup(app: Sphinx) -> ExtensionMetadata:
261262
app.add_builder(Epub3Builder)
262263

263264
# config values

0 commit comments

Comments
 (0)