From 85400a0430f4f5fb93936ceca2124c0e7968cca6 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Sat, 13 Jan 2024 02:32:10 +0000 Subject: [PATCH] Improve typing for ``_ConfigRebuild`` --- sphinx/builders/_epub_base.py | 2 +- sphinx/builders/html/__init__.py | 7 ++++--- sphinx/config.py | 12 +++++++++--- sphinx/environment/__init__.py | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/sphinx/builders/_epub_base.py b/sphinx/builders/_epub_base.py index e47780023..aa1b9ec95 100644 --- a/sphinx/builders/_epub_base.py +++ b/sphinx/builders/_epub_base.py @@ -168,7 +168,7 @@ class EpubBuilder(StandaloneHTMLBuilder): self.refnodes: list[dict[str, Any]] = [] def create_build_info(self) -> BuildInfo: - return BuildInfo(self.config, self.tags, ['html', 'epub']) + return BuildInfo(self.config, self.tags, frozenset({'html', 'epub'})) def get_theme_config(self) -> tuple[str, dict]: return self.config.epub_theme, self.config.epub_theme_options diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 0f3cccb34..0a6a0a8f0 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -50,11 +50,12 @@ from sphinx.writers.html import HTMLWriter from sphinx.writers.html5 import HTML5Translator if TYPE_CHECKING: - from collections.abc import Iterable, Iterator, Sequence + from collections.abc import Iterable, Iterator, Set from docutils.nodes import Node from sphinx.application import Sphinx + from sphinx.config import _ConfigRebuild from sphinx.environment import BuildEnvironment from sphinx.util.tags import Tags @@ -130,7 +131,7 @@ class BuildInfo: self, config: Config | None = None, tags: Tags | None = None, - config_categories: Sequence[str] = (), + config_categories: Set[_ConfigRebuild] = frozenset(), ) -> None: self.config_hash = '' self.tags_hash = '' @@ -239,7 +240,7 @@ class StandaloneHTMLBuilder(Builder): self.use_index = self.get_builder_config('use_index', 'html') def create_build_info(self) -> BuildInfo: - return BuildInfo(self.config, self.tags, ['html']) + return BuildInfo(self.config, self.tags, frozenset({'html'})) def _get_translations_js(self) -> str: candidates = [path.join(dir, self.config.language, diff --git a/sphinx/config.py b/sphinx/config.py index 6a09d6ee8..85524ab06 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -24,7 +24,7 @@ else: if TYPE_CHECKING: import os - from collections.abc import Collection, Iterator, Sequence + from collections.abc import Collection, Iterator, Sequence, Set from sphinx.application import Sphinx from sphinx.environment import BuildEnvironment @@ -33,7 +33,13 @@ if TYPE_CHECKING: logger = logging.getLogger(__name__) -_ConfigRebuild = Literal['', 'env', 'epub', 'gettext', 'html'] +_ConfigRebuild = Literal[ + '', 'env', 'epub', 'gettext', 'html', + # sphinxcontrib-applehelp + 'applehelp', + # sphinxcontrib-devhelp + 'devhelp', +] CONFIG_FILENAME = 'conf.py' UNSERIALIZABLE_TYPES = (type, types.ModuleType, types.FunctionType) @@ -423,7 +429,7 @@ class Config: valid_types = _validate_valid_types(types) self._options[name] = _Opt(default, rebuild, valid_types) - def filter(self, rebuild: str | Sequence[str]) -> Iterator[ConfigValue]: + def filter(self, rebuild: Set[_ConfigRebuild]) -> Iterator[ConfigValue]: if isinstance(rebuild, str): return (value for value in self if value.rebuild == rebuild) return (value for value in self if value.rebuild in rebuild) diff --git a/sphinx/environment/__init__.py b/sphinx/environment/__init__.py index dfb35bee6..72753e240 100644 --- a/sphinx/environment/__init__.py +++ b/sphinx/environment/__init__.py @@ -320,7 +320,7 @@ class BuildEnvironment: else: # check if a config value was changed that affects how # doctrees are read - for item in config.filter('env'): + for item in config.filter(frozenset({'env'})): if self.config[item.name] != item.value: self.config_status = CONFIG_CHANGED self.config_status_extra = f' ({item.name!r})'