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

Use defaults values from BaseModel in ModelFactory #466

Closed
ShtykovaAA opened this issue Dec 20, 2023 · 4 comments · Fixed by #472
Closed

Use defaults values from BaseModel in ModelFactory #466

ShtykovaAA opened this issue Dec 20, 2023 · 4 comments · Fixed by #472
Labels
enhancement New feature or request

Comments

@ShtykovaAA
Copy link

ShtykovaAA commented Dec 20, 2023

Summary

I think it would be great if ModelFactory can use defaults values from BaseModel

Basic Example

For example I have class AppSettings, where I set app_title as APP_TITLE by default

from pydantic import BaseModel, Field
from polyfactory.factories.pydantic_factory import ModelFactory

APP_TITLE = 'title'


class AppSettings(BaseModel):
    app_title: str = Field(APP_TITLE)

    some_another_field: int

And now I can specify app_title as APP_TITLE in ModelFactory if only I set it model

class AppSettingsFactory(ModelFactory):
    __model__ = AppSettings
    app_title = APP_TITLE

So I think it would be great to have an extra param for using defaults in ModelFactory. Maybe something like this

class AppSettingsFactory(ModelFactory):
    __model__ = AppSettings
    __use_defaults__ = True

Drawbacks and Impact

No response

Unresolved questions

No response


Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@ShtykovaAA ShtykovaAA added the enhancement New feature or request label Dec 20, 2023
@guacs
Copy link
Member

guacs commented Dec 21, 2023

@ShtykovaAA yeah this is definitely possible. The default values are parsed and stored when parsing the model already so this shouldn't be a difficult addition.

Just to clarify, when setting __use_defaults__ = True then the default value will be used for all the fields that have a default value or default factory. This is the expected behavior, correct?

@ShtykovaAA
Copy link
Author

@guacs correct!

@guacs
Copy link
Member

guacs commented Jan 4, 2024

@adhtruong am I right in thinking we don't have to worry about the defaults when it comes to the SQLAFactory since that would be handled by sqlalchemy itself?

@adhtruong
Copy link
Collaborator

@guacs SQLAlchemy has a couple forms on default

  1. https://docs.sqlalchemy.org/en/20/orm/mapping_api.html#sqlalchemy.orm.mapped_column.params.default an in memory default
  2. Or an insert default

In both cases, omitting the parameter itself should trigger using the default. Case 1 could be handled the same as above implementation but 2 is a bit more complex as may rely on a db (e.g. primary key with sequence).

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

Successfully merging a pull request may close this issue.

3 participants