WrongTypeError: should be "float" instead of "int"
See original GitHub issueIf you run this code:
import dataclasses
from dataclasses import dataclass
import dacite
@dataclass
class Person:
height: float = 160
person = Person()
person_dict = dataclasses.asdict(person)
new_person_1 = dacite.from_dict(data_class=Person, data=person_dict)
it gives this error:
WrongTypeError: wrong type for field "height" - should be "float" instead of "int"
I think it should be able to safely cast what it interprets as ints to be floats. Or alternatively, perhaps when doing asdict
, it should save it as a float 160.0
. Not sure which is better, or if the current behavior is desired since the user kind of erred with their datatype (although seems a bit user-unfriendly).
BTW, two “workarounds”:
- change a line above to
height: float = 160.0
or cast to float - change the final line to:
new_person_2 = dacite.from_dict(data_class=Person, data=person_dict,
config=dacite.Config({float: float}))
Happy to submit a PR if you like.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Type error in python/pygame - float instead of integer
PADDING , it's returning a float; that operator would normally return an int, but it still returns a float if you're dividing a...
Read more >Lesson 04: Python Integers and Floats
If your number has a decimal, Python will automatically consider it a float. If it does not, it will automatically consider it an...
Read more >Data Types and Type Conversion
Use built-in functions to convert between integers, floating point numbers, and strings. Every value has a type. Every value in a program has...
Read more >Decimal fixed point and floating point arithmetic
Construct a new Decimal object based from value. value can be an integer, string, tuple, float , or another Decimal object. If no...
Read more >How to Convert Floats to Integers in Pandas DataFrame
You can then use astype(int) in order to convert the floats to integers: ... you can do it instead on a DataFrame level...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi! Thank you @Garrett-R for reporting issue and also than you @tgpfeiffer for comprehensive response.
I would say that we should leave it as it is. If
isinstance(6, float)
returnsFalse
it means thatdacite
should also raise exception.Solutions:
Union[int, float]
Number
fromnumbers
moduleI have the same issue; I’m reading from a config file into a dict and then converting to a dataclass, which currently requires
Union[int, float]
everywhere because a value like6
isn’t recognized a validfloat
value.According to mypy,
x: float = 6
is valid, butisinstance(6, float)
is false, so at least it is not obvious what is the right behavior here.One concern I have is that while it is currently easy to work around this issue (using either
Union
or some explicit converter), if we change the behavior to implicitly accept ints as floats, then it is not possible any more to say “I actually do require a float here, not an integer”. Do you have any opinion, @konradhalas?