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

typing.Annotated not working as expected. #667

Open
MPlatypus opened this issue Apr 18, 2024 · 0 comments
Open

typing.Annotated not working as expected. #667

MPlatypus opened this issue Apr 18, 2024 · 0 comments

Comments

@MPlatypus
Copy link

Question

Basically, I am trying to set the alias of the Struct field, via typing.Annotated[], however, the following code fails.

from __future__ import annotations

import msgspec
import hikari
import json
import typing as t


class PayloadBase(msgspec.Struct):
    @classmethod
    def _from_payload(cls, payload: str) -> t.Self:
        return msgspec.json.decode(payload, type=cls, strict=False, dec_hook=cls._decode_hook)

    @property
    def _to_payload(self) -> str:
        return msgspec.json.encode(self, enc_hook=self._encode_hook).decode()

    @classmethod
    def _decode_hook(cls, type: t.Type[t.Any], obj: t.Any) -> t.Any:
        if type == hikari.Snowflake:
            return hikari.Snowflake(int(obj))
        raise ValueError("Sorry, but this value does not exist.")

    @classmethod
    def _encode_hook(cls, type: t.Type[hikari.Snowflake]) -> t.Any:
        return str(type)


class Ready(PayloadBase):
    session_id: str = msgspec.field(name="sessionId")
    resumed: bool = msgspec.field()
    guild_id: t.Annotated[hikari.Snowflake, msgspec.field(name="guildId")]


bot = hikari.GatewayBot("...", banner=None, suppress_optimization_warning=True)

payload = {
    "sessionId":"beanos",
    "resumed":True,
    "guildId":"1234567890"
}

ready = Ready._from_payload(json.dumps(payload))

print(ready.guild_id, type(ready.guild_id))

print(ready.session_id)
assert ready.session_id == "beanos"
assert ready.resumed is True
assert ready.guild_id == hikari.Snowflake(1234567890)

It decodes the session properly, however, it does not decode the guild properly, and fails on it, because its using t.Annotated. Am I doing something wrong, or is this expected behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant