mypy: Fix fallout of ignore-without-code
Fix fallout of mypy --enable-error-code=ignore-without-code check. The multi_key_dict class had several type issues: the keys() and values() methods could implicitly return None when a key type was specified but not found, pyright flagged the conditional items() redefinition, and six test call sites carried bare "# type: ignore" comments. Add from __future__ import annotations, a _keys_from_dict() helper, and proper parameter/return type annotations to keys(), values(), and items(). Consolidate the conditional Python 2/3 items() into a single function with runtime dispatch. Remove all six bare type-ignores from multi_key_dict.py and annotate the remaining pre-existing ones in other files with specific error codes required by ignore-without-code. 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
0e660dc58b
commit
241aa79120
7 changed files with 41 additions and 35 deletions
|
|
@ -100,7 +100,7 @@ class Auth(AuthBase): # export
|
|||
@override
|
||||
def _access(
|
||||
self, what: str, access_type: Access | None, who: EntityBase | None
|
||||
) -> bool: # type: ignore
|
||||
) -> bool: # type: ignore[return-value]
|
||||
slog(
|
||||
WARNING,
|
||||
f'Returning False for {access_type} access to resource {what} by {who}'
|
||||
|
|
|
|||
|
|
@ -134,7 +134,7 @@ class Auth(AuthBase): # export
|
|||
def __user_by_email(self) -> dict[str, Entity]:
|
||||
if self.___user_by_email is None:
|
||||
self.__users
|
||||
return self.___user_by_email # type: ignore # We are sure that ___user_by_email is not None at this point
|
||||
return self.___user_by_email # type: ignore[return-value] # We are sure that ___user_by_email is not None at this point
|
||||
|
||||
def _create_entity(
|
||||
self,
|
||||
|
|
@ -158,7 +158,7 @@ class Auth(AuthBase): # export
|
|||
@override
|
||||
def _access(
|
||||
self, what: str, access_type: Access | None, who: EntityBase | None
|
||||
) -> bool: # type: ignore
|
||||
) -> bool: # type: ignore[return-value]
|
||||
slog(
|
||||
WARNING,
|
||||
f'Returning False for {access_type} access to resource {what} by {who}'
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import os
|
|||
import re
|
||||
import textwrap
|
||||
|
||||
from tabulate import TableFormat, tabulate # type: ignore
|
||||
from tabulate import TableFormat, tabulate # type: ignore[import-untyped]
|
||||
|
||||
from ..log import (ERR, INFO, WARNING, get_caller_pos, prio_gets_logged, slog, slog_m)
|
||||
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ class Column(abc.ABC): # export
|
|||
def foreign_key(self, table) -> Optional[Any]:
|
||||
if self.__foreign_keys_by_table is None:
|
||||
self.__foreign_keys_by_table = dict()
|
||||
for col in self.foreign_keys: # type: ignore # Any not iterable
|
||||
for col in self.foreign_keys: # type: ignore[union-attr] # Any not iterable
|
||||
assert (col.table.name not in self.__foreign_keys_by_table)
|
||||
self.__foreign_keys_by_table[col.table.name] = col
|
||||
table_name = table if isinstance(table, str) else table.name
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ class Schema(abc.ABC): # export
|
|||
@abc.abstractmethod
|
||||
def _table(self, name: str) -> Table:
|
||||
throw(ERR, "Called pure virtual base class method")
|
||||
return None # type: ignore
|
||||
return None # type: ignore[return-value]
|
||||
|
||||
@abc.abstractmethod
|
||||
def _foreign_keys(self) -> list[CompositeForeignKey]:
|
||||
|
|
@ -97,7 +97,7 @@ class Schema(abc.ABC): # export
|
|||
return table
|
||||
if throw:
|
||||
raise Exception(f'Table "{name}" not found in database metadata')
|
||||
return None # type: ignore
|
||||
return None # type: ignore[return-value]
|
||||
|
||||
def primary_keys(self, table_name: str) -> Iterable[str]:
|
||||
return self.__tables[table_name].primary_keys
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ class Connection: # export
|
|||
except Exception:
|
||||
# mypy says: E: "Config" has no attribute "uri" [attr-defined]
|
||||
# FIXME: Who adds .uri?
|
||||
uri = c.uri # type: ignore
|
||||
uri = c.uri # type: ignore[attr-defined]
|
||||
try:
|
||||
ret = ldap.initialize(uri)
|
||||
ret.start_tls_s()
|
||||
|
|
|
|||
|
|
@ -29,9 +29,19 @@ OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
|||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
'''
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import platform
|
||||
from typing import Any
|
||||
|
||||
from typing import override
|
||||
|
||||
def _keys_from_dict(d: dict[Any, Any] | None) -> Any:
|
||||
"""Return dict_keys of *d*, or empty dict_keys if *d* is None."""
|
||||
if d is None:
|
||||
return {}
|
||||
return d
|
||||
|
||||
_python3 = int(platform.python_version_tuple()[0]) >= 3
|
||||
|
||||
class multi_key_dict(object):
|
||||
|
|
@ -250,45 +260,44 @@ class multi_key_dict(object):
|
|||
for value in self.items_dict.values():
|
||||
yield value
|
||||
|
||||
if _python3:
|
||||
items = iteritems # type: ignore
|
||||
else:
|
||||
|
||||
def items(self, key_type = None, return_all_keys = False):
|
||||
def items(self, key_type: Any = None, return_all_keys: bool = False):
|
||||
""" Returns an iterator (Python 3) or list (Python 2) over items."""
|
||||
if _python3:
|
||||
return self.iteritems(key_type, return_all_keys)
|
||||
else:
|
||||
return list(self.iteritems(key_type, return_all_keys))
|
||||
|
||||
items.__doc__ = iteritems.__doc__
|
||||
items.__doc__ = iteritems.__doc__
|
||||
|
||||
def keys(self, key_type = None):
|
||||
def keys(self, key_type: Any = None) -> Any:
|
||||
""" Returns a copy of the dictionary's keys.
|
||||
@param key_type if specified, only keys for this type will be returned.
|
||||
Otherwise list of tuples containing all (multiple) keys will be
|
||||
returned."""
|
||||
if key_type is not None:
|
||||
intermediate_key = str(key_type)
|
||||
if intermediate_key in self.__dict__:
|
||||
return self.__dict__[intermediate_key].keys()
|
||||
return _keys_from_dict(self.__dict__.get(intermediate_key))
|
||||
else:
|
||||
all_keys = {} # in order to preserve keys() type (dict_keys for python3)
|
||||
all_keys: dict[tuple, Any] = {} # preserve keys() type (dict_keys)
|
||||
for keys in self.items_dict.keys():
|
||||
all_keys[keys] = None
|
||||
return all_keys.keys()
|
||||
|
||||
def values(self, key_type = None):
|
||||
def values(self, key_type: Any = None) -> Any:
|
||||
""" Returns a copy of the dictionary's values.
|
||||
@param key_type if specified, only values pointed by keys of this type
|
||||
will be returned
|
||||
Otherwise list of all values contained in this dictionary will be
|
||||
returned."""
|
||||
if (key_type is not None):
|
||||
all_items = {} # in order to preserve keys() type (dict_values for python3)
|
||||
keys_used = set()
|
||||
all_items: dict[Any, Any] = {} # preserve dict_values type
|
||||
keys_used: set[Any] = set()
|
||||
direct_key = str(key_type)
|
||||
if direct_key in self.__dict__:
|
||||
for intermediate_key in self.__dict__[direct_key].values():
|
||||
if intermediate_key not in keys_used:
|
||||
all_items[intermediate_key] = self.items_dict[intermediate_key]
|
||||
keys_used.add(intermediate_key)
|
||||
for intermediate_key in (self.__dict__[direct_key].values()
|
||||
if direct_key in self.__dict__ else ()):
|
||||
if intermediate_key not in keys_used:
|
||||
all_items[intermediate_key] = self.items_dict[intermediate_key]
|
||||
keys_used.add(intermediate_key)
|
||||
return all_items.values()
|
||||
else:
|
||||
return self.items_dict.values()
|
||||
|
|
@ -445,7 +454,7 @@ def test_multi_key_dict():
|
|||
|
||||
# now check if we can get all possible keys (formed in a list of tuples)
|
||||
# each tuple containing all keys)
|
||||
res = sorted([sorted([str(x) for x in k]) for k in m.keys()]) # type: ignore
|
||||
res = sorted([sorted([str(x) for x in k]) for k in m.keys()])
|
||||
expected = sorted([sorted([str(x) for x in k]) for k in all_keys])
|
||||
assert (res == expected), (
|
||||
'unexpected values from m.keys(), got:\n%s\n expected:\n%s' % (res, expected)
|
||||
|
|
@ -465,7 +474,7 @@ def test_multi_key_dict():
|
|||
|
||||
# test default iterkeys()
|
||||
num_of_elements = 0
|
||||
for keys in m.keys(): # type: ignore
|
||||
for keys in m.keys():
|
||||
num_of_elements += 1
|
||||
keys_s = sorted([str(k) for k in keys])
|
||||
assert (keys_s in expected), 'm.keys(): unexpected keys: {0}'.format(keys_s)
|
||||
|
|
@ -532,7 +541,7 @@ def test_multi_key_dict():
|
|||
all_items = set()
|
||||
keys = m.keys()
|
||||
m.values()
|
||||
for k in keys: # type: ignore
|
||||
for k in keys:
|
||||
all_items.add((tuple(k), m[k[0]]))
|
||||
|
||||
res = set(m.items())
|
||||
|
|
@ -609,7 +618,7 @@ def test_multi_key_dict():
|
|||
assert (res == tst_range), 'm.values(int) is not as expected.'
|
||||
|
||||
# test keys()
|
||||
assert (set(m.keys(int)) == set(tst_range)), 'm.keys(int) is not as expected.' # type: ignore
|
||||
assert (set(m.keys(int)) == set(tst_range)), 'm.keys(int) is not as expected.'
|
||||
|
||||
# test setitem with multiple keys
|
||||
m['xy', 999, 'abcd'] = 'teststr'
|
||||
|
|
@ -654,14 +663,11 @@ def test_multi_key_dict():
|
|||
d[n] = 'now' # use datetime obj as a key
|
||||
|
||||
#test keys..
|
||||
res = [
|
||||
x for x in d.keys(
|
||||
) # type: ignore # for python3 keys() returns dict_keys dictionarly
|
||||
][0]
|
||||
res = [x for x in d.keys()][0]
|
||||
expected = n,
|
||||
assert (expected == res), 'Expected \"{0}\", but got: \"{1}\"'.format(expected, res)
|
||||
|
||||
res = [x for x in d.keys(datetime.datetime)][0] # type: ignore
|
||||
res = [x for x in d.keys(datetime.datetime)][0]
|
||||
assert (n == res), 'Expected {0} as a key, but got: {1}'.format(n, res)
|
||||
|
||||
res = [x for x in d.values()] # for python3 keys() returns dict_values dictionarly
|
||||
|
|
|
|||
Loading…
Reference in a new issue