Automatic conversion from float to int?
See original GitHub issueBug
For bugs/questions:
- OS: Any
- Python version
import sys; print(sys.version)
: 3.7 - Pydantic version
import pydantic; print(pydantic.VERSION)
: 0.18.1
You got a misfeature there, buddy. Automatic conversion (by default!) from float to int is a terrible idea:
from pydantic import PositiveInt, BaseModel
class MySchema(BaseModel):
int_only: PositiveInt
MySchema(int_only=3.1415)
# <MySchema int_only=3>
I am working with 2 separate codebases that communicate with each other. One of these apps could have the wrong idea about the type of a variable, it would be great if the validation framework caught this. I wanted to use Pydantic to validate the data. How can I circumvent this misfeature? (Please add the answer to the documentation.)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:28
- Comments:17 (7 by maintainers)
Top Results From Across the Web
Convert float to int implicit vs explicit (cast) discrepancy
To analyse this, the first job is to rewrite a += b as a = a + b . i + (int)f will...
Read more >How to convert Float to Int in Python? - GeeksforGeeks
Converting a float value to an int is done by Type conversion, which is an explicit method of converting an operand to a...
Read more >Implicit incompatible float to int conversion is deprecated
Converting a float number to an integer often involves losing the fractional value of the float number. For example, 6.8 , a float...
Read more >Implicit conversions - cppreference.com
A finite value of any real floating type can be implicitly converted to any integer type. Except where covered by boolean conversion above,...
Read more >XC8 implicit conversion of float to integer
Hello, I have used two options for changing float to integer of Cosine angle . But I got Warning as below: Warning [356]...
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 Free
Top 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
Wow, what a great way of not getting help.
I try really hard to understand peoples problems and always give constructive answers, but here I’m really tempted to just click close.
Do you always start conversations with volunteers where you would like help by calling them “buddy”, then telling them they have "terrible idea"s? Great idea buddy.
This is by considered design, but you can change the behaviour using validators with
pre=True
.Or you can use another library. Not a problem with me.
Dear Samuel,
I apologize. You are absolutely right that I was arrogant. It’s nothing personal. I repeat this mistake nearly every week and then I resolve to watch it and then suddenly it happens again. I seem unable to shake it, but I must not give up. I am not being sarcastic, I have struggled with this all my life. Therapy had no power to shadow my dad’s example. If it means anything, no offense was intended, but I spoke as if I already knew you, trying to be a little funny, and that’s the way it always happens.
Again, I am sorry and I hope you can forgive me. I did not really mean to talk down to you, although it totally appears that way. I truly admire your work in this library (one that I couldn’t have written), I think it is awesome, and this is why I am going to insist.
I still think that the default should be changed – we should not build a validation library that declines to validate types by default. (Python is a strongly typed, if dynamically typed, language.) In the case I brought to you, 3.14 is being converted to 3 by default, which is information loss, happening exactly when we wanted to prevent information loss.
At a minimum, a configuration option should be added to control this behavior.
I guess that’s the best case I can make for it.
Best regards, Nando.