Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide the component deserialize methods on the entity factory #1074

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1074.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hide the entity factory's component deserialize methods.
62 changes: 0 additions & 62 deletions hikari/api/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,68 +1296,6 @@ def deserialize_guild_sticker(self, payload: data_binding.JSONObject) -> sticker
# MESSAGE MODELS #
##################

@abc.abstractmethod
def deserialize_action_row(self, payload: data_binding.JSONObject) -> message_models.ActionRowComponent:
"""Parse a raw payload from Discord into an action row component object.

Parameters
----------
payload : hikari.internal.data_binding.JSONObject
The JSON payload to deserialize.

Returns
-------
hikari.messages.ActionRowComponent
The deserialized action row component.
"""

@abc.abstractmethod
def deserialize_button(self, payload: data_binding.JSONObject) -> message_models.ButtonComponent:
"""Parse a raw payload from Discord into a button component object.

Parameters
----------
payload : hikari.internal.data_binding.JSONObject
The JSON payload to deserialize.

Returns
-------
hikari.messages.ButtonComponent
The deserialized button component.
"""

@abc.abstractmethod
def deserialize_select_menu(self, payload: data_binding.JSONObject) -> message_models.SelectMenuComponent:
"""Parse a raw payload from Discord into a select menu component object.

Parameters
----------
payload : hikari.internal.data_binding.JSONObject
The JSON payload to deserialize.

Returns
-------
hikari.messages.SelectMenuComponent
The deserialized button component.
"""

@abc.abstractmethod
def deserialize_component(self, payload: data_binding.JSONObject) -> message_models.PartialComponent:
"""Parse a raw payload from Discord into a message component object.

Parameters
----------
payload : hikari.internal.data_binding.JSONObject
The JSON payload to deserialize.

Returns
-------
hikari.messages.PartialComponent
The deserialized message component.
hikari.errors.UnrecognisedEntityError
If the message component type isn't recognised.
"""

@abc.abstractmethod
def deserialize_partial_message(self, payload: data_binding.JSONObject) -> message_models.PartialMessage:
"""Parse a raw payload from Discord into a partial message object.
Expand Down
20 changes: 10 additions & 10 deletions hikari/impl/entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ def __init__(self, app: traits.RESTAware) -> None:
commands.CommandType.MESSAGE: self.deserialize_context_menu_command,
}
self._component_type_mapping = {
message_models.ComponentType.ACTION_ROW: self.deserialize_action_row,
message_models.ComponentType.BUTTON: self.deserialize_button,
message_models.ComponentType.SELECT_MENU: self.deserialize_select_menu,
message_models.ComponentType.ACTION_ROW: self._deserialize_action_row,
message_models.ComponentType.BUTTON: self._deserialize_button,
message_models.ComponentType.SELECT_MENU: self._deserialize_select_menu,
}
self._dm_channel_type_mapping = {
channel_models.ChannelType.DM: self.deserialize_dm,
Expand Down Expand Up @@ -2181,12 +2181,12 @@ def deserialize_guild_sticker(self, payload: data_binding.JSONObject) -> sticker
# MESSAGE MODELS #
##################

def deserialize_action_row(self, payload: data_binding.JSONObject) -> message_models.ActionRowComponent:
def _deserialize_action_row(self, payload: data_binding.JSONObject) -> message_models.ActionRowComponent:
components: typing.List[message_models.PartialComponent] = []

for component_payload in payload["components"]:
try:
components.append(self.deserialize_component(component_payload))
components.append(self._deserialize_component(component_payload))

except errors.UnrecognisedEntityError:
pass
Expand All @@ -2195,7 +2195,7 @@ def deserialize_action_row(self, payload: data_binding.JSONObject) -> message_mo
type=message_models.ComponentType(payload["type"]), components=components
)

def deserialize_button(self, payload: data_binding.JSONObject) -> message_models.ButtonComponent:
def _deserialize_button(self, payload: data_binding.JSONObject) -> message_models.ButtonComponent:
emoji_payload = payload.get("emoji")
return message_models.ButtonComponent(
type=message_models.ComponentType(payload["type"]),
Expand All @@ -2207,7 +2207,7 @@ def deserialize_button(self, payload: data_binding.JSONObject) -> message_models
is_disabled=payload.get("disabled", False),
)

def deserialize_select_menu(self, payload: data_binding.JSONObject) -> message_models.SelectMenuComponent:
def _deserialize_select_menu(self, payload: data_binding.JSONObject) -> message_models.SelectMenuComponent:
options: typing.List[message_models.SelectMenuOption] = []
for option_payload in payload["options"]:
emoji = None
Expand All @@ -2234,7 +2234,7 @@ def deserialize_select_menu(self, payload: data_binding.JSONObject) -> message_m
is_disabled=payload.get("disabled", False),
)

def deserialize_component(self, payload: data_binding.JSONObject) -> message_models.PartialComponent:
def _deserialize_component(self, payload: data_binding.JSONObject) -> message_models.PartialComponent:
component_type = message_models.ComponentType(payload["type"])

if deserialize := self._component_type_mapping.get(component_type):
Expand Down Expand Up @@ -2390,7 +2390,7 @@ def deserialize_partial_message( # noqa CFQ001 - Function too long
components = []
for component_payload in component_payloads:
try:
components.append(self.deserialize_component(component_payload))
components.append(self._deserialize_component(component_payload))

except errors.UnrecognisedEntityError:
pass
Expand Down Expand Up @@ -2509,7 +2509,7 @@ def deserialize_message(
if component_payloads := payload.get("components"):
for component_payload in component_payloads:
try:
components.append(self.deserialize_component(component_payload))
components.append(self._deserialize_component(component_payload))

except errors.UnrecognisedEntityError:
pass
Expand Down
38 changes: 19 additions & 19 deletions tests/hikari/impl/test_entity_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3939,14 +3939,14 @@ def test_max_age_when_zero(self, entity_factory_impl, invite_with_metadata_paylo
def action_row_payload(self, button_payload):
return {"type": 1, "components": [button_payload]}

def test_deserialize_action_row(self, entity_factory_impl, action_row_payload, button_payload):
action_row = entity_factory_impl.deserialize_action_row(action_row_payload)
def test__deserialize_action_row(self, entity_factory_impl, action_row_payload, button_payload):
action_row = entity_factory_impl._deserialize_action_row(action_row_payload)

assert action_row.type is message_models.ComponentType.ACTION_ROW
assert action_row.components == [entity_factory_impl.deserialize_component(button_payload)]
assert action_row.components == [entity_factory_impl._deserialize_component(button_payload)]

def test_deserialize_action_row_handles_unknown_component_type(self, entity_factory_impl):
action_row = entity_factory_impl.deserialize_action_row(
def test__deserialize_action_row_handles_unknown_component_type(self, entity_factory_impl):
action_row = entity_factory_impl._deserialize_action_row(
{"type": 1, "components": [{"type": "9494949"}, {"type": "9239292"}]}
)

Expand All @@ -3964,8 +3964,8 @@ def button_payload(self, custom_emoji_payload):
"disabled": True,
}

def test_deserialize_deserialize_button(self, entity_factory_impl, button_payload, custom_emoji_payload):
button = entity_factory_impl.deserialize_button(button_payload)
def test_deserialize__deserialize_button(self, entity_factory_impl, button_payload, custom_emoji_payload):
button = entity_factory_impl._deserialize_button(button_payload)

assert button.type is message_models.ComponentType.BUTTON
assert button.style is message_models.ButtonStyle.PRIMARY
Expand All @@ -3975,10 +3975,10 @@ def test_deserialize_deserialize_button(self, entity_factory_impl, button_payloa
assert button.is_disabled is True
assert button.url == "okokok"

def test_deserialize_deserialize_button_with_unset_fields(
def test_deserialize__deserialize_button_with_unset_fields(
self, entity_factory_impl, button_payload, custom_emoji_payload
):
button = entity_factory_impl.deserialize_button({"type": 2, "style": 5})
button = entity_factory_impl._deserialize_button({"type": 2, "style": 5})

assert button.type is message_models.ComponentType.BUTTON
assert button.style is message_models.ButtonStyle.LINK
Expand Down Expand Up @@ -4008,8 +4008,8 @@ def select_menu_payload(self, custom_emoji_payload):
"disabled": True,
}

def test_deserialize_select_menu(self, entity_factory_impl, select_menu_payload, custom_emoji_payload):
menu = entity_factory_impl.deserialize_select_menu(select_menu_payload)
def test__deserialize_select_menu(self, entity_factory_impl, select_menu_payload, custom_emoji_payload):
menu = entity_factory_impl._deserialize_select_menu(select_menu_payload)

assert menu.type is message_models.ComponentType.SELECT_MENU
assert menu.custom_id == "Not an ID"
Expand All @@ -4029,8 +4029,8 @@ def test_deserialize_select_menu(self, entity_factory_impl, select_menu_payload,
assert menu.max_values == 420
assert menu.is_disabled is True

def test_deserialize_select_menu_partial(self, entity_factory_impl):
menu = entity_factory_impl.deserialize_select_menu(
def test__deserialize_select_menu_partial(self, entity_factory_impl):
menu = entity_factory_impl._deserialize_select_menu(
{
"type": 3,
"custom_id": "Not an ID",
Expand All @@ -4050,17 +4050,17 @@ def test_deserialize_select_menu_partial(self, entity_factory_impl):
assert menu.max_values == 1
assert menu.is_disabled is False

def test_deserialize_component(self, entity_factory_impl, action_row_payload, button_payload, select_menu_payload):
def test__deserialize_component(self, entity_factory_impl, action_row_payload, button_payload, select_menu_payload):
for expected_type, payload in [
(message_models.ActionRowComponent, action_row_payload),
(message_models.ButtonComponent, button_payload),
(message_models.SelectMenuComponent, select_menu_payload),
]:
assert type(entity_factory_impl.deserialize_component(payload)) is expected_type
assert type(entity_factory_impl._deserialize_component(payload)) is expected_type

def test_deserialize_component_handles_unknown_type(self, entity_factory_impl):
def test__deserialize_component_handles_unknown_type(self, entity_factory_impl):
with pytest.raises(errors.UnrecognisedEntityError):
entity_factory_impl.deserialize_component({"type": -9434994})
entity_factory_impl._deserialize_component({"type": -9434994})

@pytest.fixture()
def partial_application_payload(self):
Expand Down Expand Up @@ -4300,7 +4300,7 @@ def test_deserialize_partial_message(
assert partial_message.interaction.user == entity_factory_impl.deserialize_user(user_payload)
assert isinstance(partial_message.interaction, message_models.MessageInteraction)

assert partial_message.components == [entity_factory_impl.deserialize_component(action_row_payload)]
assert partial_message.components == [entity_factory_impl._deserialize_component(action_row_payload)]

def test_deserialize_partial_message_with_partial_fields(self, entity_factory_impl, message_payload):
message_payload["content"] = ""
Expand Down Expand Up @@ -4482,7 +4482,7 @@ def test_deserialize_message(
assert message.interaction.user == entity_factory_impl.deserialize_user(user_payload)
assert isinstance(message.interaction, message_models.MessageInteraction)

assert message.components == [entity_factory_impl.deserialize_component(action_row_payload)]
assert message.components == [entity_factory_impl._deserialize_component(action_row_payload)]

def test_deserialize_message_with_unset_sub_fields(self, entity_factory_impl, message_payload):
del message_payload["application"]["cover_image"]
Expand Down