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

get_user_model_hook raises LookupError without auth app #1380

Closed
meshy opened this issue Feb 26, 2023 · 1 comment · Fixed by #2335
Closed

get_user_model_hook raises LookupError without auth app #1380

meshy opened this issue Feb 26, 2023 · 1 comment · Fixed by #2335
Labels
bug Something isn't working

Comments

@meshy
Copy link
Contributor

meshy commented Feb 26, 2023

Bug report

What's wrong

Djangn-stubs causes mypy to crashe with LookupError when:

  • django.contrib.auth is not in INSTALLED_APPS,
  • any imported code references get_user_model().

This happened to me because I'm importing SettingsWrapper from pytest_django.fixtures. Mypy then wants a type for get_user_model because pytest_django's django_user_model fixture imports and returns get_user_model() in that module.

When get_user_model_hook is called, but the auth app isn't installed, Django raises a LookupError. I must admit, I have no idea what it should return.

This is similar to #318.

Traceback:

Traceback (most recent call last):
  File "mypy/checkexpr.py", line 4813, in accept
  File "mypy/checkexpr.py", line 425, in visit_call_expr
  File "mypy/checkexpr.py", line 545, in visit_call_expr_inner
  File "mypy/checkexpr.py", line 1197, in check_call_expr_with_callee_type
  File "mypy/checkexpr.py", line 1280, in check_call
  File "mypy/checkexpr.py", line 1490, in check_callable_call
  File "mypy/checkexpr.py", line 1020, in apply_function_plugin
  File "/.../python3.10/site-packages/mypy_django_plugin/transformers/settings.py", line 13, in get_user_model_hook
    model_cls = django_context.apps_registry.get_model(auth_user_model)
  File "/.../python3.10/site-packages/django/apps/registry.py", line 206, in get_model
    app_config = self.get_app_config(app_label)
  File "/.../python3.10/site-packages/django/apps/registry.py", line 163, in get_app_config
    raise LookupError(message)
LookupError: No installed app with label 'auth'.

/.../python3.10/site-packages/pytest_django/fixtures.py:397: : note: use --pdb to drop into pdb

How it should work

get_user_model_hook should not raise an error when there is no auth app.

System information

  • OS: Ubuntu 22.04.2 LTS
  • python version: 3.10.6
  • django version: 3.1
  • mypy version: 1.0.1
  • django-stubs version: 1.15.0
  • django-stubs-ext version: 0.7.0
@meshy meshy added the bug Something isn't working label Feb 26, 2023
@BranislavBajuzik
Copy link

I had this exact problem. The Mypy code that throws this uses your settings.AUTH_USER_MODEL. My problem was that I pointed to empty (the __init__.py 😅) settings so the default value of auth.User was used instead of what I put in there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants