mypy: Fix warn_return_any type errors
Add proper type annotations to fix mypy warn_return_any errors. The following files were modified to add cast() calls or return type annotations where functions returned Any values: - algo/ShuntingYard.py: Add cast for operator() return value - auth/Auth.py: Add cast for load() return value, fix imports - cast.py: Fix cast_str_to_int return type, fix _strip type - db/TableIoHandler.py: Remove redundant cast, fix store() return - db/query/Queries.py: Add cast for _run() and __getitem__ returns - db/query/Query.py: Add cast for db property return - db/query/QueryResult.py: Add cast for db and query property returns - db/schema/Schema.py: Add return type to __tables property, cast columns - db/schema/Table.py: Add cast for model_module_search_paths return - ev/app/EventRouter.py: Add cast for peer_conf.get() return - ev/base/Object.py: Add cast for get_conf_section and str_or_none returns - ev/base/SimpleObject.py: Add cast for create() return, fix imports - load_yaml.py: Add cast for _expand_links_recursive return - misc.py: Fix object_builtin_name return type, add param annotations - stree/StringTree.py: Fix get() return type with default_value variable Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.82.0 Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
f171443d49
commit
ec342ba172
12 changed files with 37 additions and 38 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import re
|
||||
from typing import override
|
||||
from typing import cast, override
|
||||
|
||||
from collections import namedtuple
|
||||
|
||||
|
|
@ -58,7 +58,7 @@ class ShuntingYard(object): # export
|
|||
slog(DEBUG, msg[1:], caller = get_caller_pos())
|
||||
|
||||
def operator(self, key: str) -> Operator:
|
||||
return self.__ops[key]
|
||||
return cast('Operator', self.__ops[key])
|
||||
|
||||
def token_string(self):
|
||||
r = ""
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
import abc
|
||||
|
||||
from enum import Enum, Flag, auto
|
||||
from typing import TYPE_CHECKING, TypeAlias
|
||||
from typing import TYPE_CHECKING, Any, cast, Self, TypeAlias, Sequence
|
||||
|
||||
from ..Config import Config
|
||||
from ..enum_helper import kebab_to_pascal
|
||||
|
|
@ -12,8 +12,7 @@ from ..misc import load_object
|
|||
from ..stree.StringTree import StringTree
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
||||
from typing import Any, Mapping, Self, Sequence
|
||||
from typing import Mapping
|
||||
|
||||
class Access(Enum): # export
|
||||
Read = auto()
|
||||
|
|
@ -127,13 +126,9 @@ class Auth(abc.ABC): # export
|
|||
class_name_filter = class_name_filter.format(**macros)
|
||||
|
||||
# == Do it
|
||||
return load_object(
|
||||
path,
|
||||
base_type,
|
||||
class_name_filter,
|
||||
conf,
|
||||
*args,
|
||||
**kwargs,
|
||||
return cast(
|
||||
'Self',
|
||||
load_object(path, base_type, class_name_filter, conf, *args, **kwargs)
|
||||
)
|
||||
|
||||
def __init__(self, conf: Config, *args, **kwargs) -> None:
|
||||
|
|
|
|||
|
|
@ -9,20 +9,20 @@ from .log import DEBUG, WARNING, slog
|
|||
|
||||
_int_chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
|
||||
|
||||
def _strip(s_) -> str:
|
||||
def _strip(s_: str) -> str:
|
||||
s = s_.strip()
|
||||
if len(s) != 0:
|
||||
if s:
|
||||
return s
|
||||
raise Exception(f'Tried to strip empty string "{s_}"')
|
||||
|
||||
def cast_str_to_timedelta(s_: str): # export
|
||||
def cast_str_to_timedelta(s_: str) -> timedelta: # export
|
||||
s = _strip(s_)
|
||||
seconds = pytimeparse.parse(s)
|
||||
seconds = pytimeparse.parse(s) # returns int | None
|
||||
if seconds is None:
|
||||
raise Exception(f'Failed to convert {s} to timedelta')
|
||||
return timedelta(seconds = seconds)
|
||||
|
||||
def cast_str_to_int(s_: str): # export
|
||||
def cast_str_to_int(s_: str) -> int: # export
|
||||
s = _strip(s_)
|
||||
if s[0] == '-':
|
||||
s = s[1:]
|
||||
|
|
|
|||
|
|
@ -88,4 +88,4 @@ class TableIoHandler(ABC): # export
|
|||
return ret
|
||||
|
||||
def store(self, uri: str, data: TType) -> None:
|
||||
return self._store(uri, data)
|
||||
self._store(uri, data)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
from __future__ import annotations
|
||||
from typing import Any, TYPE_CHECKING, override
|
||||
from typing import Any, cast, TYPE_CHECKING, override
|
||||
|
||||
import abc
|
||||
|
||||
|
|
@ -26,7 +26,7 @@ class Queries(abc.ABC): # export
|
|||
|
||||
@override
|
||||
def _run(self, session, *args, **kwargs) -> QueryResult:
|
||||
return self.__func(session, *args, **kwargs)
|
||||
return cast('QueryResult', self.__func(session, *args, **kwargs))
|
||||
|
||||
@override
|
||||
def _register(self):
|
||||
|
|
@ -47,7 +47,7 @@ class Queries(abc.ABC): # export
|
|||
|
||||
def __getitem__(self, name) -> Query:
|
||||
try:
|
||||
return self.__queries[name]
|
||||
return cast('Queries.Query', self.__queries[name])
|
||||
except Exception as e:
|
||||
slog(ERR, f'Failed to retrieve query {name} ({e})')
|
||||
slog_m(ERR, '\n'.join(self.__queries.keys()))
|
||||
|
|
@ -68,7 +68,7 @@ class Queries(abc.ABC): # export
|
|||
|
||||
@property
|
||||
def schema(self) -> Schema:
|
||||
return self.__db.schema
|
||||
return cast('Schema', self.__db.schema)
|
||||
|
||||
def add(self, query: QueryBase, query_name: str, location: str, func: Any):
|
||||
slog(INFO, f'Adding query "{query_name}" on location "{location}"')
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import abc
|
||||
|
||||
from typing import TYPE_CHECKING, Any
|
||||
from typing import TYPE_CHECKING, Any, cast
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..DataBase import DataBase
|
||||
|
|
@ -47,7 +47,7 @@ class Query(abc.ABC): # export
|
|||
|
||||
@property
|
||||
def db(self) -> DataBase:
|
||||
return self.__parent.db
|
||||
return cast('DataBase', self.__parent.db)
|
||||
|
||||
@property
|
||||
def schema(self):
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from __future__ import annotations
|
|||
import abc
|
||||
|
||||
from enum import Enum, auto
|
||||
from typing import TYPE_CHECKING, Any, Union
|
||||
from typing import TYPE_CHECKING, Any, cast, Union
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..DataBase import DataBase
|
||||
|
|
@ -24,11 +24,11 @@ class QueryResult(abc.ABC): # export
|
|||
|
||||
@property
|
||||
def db(self) -> DataBase:
|
||||
return self.__query.db
|
||||
return cast('DataBase', self.__query.db)
|
||||
|
||||
@property
|
||||
def query(self) -> DataBase:
|
||||
return self.__query
|
||||
return cast('DataBase', self.__query)
|
||||
|
||||
@property
|
||||
def session(self) -> Session:
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
|||
|
||||
import abc
|
||||
|
||||
from typing import TYPE_CHECKING, override, Iterable, Optional
|
||||
from typing import TYPE_CHECKING, cast, override, Iterable, Optional
|
||||
|
||||
from ...log import DEBUG, ERR, slog, throw
|
||||
|
||||
|
|
@ -19,9 +19,9 @@ class Schema(abc.ABC): # export
|
|||
self.__access_defining_columns: Optional[list[str]] = None
|
||||
|
||||
@property
|
||||
def __tables(self):
|
||||
def __tables(self) -> dict[str, Table]:
|
||||
if self.___tables is None:
|
||||
ret = dict()
|
||||
ret: dict[str, Table] = dict()
|
||||
for name in self._table_names():
|
||||
slog(DEBUG, f'Caching metadata for table "{name}"')
|
||||
assert (isinstance(name, str))
|
||||
|
|
@ -103,7 +103,7 @@ class Schema(abc.ABC): # export
|
|||
return self.__tables[table_name].primary_keys
|
||||
|
||||
def columns(self, table_name: str) -> Iterable[Column]:
|
||||
return self.__tables[table_name].columns
|
||||
return cast('Iterable[Column]', self.__tables[table_name].columns)
|
||||
|
||||
@property
|
||||
def model_module_search_paths(self) -> list[tuple[str, type]]:
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import abc
|
|||
import re
|
||||
|
||||
from collections import OrderedDict
|
||||
from typing import TYPE_CHECKING, override
|
||||
from typing import TYPE_CHECKING, cast, override
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
from ...log import ERR, WARNING, slog, throw
|
||||
|
|
@ -177,7 +177,7 @@ class Table(abc.ABC): # export
|
|||
|
||||
def _model_module_search_paths(self) -> list[tuple[str, type]]:
|
||||
# Fall back to Schema-global default
|
||||
return self.schema.model_module_search_paths
|
||||
return cast('list[tuple[str, type]]', self.schema.model_module_search_paths)
|
||||
|
||||
@abc.abstractmethod
|
||||
def _query_name(self) -> str:
|
||||
|
|
|
|||
|
|
@ -316,7 +316,9 @@ def _expand_links(root: dict[str, Any]) -> dict[str, Any]:
|
|||
"""
|
||||
resolved: dict[tuple[tuple[str | int, ...], str], Any] = {}
|
||||
resolving: set[tuple[tuple[str | int, ...], str]] = set()
|
||||
return _expand_links_recursive(root, root, (), resolved, resolving)
|
||||
return cast(
|
||||
'dict[str, Any]', _expand_links_recursive(root, root, (), resolved, resolving)
|
||||
)
|
||||
|
||||
def load_yaml(path: str | Path, optional: bool = False) -> dict[str, Any]: # export
|
||||
seen: set[Path] = set()
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ def atomic_store(contents: bytes, path: str) -> None: # export
|
|||
os.remove(name)
|
||||
|
||||
# see https://stackoverflow.com/questions/2020014
|
||||
def object_builtin_name(o, full = True) -> None: # export
|
||||
def object_builtin_name(o: object, full: bool = True) -> str: # export
|
||||
#if not full:
|
||||
# return o.__class__.__name__
|
||||
module = o.__class__.__module__
|
||||
|
|
|
|||
|
|
@ -339,24 +339,26 @@ class StringTree(MutableMapping[str, Any]): # export
|
|||
def get(self, path: str, default: Any = None) -> StringTree | None:
|
||||
slog(DEBUG, 'looking for "{}" in "{}"'.format(path, self.content))
|
||||
_path: str = cleanup_string(path)
|
||||
# Narrow default to match return type
|
||||
default_value: StringTree | None = default
|
||||
if len(_path) == 0:
|
||||
slog(DEBUG, "returning myself")
|
||||
return self
|
||||
if is_quoted(path):
|
||||
if _path not in self.children.keys():
|
||||
return default
|
||||
return default_value
|
||||
return self.children[_path]
|
||||
components: list[str] = _path.split('.')
|
||||
if len(components) == 0:
|
||||
return self
|
||||
name: str = cleanup_string(components[0])
|
||||
if not hasattr(self, "children"):
|
||||
return default
|
||||
return default_value
|
||||
if name not in self.children.keys():
|
||||
slog(DEBUG, "Name \"" + name + "\" is not in children of", self.content)
|
||||
for child in self.children:
|
||||
slog(DEBUG, "child = ", child)
|
||||
return default
|
||||
return default_value
|
||||
rel_path: str = '.'.join(components[1:])
|
||||
return self.children[name].get(rel_path)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue