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

Strange behavior when registering str -> float conversion #622

Open
FeldrinH opened this issue Jan 26, 2024 · 0 comments
Open

Strange behavior when registering str -> float conversion #622

FeldrinH opened this issue Jan 26, 2024 · 0 comments

Comments

@FeldrinH
Copy link

Perhaps I am abusing conversions for something that they are not intended to handle, but I encountered a behavior that I found suprising and which feels like a bug.

I was trying to support deserializing floats from both floats and strings. I registered a converter like this:

@apischema.deserializer
def to_float(value: str) -> float:
    return float(value)

And then tried this:

@dataclass
class Data:
    a: float
    b: float

result = apischema.deserialize(Data, {
    "a": 1.23,
    "b": "1.23",
})
print(result)

The result was, ValidationError: [{'loc': ['a'], 'err': 'expected type string, found number'}], which is a little suprising, because it seems that adding a converter from string to float has made deserializing a float to itself invalid.

PS: I also tried adding a converter from float to float. That caused RecursionError: maximum recursion depth exceeded.

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