11"""Sphinx extension implementation."""
22
3+ from __future__ import annotations
4+
35from dataclasses import dataclass
46from functools import wraps
57from pathlib import Path
68from traceback import print_exc
7- from typing import Dict , List , Optional , Set
89
910from sphinx .ext .intersphinx import InventoryAdapter
1011from sphinx .util import import_object
1112
12- from ..parse import Name
13- from ..warn import logger , warn_type
13+ from sphinx_codeautolink .parse import Name
14+ from sphinx_codeautolink .warn import logger , warn_type
15+
1416from .backref import CodeExample , CodeRefsVisitor
1517from .block import CodeBlockAnalyser , SourceTransform , link_html
1618from .cache import DataCache
@@ -27,7 +29,7 @@ class DocumentedObject:
2729 return_type : str = None
2830
2931
30- def print_exceptions (append_source : bool = False ):
32+ def print_exceptions (* , append_source : bool = False ):
3133 """
3234 Print the traceback of uncaught and unexpected exceptions.
3335
@@ -62,27 +64,27 @@ def wrapper(*args, **kwargs):
6264class SphinxCodeAutoLink :
6365 """Provide functionality and manage state between events."""
6466
65- def __init__ (self ):
67+ def __init__ (self ) -> None :
6668 # Configuration
6769 self .do_nothing = False
68- self .global_preface : List [str ] = []
70+ self .global_preface : list [str ] = []
6971 self .custom_blocks = None
7072 self .concat_default = None
7173 self .search_css_classes = None
72- self .inventory_map : Dict [str , str ] = {}
74+ self .inventory_map : dict [str , str ] = {}
7375 self .warn_missing_inventory = None
7476 self .warn_failed_resolve = None
7577
7678 # Populated once
77- self .outdated_docs : Set [str ] = set ()
79+ self .outdated_docs : set [str ] = set ()
7880 self .inventory = {}
79- self .code_refs : Dict [str , List [CodeExample ]] = {}
81+ self .code_refs : dict [str , list [CodeExample ]] = {}
8082
8183 # Changing state
82- self .cache : Optional [ DataCache ] = None
84+ self .cache : DataCache | None = None
8385
8486 @print_exceptions ()
85- def build_inited (self , app ):
87+ def build_inited (self , app ) -> None :
8688 """Handle initial setup."""
8789 if app .builder .name != "html" :
8890 self .do_nothing = True
@@ -112,7 +114,7 @@ def build_inited(self, app):
112114 self .global_preface = preface .split ("\n " )
113115
114116 @print_exceptions ()
115- def autodoc_process_docstring (self , app , what , name , obj , options , lines ):
117+ def autodoc_process_docstring (self , app , what , name , obj , options , lines ) -> None :
116118 """Handle autodoc-process-docstring event."""
117119 if self .do_nothing :
118120 return
@@ -123,7 +125,7 @@ def autodoc_process_docstring(self, app, what, name, obj, options, lines):
123125 lines .append (" :collapse:" )
124126
125127 @print_exceptions (append_source = True )
126- def parse_blocks (self , app , doctree ):
128+ def parse_blocks (self , app , doctree ) -> None :
127129 """Parse code blocks for later link substitution."""
128130 if self .do_nothing :
129131 return
@@ -138,14 +140,14 @@ def parse_blocks(self, app, doctree):
138140 doctree .walkabout (visitor )
139141 self .cache .transforms [visitor .current_document ] = visitor .source_transforms
140142
141- def merge_environments (self , app , env , docnames , other ):
143+ def merge_environments (self , app , env , docnames , other ) -> None :
142144 """Merge transform information."""
143145 if self .do_nothing :
144146 return
145147
146148 env .sphinx_codeautolink_transforms .update (other .sphinx_codeautolink_transforms )
147149
148- def purge_doc_from_environment (self , app , env , docname ):
150+ def purge_doc_from_environment (self , app , env , docname ) -> None :
149151 """Remove transforms from cache."""
150152 if self .cache :
151153 self .cache .transforms .pop (docname , None )
@@ -169,7 +171,7 @@ def make_inventory(app):
169171 return transposed
170172
171173 @print_exceptions ()
172- def create_references (self , app , env ):
174+ def create_references (self , app , env ) -> None :
173175 """Clean source transforms and create code references."""
174176 if self .do_nothing :
175177 return
@@ -184,16 +186,16 @@ def create_references(self, app, env):
184186 transform .example
185187 )
186188 if skipped and self .warn_missing_inventory :
187- tops = sorted (set ( s .split ("." )[0 ] for s in skipped ) )
189+ tops = sorted ({ s .split ("." )[0 ] for s in skipped } )
188190 msg = (
189191 f"Cannot locate modules: { str (tops )[1 :- 1 ]} "
190192 "\n because of missing intersphinx or documentation entries"
191193 )
192194 logger .warning (msg , type = warn_type , subtype = "missing_inventory" )
193195
194196 def filter_and_resolve (
195- self , transforms : List [SourceTransform ], skipped : Set [str ], doc : str
196- ):
197+ self , transforms : list [SourceTransform ], skipped : set [str ], doc : str
198+ ) -> None :
197199 """Try to link name chains to objects."""
198200 for transform in transforms :
199201 filtered = []
@@ -207,7 +209,7 @@ def filter_and_resolve(
207209 path = "." .join (name .import_components ).replace (".()" , "()" )
208210 msg = (
209211 f"Could not resolve { self ._resolve_msg (name )} "
210- f" using path `{ path } `.\n { str ( e ) } "
212+ f" using path `{ path } `.\n { e !s } "
211213 )
212214 logger .warning (
213215 msg ,
@@ -238,7 +240,7 @@ def filter_and_resolve(
238240 transform .names = filtered
239241
240242 @staticmethod
241- def _resolve_msg (name : Name ):
243+ def _resolve_msg (name : Name ) -> str :
242244 if name .lineno == name .end_lineno :
243245 line = f"line { name .lineno } "
244246 else :
@@ -255,9 +257,10 @@ def generate_backref_tables(self, app, doctree, docname):
255257
256258 visitor = CodeRefsVisitor (doctree , code_refs = self .code_refs )
257259 doctree .walk (visitor )
260+ return None
258261
259262 @print_exceptions ()
260- def apply_links (self , app , exception ):
263+ def apply_links (self , app , exception ) -> None :
261264 """Apply links to HTML output and write refs file."""
262265 if self .do_nothing or exception is not None :
263266 return
@@ -277,7 +280,7 @@ def apply_links(self, app, exception):
277280 self .cache .write ()
278281
279282
280- def transpose_inventory (inv : dict , relative_to : str ):
283+ def transpose_inventory (inv : dict , relative_to : str ) -> dict [ str , str ] :
281284 """
282285 Transpose Sphinx inventory from {type: {name: (..., location)}} to {name: location}.
283286
0 commit comments