From adde25689377b58791ad2e357817378ad3a08a1b Mon Sep 17 00:00:00 2001 From: danieleades <33452915+danieleades@users.noreply.github.com> Date: Mon, 26 Feb 2024 08:45:19 +0000 Subject: [PATCH] [lint] shrink mypy whitelist (#11898) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- pyproject.toml | 4 ---- sphinx/roles.py | 2 +- sphinx/util/fileutil.py | 6 +++--- sphinx/util/inspect.py | 25 ++++++++++++++++--------- sphinx/util/inventory.py | 19 +++++++++++++++---- sphinx/util/nodes.py | 11 ++++++++--- 6 files changed, 43 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 01afc4562..1abd124f0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -194,7 +194,6 @@ module = [ "sphinx.highlighting", "sphinx.jinja2glue", "sphinx.registry", - "sphinx.roles", "sphinx.search", "sphinx.testing.fixtures", "sphinx.testing.path", @@ -203,12 +202,9 @@ module = [ "sphinx.util.display", "sphinx.util.docfields", "sphinx.util.docutils", - "sphinx.util.fileutil", "sphinx.util.i18n", "sphinx.util.inspect", - "sphinx.util.inventory", "sphinx.util.logging", - "sphinx.util.nodes", "sphinx.util.parallel", "sphinx.util.template", ] diff --git a/sphinx/roles.py b/sphinx/roles.py index 9618f48a5..8b78b7e0b 100644 --- a/sphinx/roles.py +++ b/sphinx/roles.py @@ -389,7 +389,7 @@ class Manpage(ReferenceRole): # TODO: Change to use `SphinxRole` once SphinxRole is fixed to support options. def code_role(name: str, rawtext: str, text: str, lineno: int, inliner: docutils.parsers.rst.states.Inliner, - options: dict | None = None, content: Sequence[str] = (), + options: dict[str, Any] | None = None, content: Sequence[str] = (), ) -> tuple[list[Node], list[system_message]]: if options is None: options = {} diff --git a/sphinx/util/fileutil.py b/sphinx/util/fileutil.py index ddefb8ab2..7a06c9850 100644 --- a/sphinx/util/fileutil.py +++ b/sphinx/util/fileutil.py @@ -4,7 +4,7 @@ from __future__ import annotations import os import posixpath -from typing import TYPE_CHECKING, Callable +from typing import TYPE_CHECKING, Any, Callable from docutils.utils import relative_path @@ -16,7 +16,7 @@ if TYPE_CHECKING: def copy_asset_file(source: str | os.PathLike[str], destination: str | os.PathLike[str], - context: dict | None = None, + context: dict[str, Any] | None = None, renderer: BaseRenderer | None = None) -> None: """Copy an asset file to destination. @@ -53,7 +53,7 @@ def copy_asset_file(source: str | os.PathLike[str], destination: str | os.PathLi def copy_asset(source: str | os.PathLike[str], destination: str | os.PathLike[str], excluded: PathMatcher = lambda path: False, - context: dict | None = None, renderer: BaseRenderer | None = None, + context: dict[str, Any] | None = None, renderer: BaseRenderer | None = None, onerror: Callable[[str, Exception], None] | None = None) -> None: """Copy asset files to destination recursively. diff --git a/sphinx/util/inspect.py b/sphinx/util/inspect.py index 120437da6..f3242e01b 100644 --- a/sphinx/util/inspect.py +++ b/sphinx/util/inspect.py @@ -53,7 +53,7 @@ def unwrap(obj: Any) -> Any: return obj -def unwrap_all(obj: Any, *, stop: Callable | None = None) -> Any: +def unwrap_all(obj: Any, *, stop: Callable[[Any], bool] | None = None) -> Any: """ Get an original object from wrapped object (unwrapping partials, wrapped functions, and other decorators). @@ -352,7 +352,7 @@ def safe_getattr(obj: Any, name: str, *defargs: Any) -> Any: raise AttributeError(name) from exc -def object_description(obj: Any, *, _seen: frozenset = frozenset()) -> str: +def object_description(obj: Any, *, _seen: frozenset[int] = frozenset()) -> str: """A repr() implementation that returns text safe to use in reST context. Maintains a set of 'seen' object IDs to detect and avoid infinite recursion. @@ -546,8 +546,9 @@ def _should_unwrap(subject: Callable) -> bool: return False -def signature(subject: Callable, bound_method: bool = False, type_aliases: dict | None = None, - ) -> inspect.Signature: +def signature( + subject: Callable, bound_method: bool = False, type_aliases: dict[str, str] | None = None, +) -> inspect.Signature: """Return a Signature object for the given *subject*. :param bound_method: Specify *subject* is a bound method or not @@ -604,15 +605,19 @@ def signature(subject: Callable, bound_method: bool = False, type_aliases: dict __validate_parameters__=False) -def evaluate_signature(sig: inspect.Signature, globalns: dict | None = None, - localns: dict | None = None, +def evaluate_signature(sig: inspect.Signature, globalns: dict[str, Any] | None = None, + localns: dict[str, Any] | None = None, ) -> inspect.Signature: """Evaluate unresolved type annotations in a signature object.""" - def evaluate_forwardref(ref: ForwardRef, globalns: dict, localns: dict) -> Any: + def evaluate_forwardref( + ref: ForwardRef, globalns: dict[str, Any] | None, localns: dict[str, Any] | None, + ) -> Any: """Evaluate a forward reference.""" return ref._evaluate(globalns, localns, frozenset()) - def evaluate(annotation: Any, globalns: dict, localns: dict) -> Any: + def evaluate( + annotation: Any, globalns: dict[str, Any], localns: dict[str, Any], + ) -> Any: """Evaluate unresolved type annotation.""" try: if isinstance(annotation, str): @@ -799,7 +804,9 @@ def getdoc( * inherited docstring * inherited decorated methods """ - def getdoc_internal(obj: Any, attrgetter: Callable = safe_getattr) -> str | None: + def getdoc_internal( + obj: Any, attrgetter: Callable[[Any, str, Any], Any] = safe_getattr, + ) -> str | None: doc = attrgetter(obj, '__doc__', None) if isinstance(doc, str): return doc diff --git a/sphinx/util/inventory.py b/sphinx/util/inventory.py index 2b466b79d..a43fd0379 100644 --- a/sphinx/util/inventory.py +++ b/sphinx/util/inventory.py @@ -25,7 +25,7 @@ class InventoryFileReader: This reader supports mixture of texts and compressed texts. """ - def __init__(self, stream: IO) -> None: + def __init__(self, stream: IO[bytes]) -> None: self.stream = stream self.buffer = b'' self.eof = False @@ -77,7 +77,12 @@ class InventoryFileReader: class InventoryFile: @classmethod - def load(cls: type[InventoryFile], stream: IO, uri: str, joinfunc: Callable) -> Inventory: + def load( + cls: type[InventoryFile], + stream: IO[bytes], + uri: str, + joinfunc: Callable[[str, str], str], + ) -> Inventory: reader = InventoryFileReader(stream) line = reader.readline().rstrip() if line == '# Sphinx inventory version 1': @@ -89,7 +94,10 @@ class InventoryFile: @classmethod def load_v1( - cls: type[InventoryFile], stream: InventoryFileReader, uri: str, join: Callable, + cls: type[InventoryFile], + stream: InventoryFileReader, + uri: str, + join: Callable[[str, str], str], ) -> Inventory: invdata: Inventory = {} projname = stream.readline().rstrip()[11:] @@ -109,7 +117,10 @@ class InventoryFile: @classmethod def load_v2( - cls: type[InventoryFile], stream: InventoryFileReader, uri: str, join: Callable, + cls: type[InventoryFile], + stream: InventoryFileReader, + uri: str, + join: Callable[[str, str], str], ) -> Inventory: invdata: Inventory = {} projname = stream.readline().rstrip()[11:] diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py index 015bc8f57..d33c2743f 100644 --- a/sphinx/util/nodes.py +++ b/sphinx/util/nodes.py @@ -402,9 +402,14 @@ def process_index_entry(entry: str, targetid: str, return indexentries -def inline_all_toctrees(builder: Builder, docnameset: set[str], docname: str, - tree: nodes.document, colorfunc: Callable, traversed: list[str], - ) -> nodes.document: +def inline_all_toctrees( + builder: Builder, + docnameset: set[str], + docname: str, + tree: nodes.document, + colorfunc: Callable[[str], str], + traversed: list[str], +) -> nodes.document: """Inline all toctrees in the *tree*. Record all docnames in *docnameset*, and output docnames with *colorfunc*.