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

Bug: Pydantic Config field min_anystr_length not taken into account #442

Open
1 of 4 tasks
Leobouloc opened this issue Nov 22, 2023 · 3 comments
Open
1 of 4 tasks
Labels
bug Something isn't working

Comments

@Leobouloc
Copy link

Leobouloc commented Nov 22, 2023

Description

In a Pydantic model, when using Config field min_anystr_length (to set a minimum length for all strings in the model), Polyfactory does not seem to take it into account which leads to pydantic validation errors during build.

My understanding is that currently, Polyfactory only parses the allow_population_by_field_name (here) from the Config.

Is this assessment correct ? Thank you for your help !

URL to code causing the issue

No response

MCVE

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

ModelFactory.seed_random(0)

class TestClass(BaseModel):
    class Config:
        min_anystr_length = 100
    myfield: str

class TestClassFactory(ModelFactory[TestClass]):
    __model__ = TestClass


class TestClassWithoutConfig(BaseModel):
    myfield: str

class TestClassWithoutConfigFactory(ModelFactory[TestClassWithoutConfig]):
    __model__ = TestClassWithoutConfig


print(TestClassWithoutConfigFactory.build()) # OK
print(TestClassFactory.build()) # ERROR

#E   pydantic.error_wrappers.ValidationError: 1 validation error for TestClass
#E   myfield
#E     ensure this value has at least 100 characters (type=value_error.any_str.min_length; #limit_value=100)

Steps to reproduce

Execute the example above (pydantic 1.10)

Screenshots

No response

Logs

E   pydantic.error_wrappers.ValidationError: 1 validation error for TestClass
E   myfield
E     ensure this value has at least 100 characters (type=value_error.any_str.min_length; limit_value=100)

Release Version

2.12.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

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 Litestar 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
@Leobouloc Leobouloc added the bug Something isn't working label Nov 22, 2023
@guacs
Copy link
Member

guacs commented Nov 22, 2023

@Leobouloc you're right. polyfactory does not currently parse the Config object for constraints and unfortunately, it seems pydantic doesn't inject any of the constraints into the model fields that we do parse :/

@Leobouloc
Copy link
Author

Could this be solved by adding min_length and max_length parameters to from_model_field and from_field_info and passing cls.__model__.__config__.min_anystr_length on to the constraints ? (I see that allow_population_by_field_name is already being parsed from from the model Config) ?

Are there some other constraints I am missing ?

@guacs
Copy link
Member

guacs commented Jan 21, 2024

I'm so sorry @Leobouloc, I had missed your question!

I think the right way to do this would be to for the from_model_field and from_field_info to consider the values in the config. I'm not sure about all the different constraints that can be set via the config, but all of them should be considered when creating the FieldMeta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants