Correct way to generate dynamic factories based on Input #188
Unanswered
dmsfabiano
asked this question in
Q&A
Replies: 2 comments 3 replies
-
Hi. You can override the factory's |
Beta Was this translation helpful? Give feedback.
1 reply
-
So, something like this from typing import Generic
from polyfactory.factories.pydantic_factory import ModelFactory, T
class MyModelFactory(ModelFactory[T], Generic[T]):
__base_factory__ = True
@classmethod
def process_kwargs(cls, **kwargs: Any) -> dict[str, Any]:
if inner_param := getattr(cls.__model__, "inner_param", None):
# do something here
super().process_kwargs(**kwargs) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
First of all, love polyfactory. I had a major pain point with factory_boy, where it's difficult to generate different objects based on parameters, and after reading the polyfactory docs, i couldn't find anythign on how to laverage this.
My use case is simple:
I have a pydantic base model that all it does is to ensure that that it has a class method defined, which is a dict version of the model (the reason why I am not just using
pydantic_model.dict
is outside of the scope of polyfactory, I have a few issues on pydantic repo that are pending fixes)Now, all I want to do is generated ModelIO objects that have one field, the field can be either a list of intergers, a random dataframe, or numpy array of integers.
On factory_boy, I would have to do something like this
after this, I would have to make specific
ModelIO
classes for each of this scenarios and after that, i would have to make a factory for all of those 3 🤯 mainly because factory_boy doesnt allow easily to set theclass Meta.model
dynamicallymy dream scenario would be to do something like
factory(native=true)
which would generateModelIO
object where the field is a list or something likefactory(cls_type=ndarray)
This is an over simplified example of my use case, but it pretty much applies to any dynamically created classes/objects from an abstract class that validates certain requirements.
I am more than happy to contribute with an example in the documentation after I understand a bit more if polyfactory has some tools to handle these scenarios
Beta Was this translation helpful? Give feedback.
All reactions