#Set max for generating decimal

1 messages · Page 1 of 1 (latest)

lyric plank
#

Is there a way to set a maximum of decimal to be generated? Current polyfactory tries to generate huge decimals which lead to errors, because the stuff is attempted to import in Mongodb. I can't set a Field validator because the field supports strings as well

viscid sunBOT
#
Notes for Set max for generating decimal
At your assistance

@lyric plank

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

calm rapids
#

Yes you can. You'll have to override the get_provider_map to change the default function that gives a random Decimal instance. Here's an example:

from __future__ import annotations

from decimal import Decimal
from typing import Any, Callable

from pydantic import BaseModel

from polyfactory.factories.pydantic_factory import ModelFactory
from polyfactory.value_generators.primitives import create_random_decimal


class Foo(BaseModel):
    decimal_value: Decimal


class FooFactory(ModelFactory[Foo]):
    @classmethod
    def get_provider_map(cls) -> dict[Any, Callable[[], Any]]:
        map = super().get_provider_map()
        # You can use your own function that provides a Decimal value if you need.
        # Or you can use the one that polyfactory uses.
        map[Decimal] = lambda: create_random_decimal(cls.__random__, maximum=Decimal(10), minimum=Decimal(0))

        return map
#

The default is to get faker to produce the Decimal instance.

lyric plank
#

Awesome, thank you so much!