Numbers in pyfake
Simple Usage
pyfake supports generating fake data for integer (<int>), floating-point (<float>), and decimal (<Decimal>) fields in your Pydantic models. Here's a simple example:
from pyfake import fake
from pydantic import BaseModel
class Product(BaseModel):
id: int
price: float
result = fake(Product)
print(result)
That’s it.
No setup, no fixtures, no config files.
pyfake inspects your schema, understands each field, and generates valid data automatically.
Constraints & Customization
You can also specify constraints like gt, lt, ge, le for more control over the generated numbers.
For example:
from pydantic import Field
class Product(BaseModel):
id: int = Field(..., gt=0) # id must be greater than 0
price: float = Field(..., ge=0.0) # price must be non-negative
pyfake will respect these constraints when generating values.
Read more about this in the Metadata & Constraints section below.
Returning Multiple Values
By default, pyfake generates a single instance of your model. But you can easily generate multiple instances by passing the num parameter:
from pyfake import fake
from pydantic import BaseModel
class Product(BaseModel):
id: int
price: float
results = fake(Product, num=5)
print(results)
{'id': 77, 'price': 82.89752225478523},
{'id': 12, 'price': 45.12345678901234},
{'id': 34, 'price': 67.89012345678901},
{'id': 56, 'price': 23.45678901234567},
{'id': 89, 'price': 90.12345678901234}
]
Receiving Model Instances
By default, pyfake returns generated data as plain dictionaries. If you want to receive actual Pydantic model instances instead, set as_dict=False:
from pyfake import fake
from pydantic import BaseModel
class Product(BaseModel):
id: int
price: float
results = fake(Product, num=3, as_dict=False)
print(results)
Product(id=77, price=82.89752225478523),
Product(id=12, price=45.12345678901234),
Product(id=34, price=67.89012345678901)
]
Metadata & Constraints
Pydantic Field
pyfake automatically extracts metadata from your Pydantic model fields, including type annotations and constraints. This metadata is used internally to determine how to generate appropriate fake data for each field.
Read more about Field metadata in the Pydantic documentation.
Constraints can come from Field(...), from Annotated[...] metadata, or from pydantic's constrained types (conint, confloat, condecimal). All three are read the same way.
Default ranges
Without bounds, numbers fall in these ranges:
| Type | Default range |
|---|---|
int |
0 to 100 |
float |
0.0 to 100.0 |
Decimal |
0 to 100, with 2 decimal places |
A single bound inside the default range just narrows it: Field(ge=50) gives values from 50 to 100.
A single bound outside it moves the range next to the bound and keeps its width: Field(ge=500) gives values from 500 to 600, and Field(le=-5) gives values from -105 to -5.
Bounds that leave no valid value (for example ge=10, le=5) raise InvalidConstraints.
Using Field constraints
from pydantic import BaseModel, Field
class Product(BaseModel):
id: int = Field(..., gt=-4, lt=0) # id must be between -4 and 0 (exclusive)
price: float = Field(..., ge=3.0, le=7.0) # price must be between 3.0 and 7.0
results = fake(Product, num=1, as_dict=False)
print(results)
Supported Field options
| Option | Description |
|---|---|
gt |
Greater than: the generated value will be greater than this value. |
ge |
Greater than or equal to: the generated value will be greater than or equal to this value. |
lt |
Less than: the generated value will be less than this value. |
le |
Less than or equal to: the generated value will be less than or equal to this value. |
multiple_of |
The generated value will be a multiple of this value. For floats, the generated value will be a multiple of this value within a reasonable tolerance to account for floating-point precision issues. |
decimal_places |
For float and Decimal fields, the number of decimal places to generate. Decimal values have exactly this many places (2 by default). |
max_digits |
For Decimal fields, the maximum number of digits in total. pyfake uses fewer decimal places when needed to fit the integer part. |
Option support updates
The following notes describe current support for numeric field options:
multiple_of: Now supported forint,float, andDecimalfields. Forfloatfields, generated values will be multiples of the specified value within floating-point precision limits; small rounding differences may occur.decimal_places: Supported for bothfloatandDecimalfields. Forfloat, values are rounded to the requested number of decimal places, but floating-point representation may introduce minor differences.
Using multiple_of
Version
Support for multiple_of was added in version v0.0.9.
The multiple_of option can be used with int, float, and Decimal fields to request values that are multiples of a given number. Here are some examples (sample outputs shown; actual values will vary):
from pydantic import BaseModel, Field
class Item(BaseModel):
count: int = Field(..., ge=0, le=100, multiple_of=5)
results = fake(Item, num=3)
print(results)
[
{'count': 75},
{'count': 20},
{'count': 45}
]
from pydantic import BaseModel, Field
class Product(BaseModel):
expiry: float = Field(..., ge=0.0, le=10.0, multiple_of=0.5)
results = fake(Product, num=3)
print(results)
[
{'expiry': 3.5},
{'expiry': 0.5},
{'expiry': 8.0}
]
from decimal import Decimal
from pydantic import BaseModel, Field
class Price(BaseModel):
price: Decimal = Field(
..., ge=0, le=10, multiple_of=Decimal("0.25"), decimal_places=2
)
result = fake(Price)
print(result)
{'price': Decimal('5.25')}
Note: float results respect floating-point precision and may show small rounding differences. Decimal values will be exact multiples when using Decimal-typed multiple_of values.
Integer Types
Integers can be generated easily from Pydantic models, such as
from pydantic import BaseModel
class User(BaseModel):
id: int
age: int
result = fake(User)
print(result)
Using Field constraints
from pydantic import BaseModel, Field
class User(BaseModel):
id: int = Field(..., gt=0) # id must be greater than 0
age: int = Field(..., ge=18, le=99) # age must be between 18 and 99
score: int = Field(..., gt=-10, lt=100) # score must be between -10 and 100
result = fake(User)
print(result)
Float Types
Floats can also be generated from Pydantic models, such as
from pydantic import BaseModel
class Product(BaseModel):
price: float
height: float
width: float
result = fake(Product)
print(result)
'price': 88.24146807024337,
'height': 34.041684788330116,
'width': 46.732209700107404
}
Using Field constraints
from pydantic import BaseModel, Field
from typing import Annotated
class Product(BaseModel):
price: float = Field(..., ge=0.0) # price must be non-negative
height: float = Field(..., gt=0.0) # height must be greater than 0
width: float = Field(
..., gt=0.0, le=100.0
) # width must be greater than 0 and less than or equal to 100
expiry: Annotated[
float | None, Field(..., gt=0.0, lt=100.0)
] # optional; when set, between 0 and 100 (exclusive)
result = fake(Product)
print(result)
'price': 88.24146807024337,
'height': 34.041684788330116,
'width': 46.732209700107404,
'expiry': None
}
Decimal Types
Decimal values are generated as scaled integers, so they never carry float rounding noise. They have 2 decimal places unless decimal_places says otherwise, and they always fit max_digits:
from decimal import Decimal
from pydantic import BaseModel, Field, condecimal
from pyfake import fake
class Invoice(BaseModel):
subtotal: Decimal
tax_rate: Decimal = Field(..., ge=0, le=1, decimal_places=3)
total: condecimal(max_digits=6, decimal_places=2)
units: condecimal(max_digits=3, ge=500)
result = fake(Invoice)
print(result)
'subtotal': Decimal('74.11'),
'tax_rate': Decimal('0.886'),
'total': Decimal('91.71'),
'units': Decimal('599')
}
units gets no decimal places: with max_digits=3, a value of at least 500 has no room for any.
If no value can satisfy the constraints (e.g. max_digits=2 with ge=1000), pyfake raises InvalidConstraints.