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

@register decorator is incompatible with circular imports #82

Open
jcushman opened this issue Oct 3, 2019 · 0 comments
Open

@register decorator is incompatible with circular imports #82

jcushman opened this issue Oct 3, 2019 · 0 comments

Comments

@jcushman
Copy link

jcushman commented Oct 3, 2019

Factoryboy documents the creation of circular dependencies like this:

class UserFactory(factory.Factory):
    class Meta:
        model = User

    username = 'john'
    main_group = factory.SubFactory('users.factories.GroupFactory')

class GroupFactory(factory.Factory):
    class Meta:
        model = Group

    name = "MyGroup"
    owner = factory.SubFactory(UserFactory)

That example will not work with the @register decorator:

@register
class UserFactory(factory.Factory):
    class Meta:
        model = User

    username = 'john'
    main_group = factory.SubFactory('users.factories.GroupFactory')

@register
class GroupFactory(factory.Factory):
    class Meta:
        model = Group

    name = "MyGroup"
    owner = factory.SubFactory(UserFactory)

Throws:

ImportError while loading conftest '/app/_python/conftest.py'.
conftest.py:58: in <module>
    class UserFactory(factory.Factory):
/usr/local/lib/python3.5/site-packages/pytest_factoryboy/fixture.py:91: in register
    subfactory_class = value.get_factory()
/usr/local/lib/python3.5/site-packages/factory/declarations.py:647: in get_factory
    return self.factory_wrapper.get()
/usr/local/lib/python3.5/site-packages/factory/declarations.py:363: in get
    self.name,
/usr/local/lib/python3.5/site-packages/factory/utils.py:20: in import_object
    return getattr(module, str(attribute_name))
E   AttributeError: module 'conftest' has no attribute 'GroupFactory'

The issue seems to be that @register resolves the imports when applied, before the later classes are defined. So a workaround is to use the non-decorator version at the end of the file. This works:

# same as first code block, then ...
register(UserFactory)
register(GroupFactory)

Ideally the decorator form could Just Work, by delaying resolution of the imports until needed. Alternatively maybe the problem could be documented, and register() could detect what's happening and throw a more useful error?

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

No branches or pull requests

1 participant