App, lib: Add type annotations to untyped functions
Add missing type annotations to functions that are called from typed contexts, satisfying the new disallow_untyped_calls mypy rule. Fixes: - Local.py: __log() with typed parameters - lib/App.py: _add_arguments(), add_cmd_to_parser(), add_cmds_to_parser() - pm/rpm.py, dpkg.py: meta_map() return type - Exec.py: __init_askpass() return type - App.py: strip_module_from_spec(), __get_project_refs_cached(), ResultCache.__init__ and run(), _add_arguments() - Added Collection type for truthy-iterable compliance Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL and pi.dev 0.81.1 Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
75c9879f67
commit
410fd7ab5c
6 changed files with 26 additions and 15 deletions
|
|
@ -10,7 +10,7 @@ import sys
|
||||||
|
|
||||||
from enum import Enum, auto
|
from enum import Enum, auto
|
||||||
from functools import cache
|
from functools import cache
|
||||||
from typing import override, TYPE_CHECKING
|
from typing import Any, override, TYPE_CHECKING
|
||||||
|
|
||||||
from .lib.App import App as Base
|
from .lib.App import App as Base
|
||||||
from .lib.Distro import Distro
|
from .lib.Distro import Distro
|
||||||
|
|
@ -19,6 +19,7 @@ from .lib.ProjectConf import ProjectConf
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
import argparse
|
import argparse
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
from typing import TypeAlias
|
from typing import TypeAlias
|
||||||
|
|
||||||
|
|
@ -36,10 +37,10 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
class ResultCache(object):
|
class ResultCache(object):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
self.__cache = {}
|
self.__cache: dict[str, Any] = {}
|
||||||
|
|
||||||
def run(self, func, args):
|
def run(self, func, args: list[Any]) -> object:
|
||||||
d = self.__cache
|
d = self.__cache
|
||||||
depth = 0
|
depth = 0
|
||||||
keys = [func.__name__] + args
|
keys = [func.__name__] + args
|
||||||
|
|
@ -200,7 +201,15 @@ class App(Base):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def __get_project_refs_cached(
|
def __get_project_refs_cached(
|
||||||
self, buf, visited, spec, section, key, add_self, scope, names_only
|
self,
|
||||||
|
buf: list[str],
|
||||||
|
visited: set[str],
|
||||||
|
spec: str,
|
||||||
|
section: str,
|
||||||
|
key: str,
|
||||||
|
add_self: bool,
|
||||||
|
scope: Scope,
|
||||||
|
names_only: bool,
|
||||||
):
|
):
|
||||||
return self.__res_cache.run(
|
return self.__res_cache.run(
|
||||||
self.__get_project_refs,
|
self.__get_project_refs,
|
||||||
|
|
@ -381,7 +390,7 @@ class App(Base):
|
||||||
self.__exec_context = None
|
self.__exec_context = None
|
||||||
|
|
||||||
@override
|
@override
|
||||||
def _add_arguments(self, parser) -> None:
|
def _add_arguments(self, parser: ArgumentParser) -> None:
|
||||||
super()._add_arguments(parser)
|
super()._add_arguments(parser)
|
||||||
parser.add_argument('-t', '--topdir', default = None, help = 'Project Path')
|
parser.add_argument('-t', '--topdir', default = None, help = 'Project Path')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
|
|
@ -537,7 +546,7 @@ class App(Base):
|
||||||
def tmpl_dir(self, name: str) -> str | None:
|
def tmpl_dir(self, name: str) -> str | None:
|
||||||
return self.find_dir(name, ['/tmpl'], ['/opt/' + name + '/share/tmpl'])
|
return self.find_dir(name, ['/tmpl'], ['/opt/' + name + '/share/tmpl'])
|
||||||
|
|
||||||
def strip_module_from_spec(self, mod):
|
def strip_module_from_spec(self, mod: str) -> str:
|
||||||
return re.sub(r'-dev$|-devel$|-run$', '', re.split('([=><]+)', mod)[0].strip())
|
return re.sub(r'-dev$|-devel$|-run$', '', re.split('([=><]+)', mod)[0].strip())
|
||||||
|
|
||||||
@cache
|
@cache
|
||||||
|
|
|
||||||
|
|
@ -14,14 +14,14 @@ from .Types import LoadTypes
|
||||||
from .util import pretty_cmd
|
from .util import pretty_cmd
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Awaitable
|
from collections.abc import Awaitable, Collection
|
||||||
from typing import TypeVar
|
from typing import TypeVar
|
||||||
|
|
||||||
T = TypeVar('T')
|
T = TypeVar('T')
|
||||||
|
|
||||||
class App: # export
|
class App: # export
|
||||||
|
|
||||||
def _add_arguments(self, parser):
|
def _add_arguments(self, parser: ArgumentParser) -> None:
|
||||||
self.__parser.add_argument(
|
self.__parser.add_argument(
|
||||||
'--log-flags', help = 'Log flags', default = self.__default_log_flags
|
'--log-flags', help = 'Log flags', default = self.__default_log_flags
|
||||||
)
|
)
|
||||||
|
|
@ -51,7 +51,7 @@ class App: # export
|
||||||
eloop: None = None,
|
eloop: None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|
||||||
def add_cmd_to_parser(cmd, parsers):
|
def add_cmd_to_parser(cmd: AbstractCmd, parsers: Any) -> ArgumentParser:
|
||||||
parser = parsers.add_parser(
|
parser = parsers.add_parser(
|
||||||
cmd.name,
|
cmd.name,
|
||||||
help = cmd.help,
|
help = cmd.help,
|
||||||
|
|
@ -67,7 +67,7 @@ class App: # export
|
||||||
def add_cmds_to_parser(
|
def add_cmds_to_parser(
|
||||||
parent: AbstractCmd | App,
|
parent: AbstractCmd | App,
|
||||||
parser: ArgumentParser,
|
parser: ArgumentParser,
|
||||||
cmds,
|
cmds: Collection[AbstractCmd],
|
||||||
all = False
|
all = False
|
||||||
) -> None:
|
) -> None:
|
||||||
if not cmds:
|
if not cmds:
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ from ..ExecContext import ExecContext as Base
|
||||||
from ..log import ERR, NOTICE, log
|
from ..log import ERR, NOTICE, log
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from ..Uri import Uri
|
from ..Uri import Uri
|
||||||
|
|
||||||
class Local(Base):
|
class Local(Base):
|
||||||
|
|
@ -37,7 +39,7 @@ class Local(Base):
|
||||||
log_prefix: str,
|
log_prefix: str,
|
||||||
) -> Result:
|
) -> Result:
|
||||||
|
|
||||||
def __log(prio, *args, verbose = verbose):
|
def __log(prio: int, *args: Any, verbose: bool = verbose) -> None:
|
||||||
if verbose:
|
if verbose:
|
||||||
log(prio, log_prefix, *args)
|
log(prio, log_prefix, *args)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ class Exec(Base):
|
||||||
if self.__askpass is not None:
|
if self.__askpass is not None:
|
||||||
os.remove(self.__askpass)
|
os.remove(self.__askpass)
|
||||||
|
|
||||||
def __init_askpass(self):
|
def __init_askpass(self) -> None:
|
||||||
if self.__askpass is None and self.password is not None:
|
if self.__askpass is None and self.password is not None:
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ from ..util import run_cmd, run_sudo
|
||||||
|
|
||||||
_meta_map: dict[str, str] | None = None
|
_meta_map: dict[str, str] | None = None
|
||||||
|
|
||||||
def meta_map():
|
def meta_map() -> dict[str, str]:
|
||||||
global _meta_map
|
global _meta_map
|
||||||
if _meta_map is None:
|
if _meta_map is None:
|
||||||
_meta_map = Package.order_tags(
|
_meta_map = Package.order_tags(
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
_meta_map: dict[str, str] | None = None
|
_meta_map: dict[str, str] | None = None
|
||||||
|
|
||||||
def meta_map():
|
def meta_map() -> dict[str, str]:
|
||||||
global _meta_map
|
global _meta_map
|
||||||
if _meta_map is None:
|
if _meta_map is None:
|
||||||
_meta_map = Package.order_tags(
|
_meta_map = Package.order_tags(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue