Skip to content

Maximum value of int field does not reflect bytes used when subclassingΒ #913

@NiceAesth

Description

@NiceAesth

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions