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

msgspec.Struct as dict keys and Dict[TypeVar, TypeVar] support #718

Open
u3Izx9ql7vW4 opened this issue Aug 14, 2024 · 0 comments
Open

msgspec.Struct as dict keys and Dict[TypeVar, TypeVar] support #718

u3Izx9ql7vW4 opened this issue Aug 14, 2024 · 0 comments

Comments

@u3Izx9ql7vW4
Copy link

u3Izx9ql7vW4 commented Aug 14, 2024

Question

I made a comment on a closed issue (#568) and I'm not sure it got any visibility so I'm creating a new one instead.

Here's a copy-pasta of my comment:

I got a chance to look at the fix (#602) and the corresponding tests. I copied your test below and made Custom inherit msgspec.Struct. Encoding still works but now decoding throws the following error

msgspec.ValidationError: Expected `object`, got `str` - at `key` in `$`

This is the code I used to generate the error (taken from your tests)

import msgspec
from typing import Dict

class Custom(msgspec.Struct):
    value : int

    def __hash__(self):
        return hash(self.value)

    def __eq__(self, other):
        return self.value == other.value

def dec_hook(typ, obj):
    if typ == Custom:
        return Custom(obj)
    raise NotImplementedError

msg = msgspec.json.encode(
    {Custom("a"): 1, Custom("b"): 2}, enc_hook=lambda x: x.value
)
assert msg == b'{"a":1,"b":2}'

obj = msgspec.json.decode(msg, type=Dict[Custom, int], dec_hook=dec_hook)
assert obj == {Custom("a"): 1, Custom("b"): 2}

Is this expected behavior? Would it be difficult to support msgspec Structs as dictionary keys?

Another thing I found is using msgspec.*.decode(dec_hook=..., type=Dict[TypeVar('A') TypeVar('B')] did not work for me. Namely I wrote a stub dec_hook function that printed 'hello world' and it never triggered on decoding.

Maybe I was doing something wrong, but was wondering if this is supported at all.

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