Replies: 1 comment
-
This should be solved in next release with this PR #628 then using the provider map attempt. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
I have an attrs class that contains a
dict
of other classes, but the annotated type is an abstract base class. How can I tell Polyfactory that it should use a specific sub-class factory for generating the elements of that dict?Details
Structures
Given this simplified data structure code:
Easy Factories
The factories for the first two concrete classes can be easily defined:
Bad Solutions
But I don't see any good way to define a Factory for the third class. So I've tried various things, but the only two working solutions that I could find are as follows:
DIY Factory
This method completely looses any flexibility that Polyfactory offers, no just defining collection length unless it is re-implemented every time:
Writing to Protected Properties
This method works like a charm, but it feels wrong to use undocumented class-fields that might just change in between patch versions as it is not part of the official API.
Default Factories
Looking at the code it seems that there is currently no official way besides
__set_as_default_factory_for_type__
to define a factory as a default factory, not even for super-types.__set_as_default_factory_for_type__
does only accept booleans and sets the default factory only for the exact type of the model and nothing else:How is this case to be handled? Did I miss anything? What would you recommend me to to?
Full Test Code & Example Output
Beta Was this translation helpful? Give feedback.
All reactions