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

Expected typing.Mapping, got str instead #53

Open
callumPearce opened this issue Oct 11, 2023 · 0 comments
Open

Expected typing.Mapping, got str instead #53

callumPearce opened this issue Oct 11, 2023 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@callumPearce
Copy link

callumPearce commented Oct 11, 2023

from __future__ import annotations
from dataclasses import dataclass
from typing import Annotated
from databind.json import JsonConverter, load


@dataclass(frozen=True)
class Inner:
    one: str
    two: str

    def __str__(self) -> str:
        f"{self.one}.{self.two}"
 
   @classmethod
    def from_string(cls, val: str) -> Inner:
        vals = val.split(".")
        return Inner(one=vals[0], two=vals[1])


@dataclass(frozen=True)
class Outer:
    number: int
    inner: Annotated[
        Inner,
        JsonConverter.using_classmethods(
            Inner, serialize="__str__", deserialize="from_string"
        ),
    ]


dict = {"number": 3, "inner": "hello.world"}
loaded = load(dict, Outer)
print(loaded)

Expected Behaviour

Inner struct is parsed using from_string class method and print out:

MyClass(number=3, inner=Inner(one='hello', two='world'))

Actual Behaviour

databind.core.converter.ConversionError: expected typing.Mapping, got str instead

Trace:
    $: TypeHint(__main__.Outer)
    .inner: TypeHint(typing.Annotated[__main__.Inner, <databind.json.settings.JsonConverter object at 0x1034aec50>])
@NiklasRosenstein NiklasRosenstein added the bug Something isn't working label Mar 19, 2024
@NiklasRosenstein NiklasRosenstein self-assigned this Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants