Skip to content

Commit

Permalink
Return MappingProxyType instead of copying after each return.
Browse files Browse the repository at this point in the history
This reverts commit af54ac1.

Following a [suggested change][suggested-change], this moves to using
immutable `MappingProxyType` as return values, bypassing the need for
copying.

[suggested-change](#116 (review))
  • Loading branch information
ItsDrike committed Jun 11, 2023
1 parent af54ac1 commit adec604
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 34 deletions.
8 changes: 3 additions & 5 deletions mcproto/packets/packet_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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,
Expand All @@ -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)
29 changes: 0 additions & 29 deletions mcproto/utils/decorators.py

This file was deleted.

0 comments on commit adec604

Please sign in to comment.