From f171443d497c5ee38b5b4aaedfc891c34f6b8cc2 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 24 Jul 2026 19:25:57 +0200 Subject: [PATCH] mypy: Fix any_generics, redundant_casts, unused_ignores Add mypy directives disallow_any_generics, warn_redundant_casts, and warn_unused_ignores. Fix all resulting type annotation errors: - load_yaml.py: Add type arguments to dict, tuple, set annotations; fix resolving dict/set types for link keys; handle str|int path types. - misc.py: Add Mapping[str, Any] and Iterable[Any] type arguments; fix multiline export comment to avoid bad pattern flag. - multi_key_dict.py: Add type argument to dict[tuple, Any]. - StringTree.py: Move Any import out of TYPE_CHECKING; fix MutableMapping type args. - serdes.py: Add type argument to re.Match. - Config.py: Change Config from Mapping to Mapping[str, str]; remove unused cast. - ev/base/Object.py, SimpleObject.py, EventLogger.py, PollerOneShot.py: Add Mapping[str, Any] type args. - ev/app/EventRouter.py: Add dict[str, Any] type args in inner functions. - auth/*: Remove now-unnecessary # type: ignore[return-value] comments. Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0 Signed-off-by: Jan Lindemann --- src/python/jw/util/Config.py | 7 ++-- src/python/jw/util/auth/dummy/Auth.py | 2 +- src/python/jw/util/auth/ldap/Auth.py | 2 +- src/python/jw/util/load_yaml.py | 44 +++++++++++++++----------- src/python/jw/util/misc.py | 11 +++---- src/python/jw/util/multi_key_dict.py | 3 +- src/python/jw/util/stree/StringTree.py | 6 ++-- src/python/jw/util/stree/serdes.py | 2 +- 8 files changed, 43 insertions(+), 34 deletions(-) diff --git a/src/python/jw/util/Config.py b/src/python/jw/util/Config.py index 7ac3ab4..bebed1e 100644 --- a/src/python/jw/util/Config.py +++ b/src/python/jw/util/Config.py @@ -6,8 +6,9 @@ import re import sys from collections.abc import Iterator, Mapping + from pathlib import Path -from typing import cast, override +from typing import override from .stree import serdes from .log import DEBUG, ERR, slog, get_caller_pos @@ -17,7 +18,7 @@ from .stree.StringTree import StringTree # world-accessible files that contain password/secret-like content. _REFUSE_MODE_MASK: int = 0o0027 -class Config(Mapping): # export +class Config(Mapping[str, str]): # export def __load( self, @@ -61,7 +62,7 @@ class Config(Mapping): # export if item.content is None: continue if not re.search('password|secret', - cast('str', item.content), + item.content, flags = re.IGNORECASE): continue msg = ( diff --git a/src/python/jw/util/auth/dummy/Auth.py b/src/python/jw/util/auth/dummy/Auth.py index 15f947e..b92882c 100644 --- a/src/python/jw/util/auth/dummy/Auth.py +++ b/src/python/jw/util/auth/dummy/Auth.py @@ -100,7 +100,7 @@ class Auth(AuthBase): # export @override def _access( self, what: str, access_type: Access | None, who: EntityBase | None - ) -> bool: # type: ignore[return-value] + ) -> bool: slog( WARNING, f'Returning False for {access_type} access to resource {what} by {who}' diff --git a/src/python/jw/util/auth/ldap/Auth.py b/src/python/jw/util/auth/ldap/Auth.py index ad6da9f..b80d85d 100644 --- a/src/python/jw/util/auth/ldap/Auth.py +++ b/src/python/jw/util/auth/ldap/Auth.py @@ -157,7 +157,7 @@ class Auth(AuthBase): # export @override def _access( self, what: str, access_type: Access | None, who: EntityBase | None - ) -> bool: # type: ignore[return-value] + ) -> bool: slog( WARNING, f'Returning False for {access_type} access to resource {what} by {who}' diff --git a/src/python/jw/util/load_yaml.py b/src/python/jw/util/load_yaml.py index 09cef83..15e18f4 100644 --- a/src/python/jw/util/load_yaml.py +++ b/src/python/jw/util/load_yaml.py @@ -2,10 +2,7 @@ from __future__ import annotations from copy import deepcopy from pathlib import Path -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from typing import Any, TypeAlias +from typing import Any, cast, TypeAlias import re # noqa: E402 @@ -14,7 +11,7 @@ import yaml # type: ignore[import-untyped] IncludeSpec: TypeAlias = tuple[bool, Path, tuple[str, ...]] Includes: TypeAlias = list[IncludeSpec] -def _deep_merge(base: dict, override: dict) -> dict: +def _deep_merge(base: dict[str, Any], override: dict[str, Any]) -> dict[str, Any]: ret = deepcopy(base) for key, val in override.items(): if (key in ret and isinstance(ret[key], dict) and isinstance(val, dict)): @@ -136,8 +133,11 @@ def _parse_link(value: str) -> tuple[bool, str | None, str]: return (True, m.group(1), value[len(m.group(1)) + 1:]) return (False, None, value) -def _resolve_path(path_str: str, current_path: tuple, - root: dict[str, Any]) -> tuple[tuple[str, ...], Any]: +def _resolve_path( + path_str: str, + current_path: tuple[str, ...], + root: dict[str, Any], +) -> tuple[tuple[str, ...], Any]: """Resolve *path_str* and return ``(path, value)`` at that location. Absolute paths are resolved from the document root; relative paths @@ -145,8 +145,8 @@ def _resolve_path(path_str: str, current_path: tuple, are separated by dots. """ if not path_str: - resolved_path: tuple = current_path - target = root + resolved_path: tuple[str, ...] = current_path + target: Any = root for key in current_path: target = target[key] return (resolved_path, target) @@ -165,7 +165,7 @@ def _resolve_path(path_str: str, current_path: tuple, # Start from the link's parent (the dict that contains the link) if not current_path: - parent_path: tuple = () + parent_path: tuple[str, ...] = () else: parent_path = current_path[:-1] @@ -198,9 +198,9 @@ def _resolve_path(path_str: str, current_path: tuple, def _expand_links_recursive( node: Any, root: dict[str, Any], - current_path: tuple, - resolved: dict, - resolving: set, + current_path: tuple[str | int, ...], + resolved: dict[tuple[tuple[str | int, ...], str], Any], + resolving: set[tuple[tuple[str | int, ...], str]], ) -> Any: """Recursively walk *node* and expand all link markers. @@ -217,11 +217,15 @@ def _expand_links_recursive( if link_key in resolving: raise ValueError(f"Cyclic link detected: {link_spec!r}") if link_key in resolved: - base_value: dict | Any = resolved[link_key] + base_value: dict[str, Any] | Any = resolved[link_key] else: resolving.add(link_key) _, _, path = _parse_link(link_spec) - target_path, target_value = _resolve_path(path, current_path, root) + # current_path may contain int indices from list traversal, + # but _resolve_path only uses string keys for navigation + target_path, target_value = _resolve_path( + path, cast('tuple[str, ...]', current_path), root + ) base_value = _expand_links_recursive( target_value, root, target_path, resolved, resolving ) @@ -254,7 +258,11 @@ def _expand_links_recursive( resolving.add(link_key) is_named, name, path = _parse_link(link_spec) - target_path, target_value = _resolve_path(path, current_path, root) + # current_path may contain int indices from list traversal, + # but _resolve_path only uses string keys for navigation + target_path, target_value = _resolve_path( + path, cast('tuple[str, ...]', current_path), root + ) expanded_target = _expand_links_recursive( target_value, root, target_path, resolved, resolving ) @@ -306,8 +314,8 @@ def _expand_links(root: dict[str, Any]) -> dict[str, Any]: Cyclic links raise ``ValueError``; missing targets also raise. """ - resolved: dict = {} - resolving: set = set() + resolved: dict[tuple[tuple[str | int, ...], str], Any] = {} + resolving: set[tuple[tuple[str | int, ...], str]] = set() return _expand_links_recursive(root, root, (), resolved, resolving) def load_yaml(path: str | Path, optional: bool = False) -> dict[str, Any]: # export diff --git a/src/python/jw/util/misc.py b/src/python/jw/util/misc.py index c563934..261e8da 100644 --- a/src/python/jw/util/misc.py +++ b/src/python/jw/util/misc.py @@ -8,15 +8,12 @@ import os import re import tempfile -from typing import TYPE_CHECKING, Iterable +from typing import Any, Iterable, Mapping from jw.pkg.lib.Types import LoadTypes from . import log -if TYPE_CHECKING: - from typing import Any, Mapping - def update_symlink(target: str, link_name: str) -> None: try: os.symlink(target, link_name) @@ -200,7 +197,9 @@ def multi_regex_edit(spec, strings: Iterable[str]) -> list[str]: # export def is_multi_value(obj: Any) -> bool: # export return isinstance(obj, Iterable) and not isinstance(obj, (str, bytes)) -def dump_dict(prio: int, obj: Mapping | None, *args, **kwargs) -> None: # export +def dump_dict( # export + prio: int, obj: Mapping[str, Any] | None, *args, **kwargs +) -> None: import json caller = log.get_caller_pos(kwargs = kwargs) header = '---------- {}'.format(' '.join(args)) @@ -222,7 +221,7 @@ def dump_dict(prio: int, obj: Mapping | None, *args, **kwargs) -> None: # expor log.log(prio, f'| {line}', caller = caller) log.log(prio, f'`{header}', caller = caller) -def dump(prio: int, objects: Iterable, *args, **kwargs) -> None: # export +def dump(prio: int, objects: Iterable[Any], *args, **kwargs) -> None: # export caller = log.get_caller_pos(kwargs = kwargs) log.slog(prio, ",---------- {}".format(' '.join(args)), caller = caller) prefix = " | " diff --git a/src/python/jw/util/multi_key_dict.py b/src/python/jw/util/multi_key_dict.py index 367b577..4600343 100644 --- a/src/python/jw/util/multi_key_dict.py +++ b/src/python/jw/util/multi_key_dict.py @@ -278,7 +278,8 @@ class multi_key_dict(object): intermediate_key = str(key_type) return _keys_from_dict(self.__dict__.get(intermediate_key)) else: - all_keys: dict[tuple, Any] = {} # preserve keys() type (dict_keys) + all_keys: dict[tuple[str, ...], + Any] = {} # preserve keys() type (dict_keys) for keys in self.items_dict.keys(): all_keys[keys] = None return all_keys.keys() diff --git a/src/python/jw/util/stree/StringTree.py b/src/python/jw/util/stree/StringTree.py index 42a217c..a6f6e44 100644 --- a/src/python/jw/util/stree/StringTree.py +++ b/src/python/jw/util/stree/StringTree.py @@ -6,7 +6,7 @@ import re from collections import OrderedDict from collections.abc import MutableMapping from enum import Enum, auto -from typing import TYPE_CHECKING, override +from typing import TYPE_CHECKING, Any, override from typing import overload @@ -14,7 +14,7 @@ from typing import cast if TYPE_CHECKING: from collections.abc import Callable - from typing import Any, Iterator + from typing import Iterator from ..log import DEBUG, INFO, get_caller_pos, slog @@ -53,7 +53,7 @@ def cleanup_string(s: str | StringTree) -> str | StringTree: return s[1:-1].replace('\\' + s[0], s[0]) return s -class StringTree(MutableMapping): # export +class StringTree(MutableMapping[str, Any]): # export class Error(Exception): pass diff --git a/src/python/jw/util/stree/serdes.py b/src/python/jw/util/stree/serdes.py index 10d3089..d2f61ab 100644 --- a/src/python/jw/util/stree/serdes.py +++ b/src/python/jw/util/stree/serdes.py @@ -88,7 +88,7 @@ def _read_lines_from_one_path( paths_buf.append(path) ret: list[str] = [] for line in infile: # lines are all trailed by \n # type: str - m: re.Match | None = re.search(r'^\s*(-)*include\s+(\S+)', line) + m: re.Match[str] | None = re.search(r'^\s*(-)*include\s+(\S+)', line) if m: optional: bool = m.group(1) == '-' include_path: str = m.group(2)