question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WrongTypeError: should be "float" instead of "int"

See original GitHub issue

If 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”:

  1. change a line above to height: float = 160.0 or cast to float
  2. 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:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
konradhalascommented, Nov 27, 2019

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) returns False it means that dacite should also raise exception.

Solutions:

  • (as mentioned) use Union[int, float]
  • use Number from numbers module
1reaction
tgpfeiffercommented, Nov 25, 2019

I 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 like 6 isn’t recognized a valid float value.

According to mypy, x: float = 6 is valid, but isinstance(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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found