-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support a fully qualified path string as Meta.model #944
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: Javier Buzzi <[email protected]>
Co-authored-by: Javier Buzzi <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good!
@@ -16,6 +16,14 @@ def import_object(module_name, attribute_name): | |||
return getattr(module, attribute_name) | |||
|
|||
|
|||
def resolve_type(type_or_path): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolve_type
is always called with an str
. Can simplify the function.
utils.resolve_type('datetime.foo') | ||
|
||
def test_invalid_module(self): | ||
with self.assertRaises(ImportError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with self.assertRaises(ImportError): | |
with self.assertRaisesRegex(ImportError, r"^No module named 'this-is-an-invalid-module'$"): |
self.assertEqual(datetime.date, imported) | ||
|
||
def test_unknown_attribute(self): | ||
with self.assertRaises(AttributeError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with self.assertRaises(AttributeError): | |
with self.assertRaisesRegex(AttributeError, r"^module 'datetime' has no attribute 'foo'$"): |
Please add a changelog entry. |
Co-authored-by: François Freitag <[email protected]>
Hey, thanks for the idea! However, I'm not so sure about the design of this feature — dotted names in subfactories is only supported to avoid circular references; overall, I feel that "explicit is better than implicit" is an important notion in Python, thus one should use explicit references rather than dynamically constructed ones. Using lazily evaluated imports tends to trigger unexpected issues, which would only appear when calling a specific factory, instead of at import time. What is the actual advantage in adding this feature? :) |
Hi @rbarrois, thank you for checking this out. The idea comes from this experiment https://gist.github.com/mgaitan/89afa174ff178a05f9d6a9990aff87dc ; I consider factory-boy is much more powerful than a "test fixture replacement". For instance, we are using it as the underlying tool to create synthetic data as a starting point for development and demos (outside test context). Moreover, as I explain in the notebook I considered it a powerful (yet simple, pure python defined) dependency injection framework. However, to make it really usable for these alternative use cases, it would very beneficial to support this feature. An application that follow an Inversion of control paradigm using dependency injections are in general quite big and complex, and the declarations of them as factories would require to import every target "model" (with the potential side effects of imports) even when not all the services are required at the same time. |
As we already support lazy imports in subfactories, it seems natural to support it in
Meta.model