-
Notifications
You must be signed in to change notification settings - Fork 309
Closed
Labels
Description
Noticed this issue when working with documents from MongoDB (using motor
).
It returns integers as BSON types, which are subclasses of the int
type.
Minimal reproduction examples: (pydantic 2.2.1)
from pydantic import BaseModel
class MyInt(int):
...
class Test(BaseModel):
id: int
x = 1046862536621953054
a = Test(id=MyInt(x))
print(a.id)
assert x == a.id # Fails, a.id is equal to 1046862536621953024
from pydantic import BaseModel, ConfigDict
class MyInt(int):
...
class Test(BaseModel):
model_config = ConfigDict(arbitrary_types_allowed=True)
id: MyInt
x = 1046862536621953054
a = Test(id=MyInt(x))
print(a.id)
assert x == a.id # Passes
from pydantic import BaseModel
class MyInt(int):
...
class Test(BaseModel):
id: int
x = 1046862536621953054
a = Test(id=int(MyInt(x)))
print(a.id)
assert x == a.id # Passes
The first example should either not allow passing the type in, or it should get the value correctly (same behaviour as in v1).
Selected Assignee: @samuelcolvin
pydantic-hooky