From 76164e419e50520412fc07c1b25bd5fe19aa5d4d Mon Sep 17 00:00:00 2001 From: Gobot1234 Date: Fri, 22 Dec 2023 16:06:21 +0000 Subject: [PATCH] Fix Item class issues Fixes #405 --- steam/ext/csgo/backpack.py | 25 +++++++++++++++++-------- tests/unit/test_ext_csgo.py | 13 +++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/steam/ext/csgo/backpack.py b/steam/ext/csgo/backpack.py index 9ccf433b..ee4134cb 100644 --- a/steam/ext/csgo/backpack.py +++ b/steam/ext/csgo/backpack.py @@ -143,10 +143,11 @@ def __repr__(self) -> str: return f"<{self.__class__.__name__} id={self.id} position={self.position}>" -class CasketItem(BaseItem): +@BaseItem.register +class CasketItem(BaseItem if TYPE_CHECKING else object): """Represents an item in a :class:`Casket`.""" - __slots__ = ("_casket_id",) + __slots__ = (*BaseItem.SLOTS, "_casket_id") _casket_id: AssetID @property @@ -162,10 +163,8 @@ def __repr__(self) -> str: return f"<{self.__class__.__name__} id={self.id} casket={self.casket}>" -@dataclass(repr=False) -class BaseInspectedItem: - """Represents an item received after inspecting an item.""" - +@dataclass +class _BaseInspectedItem(metaclass=ABCMeta): __slots__ = SLOTS = ( "id", "def_index", @@ -221,11 +220,21 @@ def __repr__(self) -> str: return f"<{self.__class__.__name__} id={self.id}>" +@dataclass(repr=False) +@_BaseInspectedItem.register +class BaseInspectedItem(_BaseInspectedItem if TYPE_CHECKING else object, metaclass=ABCMeta): + """Represents an item received after inspecting an item.""" + + __slots__ = _BaseInspectedItem.SLOTS + __annotations__ = _BaseInspectedItem.__annotations__ + + OwnerT = TypeVar("OwnerT", bound="PartialUser", default="BaseUser", covariant=True) -class InspectedItem(Item[OwnerT], BaseInspectedItem): - __slots__ = BaseInspectedItem.SLOTS +@BaseInspectedItem.register +class InspectedItem(Item[OwnerT], _BaseInspectedItem): + __slots__ = _BaseInspectedItem.SLOTS F = TypeVar("F", bound=Callable[..., object]) diff --git a/tests/unit/test_ext_csgo.py b/tests/unit/test_ext_csgo.py index 9e9a048e..36ccebf8 100644 --- a/tests/unit/test_ext_csgo.py +++ b/tests/unit/test_ext_csgo.py @@ -1,4 +1,17 @@ +from unittest.mock import MagicMock + from steam.ext import csgo +from steam.protobufs.econ import Asset, ItemDescription client = csgo.Client() bot = csgo.Bot(command_prefix="!") + + +def test_item_construction(): + item = csgo.BackpackItem(MagicMock(), Asset(), ItemDescription(), MagicMock()) + assert csgo.BackpackItem.__slots__ + for attr in csgo.BackpackItem.__slots__: + setattr(item, attr, MagicMock()) + csgo.BaseInspectedItem(*(MagicMock(),) * len(csgo.BaseInspectedItem.__slots__)) + for attr in csgo.CasketItem.__slots__: + setattr(csgo.CasketItem(), attr, MagicMock())