jw-python/src/python/jw/util/auth/dummy/Auth.py
Jan Lindemann 241aa79120
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>
2026-08-12 23:10:12 +00:00

146 lines
4.2 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING, override
from ...Config import Config
from ...log import WARNING, slog
from ..Auth import Access
from ..Auth import Auth as AuthBase
from ..Auth import Entity as EntityBase
from ..Auth import Group as Group # Needed by jwutils utility layer
from ..Auth import User as User # Needed by jwutils utility layer
from ..Auth import Kind, ProjectFlags
if TYPE_CHECKING:
from typing import Mapping, Sequence
class Entity(EntityBase): # export
def __init__(
self,
*,
auth: AuthBase,
name: str,
conf: Config,
kind: Kind,
**kwargs,
):
super().__init__(**kwargs)
self.__name = name
self.__conf = conf
self.__kind = kind
self.__auth = auth
self.__groups: list[Entity] | None = None
self.__email: str = conf['email']
@property
def conf(self):
return self.__conf
@override
def _name(self) -> str:
return self.__name
@override
def _groups(self) -> Sequence[Entity]:
if self.__kind != Kind.User:
return []
if self.__groups is None:
name: str = ''
ret: list[Entity] = []
for name in self.conf['groups']:
ret.append(
Entity(
auth = self.__auth,
name = name,
kind = Kind.Group,
conf = Config(),
)
)
self.__groups = ret
return self.__groups
@override
def _email(self) -> str:
return self.__email
@override
def _kind(self) -> Kind:
return self.__kind
@override
def _projects(self, flags: ProjectFlags) -> Sequence[str]:
return []
class Auth(AuthBase): # export
def __init__(self, conf: Config, **kwargs):
super().__init__(conf, **kwargs)
self.___users: dict[str, EntityBase] | None = None
self.__groups = None
self.__current_user: EntityBase | None = None
self.__user_by_email: dict[str, EntityBase] | None = None
@property
def __users(self) -> dict[str, EntityBase]:
if self.___users is None:
ret: dict[str, EntityBase] = {}
for name in self.conf.entries('user'):
conf = self.conf.branch('user.' + name)
assert conf is not None, 'Config is None'
ret[name] = Entity(
auth = self,
name = name,
conf = conf,
kind = Kind.User,
)
self.___users = ret
return self.___users
@override
def _access(
self, what: str, access_type: Access | None, who: EntityBase | None
) -> bool: # type: ignore[return-value]
slog(
WARNING,
f'Returning False for {access_type} access to resource {what} by {who}'
)
return False
@override
def _entity(self, name: str, kind: Kind) -> EntityBase:
if kind != Kind.User:
raise NotImplementedError(
'Support for retrieving groups is not implemented'
)
return self.__users[name]
@override
def _entities(self, kind: Kind) -> Mapping[str, EntityBase]:
if kind != Kind.User:
raise NotImplementedError(
'Support for retrieving all groups is not implemented'
)
return self.__users
@override
def _current_entity(self) -> EntityBase:
if self.__current_user is None:
self.__current_user = self._entity(
self.conf['current_user'],
kind = Kind.User,
)
return self.__current_user
@override
def _entity_by_email(self, email: str, kind: Kind) -> EntityBase:
if kind != Kind.User:
raise NotImplementedError(
'Support for retrieving groups by email is not implemented'
)
if self.__user_by_email is None:
ret: dict[str, EntityBase] = dict()
for user in self.__users.values():
ret[user.email] = user
self.__user_by_email = ret
return self.__user_by_email[email]