Skip to content

How to disable randomness in polyfactory? #460

Answered by Alc-Alc
AndreiTapai asked this question in Q&A
Discussion options

You must be logged in to vote

To answer the question in your title

How to disable randomness in polyfactory?

__random_seed__

To answer the question in your post

I would like values to be generated all the time for all fields. Is there a straightforward way to make this happen?

__allow_none_optionals__

You can combine these and it would ensure you always get the same non optional values

from dataclasses import dataclass
from polyfactory.factories import DataclassFactory


@dataclass
class Person:
    name: str | None


class PersonFactory(DataclassFactory[Person]):
    __model__ = Person
    __random_seed__ = 1
    __allow_none_optionals__ = False


person = PersonFactory.build()
print(person.name)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by JacobCoffee
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants