diff --git a/mcproto/packets/packet_map.py b/mcproto/packets/packet_map.py index 9dde1310..713ea1b0 100644 --- a/mcproto/packets/packet_map.py +++ b/mcproto/packets/packet_map.py @@ -4,11 +4,10 @@ import pkgutil from collections.abc import Iterator, Mapping, Sequence from functools import lru_cache -from types import ModuleType +from types import MappingProxyType, ModuleType from typing import Literal, NamedTuple, NoReturn, overload from mcproto.packets.packet import ClientBoundPacket, GameState, Packet, PacketDirection, ServerBoundPacket -from mcproto.utils.decorators import copied_return __all__ = ["generate_packet_map"] @@ -95,9 +94,8 @@ def generate_packet_map( ... -@copied_return @lru_cache() -def generate_packet_map(direction: PacketDirection, state: GameState) -> dict[int, type[Packet]]: +def generate_packet_map(direction: PacketDirection, state: GameState) -> Mapping[int, type[Packet]]: """Dynamically generated a packet map for given ``direction`` and ``state``. This generation is done by dynamically importing all of the modules containing these packets, @@ -124,4 +122,4 @@ def generate_packet_map(direction: PacketDirection, state: GameState) -> dict[in if issubclass(packet_class, direction_class): packet_map[packet_class.PACKET_ID] = packet_class - return packet_map + return MappingProxyType(packet_map) diff --git a/mcproto/utils/decorators.py b/mcproto/utils/decorators.py deleted file mode 100644 index e7c04909..00000000 --- a/mcproto/utils/decorators.py +++ /dev/null @@ -1,29 +0,0 @@ -from __future__ import annotations - -from collections.abc import Callable -from functools import wraps -from typing import Protocol, TypeVar - -from typing_extensions import ParamSpec - -__all__ = ["copied_return"] - -T = TypeVar("T") - - -class SupportsCopy(Protocol): - def copy(self: T) -> T: - ... - - -P = ParamSpec("P") -R_Copyable = TypeVar("R_Copyable", bound=SupportsCopy) - - -def copied_return(func: Callable[P, R_Copyable]) -> Callable[P, R_Copyable]: - @wraps(func) - def wrapper(*args: P.args, **kwargs: P.kwargs) -> R_Copyable: - ret = func(*args, **kwargs) - return ret.copy() - - return wrapper