Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically generated factories don't inherit the overridden get_provider_map #23

Closed
jenniferplusplus opened this issue Jan 8, 2022 · 2 comments

Comments

@jenniferplusplus
Copy link

Issue

I'm trying to generate some deeply nested models, and somewhere in the tree are fields defined as Optional[Exception]. Pydantic Factories doesn't have a handler for Exception. I'm not sure if that's intentional, but it creates a problem for me. I tried to override get_provider_map, but this didn't help because the field is in a nested model.

When a factory dynamically generates a new ModelFactory, the generated factory doesn't use the parent's overridden class methods. This makes it difficult or impossible to influence how those models are generated and resolve any errors in the process.

I was able to work around the issue by also overriding create_factory, so that generated factories will inherit from their parent in the hierarchy. If this is the preferred solution, then it would be great to have that documented. Otherwise, it would be good to have an option to enable that inheritance or otherwise pass on the top level get_provider_map.

Also, is it intentional that there's no provider for Exception?

Anyway, thank you this library is great and it's going to make my tests much more maintainable.

Workaround

class RootFactory(ModelFactory):
    __model__ = RootModel

    @classmethod
    def get_provider_map(cls) -> Dict[Any, Callable]:
        provider_map = super().get_provider_map()
        provider_map.update({Exception: Exception})
        return provider_map

    @classmethod
    def create_factory(cls, model, base=None, **kwargs):
        return super().create_factory(model, cls, **kwargs)
@Goldziher
Copy link
Contributor

Goldziher commented Jan 8, 2022

Hi @jenniferplusplus ,

Thanks for the detailed issue.

It's not by intention that there are no providers for exceptions, I just didn't think of this.

Its probably a good idea to add all builtin exceptions to the providers map... what do you think?

As for inheriting from the parent factory, I'd classify this as a bug - it should be the base behaviour.

Would you like to contribute a PR for this or should @lindycoder and myself handle this?

@Goldziher
Copy link
Contributor

v1.2.0 is released addressing this ticket. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants