7
7
import os
8
8
from functools import lru_cache
9
9
from pathlib import Path
10
- from typing import Any , Dict , Iterator , List , Optional
10
+ from typing import Any , Dict , Iterator , List , Optional , cast
11
11
12
12
import sphinx .application
13
13
from docutils import nodes
@@ -71,7 +71,7 @@ def has_not_enough_items_to_show_toc(
71
71
72
72
toctree = TocTree (builder .env ).get_toc_for (docname , builder )
73
73
try :
74
- self_toctree = toctree [0 ][1 ]
74
+ self_toctree = toctree [0 ][1 ] # type: ignore[index]
75
75
except IndexError :
76
76
val = True
77
77
else :
@@ -84,8 +84,8 @@ def get_pygments_style_colors(
84
84
style : Style , * , fallbacks : Dict [str , str ]
85
85
) -> Dict [str , str ]:
86
86
"""Get background/foreground colors for given pygments style."""
87
- background = style .background_color
88
- text_colors = style .style_for_token (Text )
87
+ background = style .background_color # type: ignore[attr-defined]
88
+ text_colors = style .style_for_token (Text ) # type: ignore[attr-defined]
89
89
foreground = text_colors ["color" ]
90
90
91
91
if not background :
@@ -172,9 +172,10 @@ def _add_asset_hashes(static: List[str], add_digest_to: List[str]) -> None:
172
172
"theme-provide assets such as `html_style`."
173
173
)
174
174
175
- if "?digest=" in static [index ].filename : # make this idempotent
175
+ # Make this idempotent
176
+ if "?digest=" in static [index ].filename : # type: ignore[attr-defined]
176
177
continue
177
- static [index ].filename = _asset_hash (asset ) # type: ignore
178
+ static [index ].filename = _asset_hash (asset ) # type: ignore[attr-defined]
178
179
179
180
180
181
def _html_page_context (
@@ -201,9 +202,11 @@ def _html_page_context(
201
202
# Values computed from page-level context.
202
203
context ["furo_navigation_tree" ] = _compute_navigation_tree (context )
203
204
context ["furo_hide_toc" ] = _compute_hide_toc (
204
- context , builder = app .builder , docname = pagename
205
+ context , builder = cast ( StandaloneHTMLBuilder , app .builder ) , docname = pagename
205
206
)
206
207
208
+ assert _KNOWN_STYLES_IN_USE ["light" ]
209
+ assert _KNOWN_STYLES_IN_USE ["dark" ]
207
210
# Inject information about styles
208
211
context ["furo_pygments" ] = {
209
212
"light" : get_pygments_style_colors (
@@ -248,10 +251,10 @@ def _builder_inited(app: sphinx.application.Sphinx) -> None:
248
251
249
252
builder = app .builder
250
253
assert (
251
- builder .highlighter is not None
254
+ builder .highlighter is not None # type: ignore[attr-defined]
252
255
), "there should be a default style known to Sphinx"
253
256
assert (
254
- builder .dark_highlighter is None
257
+ builder .dark_highlighter is None # type: ignore[attr-defined]
255
258
), "this shouldn't be a dark style known to Sphinx"
256
259
update_known_styles_state (app )
257
260
@@ -273,17 +276,25 @@ def update_known_styles_state(app: sphinx.application.Sphinx) -> None:
273
276
274
277
275
278
def _get_light_style (app : sphinx .application .Sphinx ) -> Style :
276
- return app .builder .highlighter .formatter_args ["style" ]
279
+ # fmt: off
280
+ # For https://github.com/psf/black/issues/3869
281
+ return (
282
+ app # type: ignore[no-any-return]
283
+ .builder
284
+ .highlighter # type: ignore[attr-defined]
285
+ .formatter_args ["style" ]
286
+ )
287
+ # fmt: on
277
288
278
289
279
290
def _get_dark_style (app : sphinx .application .Sphinx ) -> Style :
280
291
dark_style = app .config .pygments_dark_style
281
- return PygmentsBridge ("html" , dark_style ).formatter_args ["style" ]
292
+ return cast ( Style , PygmentsBridge ("html" , dark_style ).formatter_args ["style" ])
282
293
283
294
284
- def _get_styles (formatter : HtmlFormatter , * , prefix : str ) -> Iterator [str ]:
295
+ def _get_styles (formatter : HtmlFormatter [ str ] , * , prefix : str ) -> Iterator [str ]:
285
296
"""Get styles out of a formatter, where everything has the correct prefix."""
286
- for line in formatter .get_linenos_style_defs ():
297
+ for line in formatter .get_linenos_style_defs (): # type: ignore[no-untyped-call]
287
298
yield f"{ prefix } { line } "
288
299
yield from formatter .get_background_style_defs (prefix )
289
300
yield from formatter .get_token_style_defs (prefix )
0 commit comments