-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
model_dump
should serialise bools as bools for json
#7294
Comments
|
Hi @Booplicate, Thanks very much for reporting this. I can confirm that this is a bug. This issue is a duplicate of the #6830 bug. We're working on fixing this issue in The issue is actually with the wrong type being selected when serializing a See the example below: from pydantic import BaseModel, StrictBool, StrictFloat, StrictInt
class SomeModel(BaseModel):
value: StrictFloat | StrictInt | StrictBool
class BoolModel(BaseModel):
value: StrictBool
m1 = SomeModel(value=True)
m2 = BoolModel(value=True)
# Passes
assert type(m1.model_dump(mode="python")["value"]) is bool
# This will fail
assert type(m1.model_dump(mode="json")["value"]) is bool, "didn't serialise as bool"
# Passes
assert type(m2.model_dump(mode="python")["value"]) is bool
# Passes
assert type(m2.model_dump(mode="json")["value"]) is bool I'm going to close this issue given that it's a duplicate of #6830, but we will include a test case like this one in our test suite once we fix this bug. |
Good to know, thank you. |
Hello, is that fixed ? I'm having the issue with See below: class MyModel(BaseModel):
value: str | float | int | bool
m1 = MyModel(value=True)
# fails
assert type(m1.model_dump(mode="json")["value"]) is bool NB : the ordering of types seems to be a fix for the issue see below class MyModel(BaseModel):
value: str | bool | float | int
m1 = MyModel(value=True)
# passes
assert type(m1.model_dump(mode="json")["value"]) is bool |
You're right, this is still an issue |
@sydney-runkle Was this fixed with pydantic/pydantic-core#1398 ? I can't figure it out from the test case in that PR as it doesn't test |
Ah @dexterionut, yep! This is fixed by that PR, great call! |
Initial Checks
Description
Currently when dumping model to json, objects of type
bool
are being serialised as integers in json. I think this is a bug because json natively supports booleans (true
/false
).Example Code
Python, Pydantic & OS Version
Selected Assignee: @davidhewitt
The text was updated successfully, but these errors were encountered: