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

The flag allow_population_by_field_name is not recognized #38

Closed
islam-aymann opened this issue May 3, 2022 · 14 comments
Closed

The flag allow_population_by_field_name is not recognized #38

islam-aymann opened this issue May 3, 2022 · 14 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@islam-aymann
Copy link

I have two models User, Review and their ModelFactorys.

from pydantic import BaseModel, Field

from pydantic_factories import ModelFactory


class User(BaseModel):
    login: str


class Review(BaseModel):
    body: str
    author: User = Field(alias="user")

    class Config:
        allow_population_by_field_name = True


class UserFactory(ModelFactory):
    __model__ = User


class ReviewFactory(ModelFactory):
    __model__ = Review


if __name__ == '__main__':
    author: User = UserFactory.build(login="me")

    review: Review = ReviewFactory.build(author=author)
    assert id(author) != id(review.author)
    assert review.author.login != author.login

    review: Review = ReviewFactory.build(user=author)
    assert id(author) != id(review.author)  # 🙄 why?
    assert review.author.login == author.login

*note: all assertion are successful

Review model has the allow_population_by_field_name flag set to True which means
Review model should be able to accept both author and user attributes to
populate the User model, however it's not recognized on building the instance and
new instance gets created.

I also noticed that new object was created on supplying a valid User instance to the ReviewFactory!!
see the why line

@Goldziher
Copy link
Contributor

Well, I wouldn't expect this to work - it doesn't exist :). Also, not in the docsö.

What is this flag supposed to achieve exactly?

@islam-aymann
Copy link
Author

The flag gives the Model the ability to accept the data in both the field name and the alias.

so you can do:

data = {
    "body": "lorem ipsum ..."
    "author": {
        "login": "username",
        ...
    }
}

obj = Review(**data)

same as:

data = {
    "body": "lorem ipsum ..."
    "user": {
        "login": "username",
        ...
    }
}

obj = Review(**data)

@Goldziher
Copy link
Contributor

oh, you mean the pydantic config. I see. So, this library doesn't read the pydantic model config, but rather the configuration of individual fields.

I'm not sure how desirable this behaviour is - @lindycoder , thoughts?

@islam-main how would you suggest implementing this?

@lindycoder
Copy link
Contributor

lindycoder commented May 3, 2022

Yes i'm missing this behaviour actually, wanted to propose an update but i'm short on time.
i was thinking a __by_alias__. true/false that fits the contract of pydantic's .dict(by_alias=False) and .json(by_alias=False) functions... and so .build() would either use the alias or the field name, i don't see a point in supporting both in the same .build() call

Would default to True for backward compatibility

That new config would turn off this if
https://github.com/Goldziher/pydantic-factories/blob/main/pydantic_factories/factory.py#L489

            if model_field.alias:
                field_name = model_field.alias

@Goldziher
Copy link
Contributor

Ok. Then a PR is welcome here 😃. I'll add the label.

@Goldziher Goldziher added help wanted Extra attention is needed good first issue Good for newcomers enhancement New feature or request labels May 3, 2022
@islam-aymann
Copy link
Author

@sondrelg @Goldziher Why wouldn't we support both (name and alias) and implement the same behavior of Pydantic itself?
and I guess maybe we should extract the flag from the Config class, not letting the user to duplicate information?

@lindycoder
Copy link
Contributor

It's true that if you try to make the factory work on field name but the model doesn't have the flag then it will fail. That's a good argument for reading the model's config.

@Goldziher
Copy link
Contributor

Implementation should support both I guess. I mean, both the field name and alias.

@islam-aymann
Copy link
Author

I am also short on time but I think I might be able to open a PR in a week or something to initiate Pydantic Model Config support.

@islam-aymann
Copy link
Author

I'll start with this issue flag allow_population_by_field_name as it's very popular on GitHub.

@mrkovalchuk
Copy link

mrkovalchuk commented May 18, 2022

@islam-main @Goldziher #43 this is it?

I have the same issues. If this PR looks fine, I would be glad to merge it as soon as possible

@islam-aymann
Copy link
Author

@mrkovalchuk Yes it's, I'll check it.

@Goldziher
Copy link
Contributor

Thank you for contributing. There are comments on the PR.

@Goldziher
Copy link
Contributor

Thanks a lot for contributing @mrkovalchuk - v1.2.9 is been released with your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants