Models/apps get imported twice: Conflicting 'model' models in application 'app': <class 'project.project.app.models.Model'> and <class 'app.models.Model'>. #75

Closed
blueyed opened this Issue Mar 16, 2014 · 3 comments

2 participants

@blueyed

I am not sure, if this is a bug in Django 1.7 (alpha, master), or a problem with pytest_django.

I am using django-configurations.

When calling py.test as follows, I get an exception (see title):

DJANGO_SETTINGS_MODULE=config.settings DJANGO_CONFIGURATION=Tests PYTHONPATH=. py.test --debug -s --ipdb tmm/movies/tests.py

It appears that setup() is called twice, and first the app/model is project.project.app.model, and the second time app.model.

I see two issues here: setup() being called twice, and the different naming of the same app/model.

This gets triggered/caused by from .models import Model in the test file.
(When I comment this line, another error pops up: " AttributeError: 'db.backends.dummy.base.DatabaseWrapper' object has no attribute 'Database' "
https://code.djangoproject.com/ticket/22279#ticket)

> …/django-master/django/apps/registry.py(201)register_model()
    200                 "Conflicting '%s' models in application '%s': %s and %s." %
--> 201                 (model_name, app_label, app_models[model_name], model))
    202         app_models[model_name] = model

ipdb> w
  /home/user/.virtualenvs/tmm/bin/py.test(9)<module>()
      6 
      7 if __name__ == '__main__':
      8     sys.exit(
----> 9         load_entry_point('pytest==2.5.3.dev1', 'console_scripts', 'py.test')()
     10     )

  …/pytest/_pytest/config.py(20)main()
     18     """
     19     config = _prepareconfig(args, plugins)
---> 20     return config.hook.pytest_cmdline_main(config=config)
     21 
     22 class cmdline:  # compatibility namespace

  …/pytest/_pytest/core.py(377)__call__()
    375     def __call__(self, **kwargs):
    376         methods = self.hookrelay._pm.listattr(self.name)
--> 377         return self._docall(methods, kwargs)
    378 
    379     def pcall(self, plugins, **kwargs):

  …/pytest/_pytest/core.py(388)_docall()
    386         mc = MultiCall(methods, kwargs, firstresult=self.firstresult)
    387         try:
--> 388             res = mc.execute()
    389             if res:
    390                 self.trace("finish", self.name, "-->", res)

  …/pytest/_pytest/core.py(289)execute()
    287             method = self.methods.pop()
    288             kwargs = self.getkwargs(method)
--> 289             res = method(**kwargs)
    290             if res is not None:
    291                 self.results.append(res)

  …/pytest/_pytest/main.py(112)pytest_cmdline_main()
    110 
    111 def pytest_cmdline_main(config):
--> 112     return wrap_session(config, _main)
    113 
    114 def _main(config, session):

  …/pytest/_pytest/main.py(81)wrap_session()
     79             config.hook.pytest_sessionstart(session=session)
     80             initstate = 2
---> 81             doit(config, session)
     82         except pytest.UsageError:
     83             args = sys.exc_info()[1].args

  …/pytest/_pytest/main.py(118)_main()
    116     running tests and reporting. """
    117     config.hook.pytest_collection(session=session)
--> 118     config.hook.pytest_runtestloop(session=session)
    119 
    120 def pytest_collection(session):

  …/pytest/_pytest/core.py(377)__call__()
    375     def __call__(self, **kwargs):
    376         methods = self.hookrelay._pm.listattr(self.name)
--> 377         return self._docall(methods, kwargs)
    378 
    379     def pcall(self, plugins, **kwargs):

  …/pytest/_pytest/core.py(388)_docall()
    386         mc = MultiCall(methods, kwargs, firstresult=self.firstresult)
    387         try:
--> 388             res = mc.execute()
    389             if res:
    390                 self.trace("finish", self.name, "-->", res)

  …/pytest/_pytest/core.py(289)execute()
    287             method = self.methods.pop()
    288             kwargs = self.getkwargs(method)
--> 289             res = method(**kwargs)
    290             if res is not None:
    291                 self.results.append(res)

  …/pytest/_pytest/main.py(138)pytest_runtestloop()
    136     for i, item in enumerate(session.items):
    137         nextitem = getnextitem(i)
--> 138         item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
    139         if session.shouldstop:
    140             raise session.Interrupted(session.shouldstop)

  …/pytest/_pytest/core.py(377)__call__()
    375     def __call__(self, **kwargs):
    376         methods = self.hookrelay._pm.listattr(self.name)
--> 377         return self._docall(methods, kwargs)
    378 
    379     def pcall(self, plugins, **kwargs):

  …/pytest/_pytest/core.py(388)_docall()
    386         mc = MultiCall(methods, kwargs, firstresult=self.firstresult)
    387         try:
--> 388             res = mc.execute()
    389             if res:
    390                 self.trace("finish", self.name, "-->", res)

  …/pytest/_pytest/core.py(289)execute()
    287             method = self.methods.pop()
    288             kwargs = self.getkwargs(method)
--> 289             res = method(**kwargs)
    290             if res is not None:
    291                 self.results.append(res)

  …/pytest/_pytest/runner.py(64)pytest_runtest_protocol()
     62         nodeid=item.nodeid, location=item.location,
     63     )
---> 64     runtestprotocol(item, nextitem=nextitem)
     65     return True
     66 

  …/pytest/_pytest/runner.py(71)runtestprotocol()
     69     if hasrequest and not item._request:
     70         item._initrequest()
---> 71     rep = call_and_report(item, "setup", log)
     72     reports = [rep]
     73     if rep.passed:

  …/pytest/_pytest/runner.py(110)call_and_report()
    108     call = call_runtest_hook(item, when, **kwds)
    109     hook = item.ihook
--> 110     report = hook.pytest_runtest_makereport(item=item, call=call)
    111     if log:
    112         hook.pytest_runtest_logreport(report=report)

  …/pytest/_pytest/runner.py(126)call_runtest_hook()
    124     hookname = "pytest_runtest_" + when
    125     ihook = getattr(item.ihook, hookname)
--> 126     return CallInfo(lambda: ihook(item=item, **kwds), when=when)
    127 
    128 class CallInfo:

  …/pytest/_pytest/runner.py(145)__init__()
    143                 self.excinfo = py.code.ExceptionInfo()
    144         finally:
--> 145             self.stop = time()
    146 
    147     def __repr__(self):

  …/pytest/_pytest/runner.py(126)<lambda>()
    124     hookname = "pytest_runtest_" + when
    125     ihook = getattr(item.ihook, hookname)
--> 126     return CallInfo(lambda: ihook(item=item, **kwds), when=when)
    127 
    128 class CallInfo:

  …/pytest/_pytest/main.py(162)call_matching_hooks()
    160         def call_matching_hooks(**kwargs):
    161             plugins = self.config._getmatchingplugins(self.fspath)
--> 162             return hookmethod.pcall(plugins, **kwargs)
    163         return call_matching_hooks
    164 

  …/pytest/_pytest/core.py(381)pcall()
    379     def pcall(self, plugins, **kwargs):
    380         methods = self.hookrelay._pm.listattr(self.name, plugins=plugins)
--> 381         return self._docall(methods, kwargs)
    382 
    383     def _docall(self, methods, kwargs):

  …/pytest/_pytest/core.py(392)_docall()
    390                 self.trace("finish", self.name, "-->", res)
    391         finally:
--> 392             self.trace.root.indent -= 1
    393         return res
    394 

  …/pytest/_pytest/core.py(289)execute()
    287             method = self.methods.pop()
    288             kwargs = self.getkwargs(method)
--> 289             res = method(**kwargs)
    290             if res is not None:
    291                 self.results.append(res)

  …/pytest/_pytest/runner.py(85)pytest_runtest_setup()
     83 
     84 def pytest_runtest_setup(item):
---> 85     item.session._setupstate.prepare(item)
     86 
     87 def pytest_runtest_call(item):

  …/pytest/_pytest/runner.py(387)prepare()
    385             except Exception:
    386                 col._prepare_exc = sys.exc_info()
--> 387                 raise
    388 
    389 def collect_one_node(collector):

  …/pytest/_pytest/unittest.py(74)setup()
     72             self._testcase.setup_method(self._obj)
     73         if hasattr(self, "_request"):
---> 74             self._request._fillfixtures()
     75 
     76     def teardown(self):

  …/pytest/_pytest/python.py(1265)_fillfixtures()
   1263         for argname in fixturenames:
   1264             if argname not in item.funcargs:
-> 1265                 item.funcargs[argname] = self.getfuncargvalue(argname)
   1266 
   1267     def cached_setup(self, setup, teardown=None, scope="module", extrakey=None):

  …/pytest/_pytest/python.py(1313)getfuncargvalue()
   1311         function body.
   1312         """
-> 1313         return self._get_active_fixturedef(argname).cached_result[0]
   1314 
   1315     def _get_active_fixturedef(self, argname):

  …/pytest/_pytest/python.py(1327)_get_active_fixturedef()
   1325                     return PseudoFixtureDef
   1326                 raise
-> 1327             result = self._getfuncargvalue(fixturedef)
   1328             self._funcargs[argname] = result
   1329             self._fixturedefs[argname] = fixturedef

  …/pytest/_pytest/python.py(1383)_getfuncargvalue()
   1381             # if fixture function failed it might have registered finalizers
   1382             self.session._setupstate.addfinalizer(fixturedef.finish,
-> 1383                                                   subrequest.node)
   1384         return val
   1385 

  …/pytest/_pytest/python.py(1827)execute()
   1825                     fixturefunc = fixturefunc.__get__(request.instance)
   1826             result = call_fixture_func(fixturefunc, request, kwargs,
-> 1827                                        self.yieldctx)
   1828         self.cached_result = (result, my_cache_key)
   1829         return result

  …/pytest/_pytest/python.py(1755)call_fixture_func()
   1753                     "Instead write and return an inner function/generator "
   1754                     "and let the consumer call and iterate over it.")
-> 1755         res = fixturefunc(**kwargs)
   1756     return res
   1757 

  …/virtualenv/lib/python2.7/site-packages/pytest_django/plugin.py(129)_django_runner()
    127         # Django >= 1.7: Call django.setup() to initialize Django
    128         setup = getattr(django, 'setup', lambda: None)
--> 129         setup()
    130 
    131         from django.test.simple import DjangoTestSuiteRunner

  …/django-master/django/__init__.py(21)setup()
     17     from django.conf import settings
     18     from django.utils.log import configure_logging
     19 
     20     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
---> 21     apps.populate(settings.INSTALLED_APPS)

  …/django-master/django/apps/registry.py(112)populate()
    110 
    111             for app_config in self.get_app_configs():
--> 112                 app_config.ready()
    113 
    114     def check_ready(self):

  …/django-master/django/apps/config.py(190)import_models()
    188         if module_has_submodule(self.module, MODELS_MODULE_NAME):
    189             models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME)
--> 190             self.models_module = import_module(models_module_name)
    191 
    192     def ready(self):

  /usr/lib/python2.7/importlib/__init__.py(37)import_module()
     34                 break
     35             level += 1
     36         name = _resolve_name(name[level:], package, level)
---> 37     __import__(name)
     38     return sys.modules[name]

  …/tmm/movies/models.py(54)<module>()
     52 
     53 
---> 54 class Person(models.Model):
     55     name = models.CharField(max_length=255)
     56 

  …/django-master/django/db/models/base.py(299)__new__()
    297 
    298         new_class._prepare()
--> 299         new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
    300         return new_class
    301 

> …/django-master/django/apps/registry.py(201)register_model()
    199             raise RuntimeError(
    200                 "Conflicting '%s' models in application '%s': %s and %s." %
--> 201                 (model_name, app_label, app_models[model_name], model))
    202         app_models[model_name] = model
    203         self.clear_cache()

  …/pytest/_pytest/runner.py(139)__init__()
    137         try:
    138             try:
--> 139                 self.result = func()
    140             except KeyboardInterrupt:
    141                 raise

  …/pytest/_pytest/runner.py(126)<lambda>()
    124     hookname = "pytest_runtest_" + when
    125     ihook = getattr(item.ihook, hookname)
--> 126     return CallInfo(lambda: ihook(item=item, **kwds), when=when)
    127 
    128 class CallInfo:

  …/pytest/_pytest/main.py(162)call_matching_hooks()
    160         def call_matching_hooks(**kwargs):
    161             plugins = self.config._getmatchingplugins(self.fspath)
--> 162             return hookmethod.pcall(plugins, **kwargs)
    163         return call_matching_hooks
    164 

  …/pytest/_pytest/core.py(381)pcall()
    379     def pcall(self, plugins, **kwargs):
    380         methods = self.hookrelay._pm.listattr(self.name, plugins=plugins)
--> 381         return self._docall(methods, kwargs)
    382 
    383     def _docall(self, methods, kwargs):

  …/pytest/_pytest/core.py(388)_docall()
    386         mc = MultiCall(methods, kwargs, firstresult=self.firstresult)
    387         try:
--> 388             res = mc.execute()
    389             if res:
    390                 self.trace("finish", self.name, "-->", res)

  …/pytest/_pytest/core.py(289)execute()
    287             method = self.methods.pop()
    288             kwargs = self.getkwargs(method)
--> 289             res = method(**kwargs)
    290             if res is not None:
    291                 self.results.append(res)

  …/pytest/_pytest/runner.py(85)pytest_runtest_setup()
     83 
     84 def pytest_runtest_setup(item):
---> 85     item.session._setupstate.prepare(item)
     86 
     87 def pytest_runtest_call(item):

  …/pytest/_pytest/runner.py(384)prepare()
    382             self.stack.append(col)
    383             try:
--> 384                 col.setup()
    385             except Exception:
    386                 col._prepare_exc = sys.exc_info()

  …/pytest/_pytest/unittest.py(74)setup()
     72             self._testcase.setup_method(self._obj)
     73         if hasattr(self, "_request"):
---> 74             self._request._fillfixtures()
     75 
     76     def teardown(self):

  …/pytest/_pytest/python.py(1265)_fillfixtures()
   1263         for argname in fixturenames:
   1264             if argname not in item.funcargs:
-> 1265                 item.funcargs[argname] = self.getfuncargvalue(argname)
   1266 
   1267     def cached_setup(self, setup, teardown=None, scope="module", extrakey=None):

  …/pytest/_pytest/python.py(1313)getfuncargvalue()
   1311         function body.
   1312         """
-> 1313         return self._get_active_fixturedef(argname).cached_result[0]
   1314 
   1315     def _get_active_fixturedef(self, argname):

  …/pytest/_pytest/python.py(1327)_get_active_fixturedef()
   1325                     return PseudoFixtureDef
   1326                 raise
-> 1327             result = self._getfuncargvalue(fixturedef)
   1328             self._funcargs[argname] = result
   1329             self._fixturedefs[argname] = fixturedef

  …/pytest/_pytest/python.py(1379)_getfuncargvalue()
   1377         try:
   1378             # call the fixture function
-> 1379             val = fixturedef.execute(request=subrequest)
   1380         finally:
   1381             # if fixture function failed it might have registered finalizers

  …/pytest/_pytest/python.py(1827)execute()
   1825                     fixturefunc = fixturefunc.__get__(request.instance)
   1826             result = call_fixture_func(fixturefunc, request, kwargs,
-> 1827                                        self.yieldctx)
   1828         self.cached_result = (result, my_cache_key)
   1829         return result

  …/pytest/_pytest/python.py(1755)call_fixture_func()
   1753                     "Instead write and return an inner function/generator "
   1754                     "and let the consumer call and iterate over it.")
-> 1755         res = fixturefunc(**kwargs)
   1756     return res
   1757 

  …/virtualenv/lib/python2.7/site-packages/pytest_django/plugin.py(129)_django_runner()
    127         # Django >= 1.7: Call django.setup() to initialize Django
    128         setup = getattr(django, 'setup', lambda: None)
--> 129         setup()
    130 
    131         from django.test.simple import DjangoTestSuiteRunner

  …/django-master/django/__init__.py(21)setup()
     17     from django.conf import settings
     18     from django.utils.log import configure_logging
     19 
     20     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
---> 21     apps.populate(settings.INSTALLED_APPS)

  …/django-master/django/apps/registry.py(106)populate()
    104             for app_config in self.app_configs.values():
    105                 all_models = self.all_models[app_config.label]
--> 106                 app_config.import_models(all_models)
    107 
    108             self.clear_cache()

  …/django-master/django/apps/config.py(190)import_models()
    188         if module_has_submodule(self.module, MODELS_MODULE_NAME):
    189             models_module_name = '%s.%s' % (self.name, MODELS_MODULE_NAME)
--> 190             self.models_module = import_module(models_module_name)
    191 
    192     def ready(self):

  /usr/lib/python2.7/importlib/__init__.py(37)import_module()
     34                 break
     35             level += 1
     36         name = _resolve_name(name[level:], package, level)
---> 37     __import__(name)
     38     return sys.modules[name]

  …/tmm/movies/models.py(54)<module>()
     52 
     53 
---> 54 class Person(models.Model):
     55     name = models.CharField(max_length=255)
     56 

  …/django-master/django/db/models/base.py(299)__new__()
    297 
    298         new_class._prepare()
--> 299         new_class._meta.apps.register_model(new_class._meta.app_label, new_class)
    300         return new_class
    301 

> …/django-master/django/apps/registry.py(201)register_model()
    199             raise RuntimeError(
    200                 "Conflicting '%s' models in application '%s': %s and %s." %
--> 201                 (model_name, app_label, app_models[model_name], model))
    202         app_models[model_name] = model
    203         self.clear_cache()


> RuntimeError: Conflicting 'person' models in application 'movies': <class 'tmm.tmm.movies.models.Person'> and <class 'movies.models.Person'>.
@blueyed blueyed closed this Mar 16, 2014
@syabro

No, it's not.
I'm running django tests fine, but py.test produces this error

@blueyed

@syabro
IIRC this is caused by __init__.py files being present where they should not be.

Check the comment at https://code.djangoproject.com/ticket/22280#comment:20.

If you still think it's a problem with pytest, please provide a traceback and additional information.

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