Open ErikvdVen opened 5 months ago
I also figured it fails when providing a certain size:
class HouseHoldFactory(ModelFactory[HouseHold]):
familymembers = Use(FamilyMemberFactory, size=10)
This also tries to generate a list and fails. For now I fixed it this way:
class HouseHoldFactory(ModelFactory[HouseHold]):
@classmethod
def familymembers(cls) -> dict[str, FamilyMemberFactory]:
familymembers = FamilyMemberFactory.batch(size=10)
return {familymember.id: familymember for familymember in familymembers}
Summary
As mentioned on the Discord channel, below code will result in a Pydantic validation error. If I turn households into lists (as well as in the defaults dict as in the HousHolds class) it all works!! But using dict[str, HouseHold] as typehint, does not work out.
As a maintainer
g...
pointed out: This is something we just don't handle in polyfactory currently. When given a dictionary for households, then it's assumed that the value you've given is what should be used. That means we end up not creating instances of HouseHold, but just pass in the raw dictionary and pydantic complains.Currently we do support Sequence[SomeModel, but no other type is supported. I think supporting things like Mapping[str, SomeModel] is going to be somewhat complex though I'm not a 100% sure.
The code:
Just running the build method without any defaults works, but as you can see, the
relatives
key of theFamilyMember
class should only contain a Household id and a Familymember id that exist (ie. an id of a Household that contains a Familymember with a certain id). So there are dependencies, that's the reason of this whole dictionary of necessary defaults.It does saves us from having to provide all the other fields, as in practice these classes could contain a ton of extra fields, which don't have dependencies, so Polyfactory could easily fake those for us.
Basic Example
Maintainer
g...
provided a basic solution for now:So that should work out or I could override the build method, was his suggestion.
What I think would be ideal is:
So the
ModelFactory
knows when it hits the key households, it actually knows how to handle that object (the keys should be a string and the values should be parsed with thatHouseHoldFactory
class).Drawbacks and Impact
No response
Unresolved questions
No response