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

Make it easier to extend the supported attribute types #33

Closed
lyz-code opened this issue Mar 4, 2022 · 3 comments
Closed

Make it easier to extend the supported attribute types #33

lyz-code opened this issue Mar 4, 2022 · 3 comments

Comments

@lyz-code
Copy link
Contributor

lyz-code commented Mar 4, 2022

As a user, I want to easily extend the available mappers of the pydantic model attribute types, for example, if there is a class that has an attribute type not supported by pydantic-factories and involves third-party libraries like this case, the extension of the mappers should be done on the user side.

Right now to do this the user has to subclass ModelFactory, override the method and extend the base value. This has the downsides that:

  • As the returned value of get_provider_map is generated on the return statement, to override the method the user has to copy and paste the whole dictionary potentially being outdated on future pydantic-factories releases.
  • It's not documented how to do this.
  • It's not easy for the user to do all this.

What I'm proposing is that ModelFactory has an internal attribute settable by the user where she can define the additional mappers, for example to give support to FakeDateTime the user's code would be:

from freezegun.api import FakeDatetime

class CustomModelFactory(ModelFactory):
    additional_mappers = { FakeDateTime: cls._get_faker().date_time_between }

We'll have to tweak get_provider_map so that it merges the default mapper dictionary with the user's

@Goldziher
Copy link
Contributor

I have no problem with this proposal you're welcome to add a PR. Please note the following though:

  1. we use the dunder pattern for factory variables, i.e. __additional_mappers__ instead of additional_mappers
  2. you could contribute documentation regarding how to subclass the factory, which will resolve the issue
  3. you can also subclass the method get_mock_value

@lyz-code
Copy link
Contributor Author

lyz-code commented Mar 4, 2022

Mocking get_mock_value makes more sense, I'll add the docs once I've got a working example, thanks!

@lyz-code
Copy link
Contributor Author

lyz-code commented Mar 4, 2022

Done!

@lyz-code lyz-code closed this as completed Mar 4, 2022
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