From b9a1e0cf77f0bbbfccea9c9a1d76733dc5f8ab33 Mon Sep 17 00:00:00 2001 From: Florian Strzelecki Date: Sat, 15 Jan 2022 19:16:20 +0100 Subject: [PATCH] tools.target: use SopelIdentifierMemory in Channel object Note: it is possible to keep the same type hint because SopelIdentifierMemory is a subclass of dict. --- sopel/tools/target.py | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/sopel/tools/target.py b/sopel/tools/target.py index 97d698c9ac..c02dfa6973 100644 --- a/sopel/tools/target.py +++ b/sopel/tools/target.py @@ -5,7 +5,7 @@ from typing import Any, Callable, Dict, Optional, Set, Union from sopel import privileges -from sopel.tools import identifiers +from sopel.tools import identifiers, memories IdentifierFactory = Callable[[str], identifiers.Identifier] @@ -94,13 +94,32 @@ def __init__( assert isinstance(name, identifiers.Identifier) self.name = name """The name of the channel.""" - self.users: Dict[identifiers.Identifier, User] = {} + + self.make_identifier: IdentifierFactory = identifier_factory + """Factory to create :class:`~sopel.tools.identifiers.Identifier`. + + ``Identifier`` is used for :class:`User`'s nick, and the channel + needs to translate nicks from string to ``Identifier`` when + manipulating data associated to a user by its nickname. + """ + + self.users: Dict[ + identifiers.Identifier, + User, + ] = memories.SopelIdentifierMemory( + identifier_factory=self.make_identifier, + ) """The users in the channel. This maps nickname :class:`~sopel.tools.identifiers.Identifier`\\s to :class:`User` objects. """ - self.privileges: Dict[identifiers.Identifier, int] = {} + self.privileges: Dict[ + identifiers.Identifier, + int, + ] = memories.SopelIdentifierMemory( + identifier_factory=self.make_identifier, + ) """The permissions of the users in the channel. This maps nickname :class:`~sopel.tools.identifiers.Identifier`\\s to @@ -133,14 +152,6 @@ def __init__( is available, otherwise the time Sopel received it. """ - self.make_identifier: IdentifierFactory = identifier_factory - """Factory to create :class:`~sopel.tools.identifiers.Identifier`. - - ``Identifier`` is used for :class:`User`'s nick, and the channel - needs to translate nicks from string to ``Identifier`` when - manipulating data associated to a user by its nickname. - """ - def clear_user(self, nick: identifiers.Identifier) -> None: """Remove ``nick`` from this channel.