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

Dacite will put an int into an Optional[float] field but not a float | None field. #245

Open
aquirdTurtle opened this issue Oct 4, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@aquirdTurtle
Copy link

Summary
Apologies if this is logged somewhere else. Dacite will put an int into an Optional[float] field but not a float | None field.
...
To Reproduce

from typing import Optional
from dacite.core import from_dict
from dataclasses import dataclass

@dataclass
class foo:
    bar: Optional[float]

@dataclass
class zen:
    zoo: float | None

print(from_dict(foo, dict(bar=5))) # foo(bar=5)
print(from_dict(zen, dict(zoo=5))) # error

Where the error from the second print statement is

Traceback (most recent call last):
  File "/home/mbrown/code/abraxas/local/t.py", line 51, in <module>
    print(from_dict(zen, dict(zoo=5)))
  File "/home/mbrown/miniconda3/envs/abraxas-env/lib/python3.10/site-packages/dacite/core.py", line 68, in from_dict
    raise WrongTypeError(field_path=field.name, field_type=field.type, value=value)
dacite.exceptions.WrongTypeError: wrong value type for field "zoo" - should be "float | None" instead of value "5" of type "int"

This seems to be because of dacite's implementation of is_instance not being consistent with isinstance

from dacite.typing import is_instance
print(isinstance(5, float | None)) # false
print(isinstance(5, Optional[float])) # false
print(is_instance(5, float | None)) # false
print(is_instance(5, Optional[float])) # true

...

Expected behavior
Probably both should fail. But at least they should be consistent.
...

Environment

  • Python version: 3.10.10
  • dacite version: 1.8.1
@aquirdTurtle aquirdTurtle added the bug Something isn't working label Oct 4, 2023
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

1 participant