Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Don't leave AttributeBuilder in an inconsistent state on exceptions #256
When one of the LazyValues raises an exception, don't leave its name in __pending stack of the AttributeBuilder, preventing evaluation of any other LazyValues.
The use case for this is: several declarations on a factory have to be in sync with each other, but can supply default values if needed. For example:
class UserFactory(factory.Factory): @factory.lazy_attribute def username(self): return self.email[:self.email.index('@')] @factory.lazy_attribute def email(self): try: return self.username + '@example.com' except factory.containers.CyclicDefinitionError: return 'email@example.com'
This allows any of the following:
UserFactory(username='jane') UserFactory(firstname.lastname@example.org') UserFactory()
Hmm, the idea is quite interesting!
However, I'd like to find a cleaner way to handle it than to rely on cyclic definition; I'll try to think of a cleaner API (maybe with some helper).
I'll keep this PR open for now, I hope to get some more time to think about this next week :)