Skip to content
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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

mgaitan
Copy link

@mgaitan mgaitan commented Jun 22, 2022

As we already support lazy imports in subfactories, it seems natural to support it in Meta.model

@mgaitan mgaitan changed the title support fully a qualify path string as Meta.model support a fully qualified path string as Meta.model Jun 22, 2022
tests/test_utils.py Outdated Show resolved Hide resolved
tests/test_utils.py Outdated Show resolved Hide resolved
tests/test_base.py Outdated Show resolved Hide resolved
factory/utils.py Outdated Show resolved Hide resolved
Copy link
Member

@francoisfreitag francoisfreitag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

docs/reference.rst Outdated Show resolved Hide resolved
@@ -16,6 +16,14 @@ def import_object(module_name, attribute_name):
return getattr(module, attribute_name)


def resolve_type(type_or_path):
Copy link
Member

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with self.assertRaises(AttributeError):
with self.assertRaisesRegex(AttributeError, r"^module 'datetime' has no attribute 'foo'$"):

@francoisfreitag
Copy link
Member

Please add a changelog entry.

Co-authored-by: François Freitag <[email protected]>
@rbarrois
Copy link
Member

rbarrois commented Jul 7, 2022

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? :)

@mgaitan
Copy link
Author

mgaitan commented Jul 9, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants