You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
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:
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.
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:
ElementFactory - success: ConcreteElement(value=-59956609021.1241)
BrokenCompositeFactory - success: Composite(element=<__main__.ElementFactory object at 0x7737bd613d70>)
CompositeFactory - success: Composite(element=ConcreteElement(value=9781733.7957184))
ContainerFactory - failed: TypeError("Can't instantiate abstract class Element without an implementation for abstract method 'do_something'")
UseContainerFactory - success: Container(elements=ConcreteElement(value=7977535438.98355))
ProviderMapContainerFactory - failed: TypeError("Can't instantiate abstract class Element without an implementation for abstract method 'do_something'")
TypeOverrideContainerFactory - failed: TypeError("Can't instantiate abstract class Element without an implementation for abstract method 'do_something'")
MethodContainerFactory - success: Container(elements={'foo': ConcreteElement(value=95.2503692953739)})
Registering ElementFactory for Element type...
ContainerFactory - success: Container(elements={'ZvjYPTTthGmsgTIHBgRa': ConcreteElement(value=6.12086637877049)})
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
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