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.convert seems to not call dec_hook for custom dict key #775

Open
the-forest-tree opened this issue Nov 21, 2024 · 0 comments
Open

Comments

@the-forest-tree
Copy link

Description

Hi,

I am not sure if this is really a bug it might be expected behavior depending of how different json.decode is from convert

The issue I am facing is that dec_hook is not called for a custom dict key when using convert

I refer to the tests suite that was added for this issue

from msgspec import convert, json

class Custom:
    def __init__(self, value):
        self.value = value

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

    def __eq__(self, other):
        return self.value == other.value
    
    def __repr__(self):
        return f"Custom({self.value})"

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

# This works as expected
json.decode('{"a": true, "b": false}', type=dict[Custom, bool], dec_hook=dec_hook)
# Output: {Custom(a): True, Custom(b): False}

# This does not return Custom as dict keys
convert(dict(a=True, b=False), dict[Custom, bool], dec_hook=dec_hook)
# Output: {'a': True, 'b': False}

# But works for dict values
convert(dict(a=True, b=False), dict[Custom, Custom], dec_hook=dec_hook)
# Output: {'a': Custom(True), 'b': Custom(False)}
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