Hi,
I've been thinking of some improvements for the way factory_boy handles
declarations; and have written a proposal for a possible implementation.
This would introspect declaration's arguments to automatically detect
nested
declarations:
@factory.lazy_attribute
def email(self, domain=factory.Faker('domain'), rank=factory.Sequence()):
"""Generate a guaranteed unique domain name by prepending dXX-"""
return '{}@d{}-{}'.format(self.username, rank, domain)
In the above example, we would automatically detect that `domain` and
`rank`
are factory declarations, and evaluate them as such — like one would with
a subfactory.
Without this, users would have to add an extra parameter to construct the
`domain` field with Faker, even if it's only useful inside a declaration.
Before going through with this plan, I'd like to collect some feedback from
the community:
- Would such a feature be useful in your codebases?
- Have you used other systems which also used introspection? Did you meet
unexpected issues?
- Do you see possible improvements to the API for this feature?
I've also opened an issue on the project's Github:
https://github.com/FactoryBoy/factory_boy/issues/774
Feel free to comment there or to reply to this message; I'll coordinate
replies in both directions.
Thanks,
--
Raphaël Barrois