pennersr/django-allauth

0.63.2 breaks with ImportError when using `secure_admin_login`

mcastle opened this issue · 8 comments

allauth/account/internal/flows/manage_email.py", line 7, in <module>
    from allauth.account.reauthentication import raise_if_reauthentication_required

ImportError: cannot import name 'raise_if_reauthentication_required' from partially initialized module 'allauth.account.reauthentication' (most likely due to a circular import)

Can you share the full stack trace? Also please include the python version you are using. I have trouble reproducing this issue. Is this running vanilla allauth, or do you have custom adapters & forms? Can you try commenting those out?

I just submitted a fix. If you'd still like the stack trace, please let me know. Looks like the issue was caused when using secure_admin_login and importing it with from allauth.account.decorators import secure_admin_login.

If you'd still like the stack trace, please let me know.

Yes please. I would like to understand why this issue is not being exposed in the test suite and why it also doesn't show up in the example projects.

If you'd still like the stack trace, please let me know.

Yes please. I would like to understand why this issue is not being exposed in the test suite and why it also doesn't show up in the example projects.

Sure thing, below is the stack trace. This is with Python 3.12.1 and Django 5.0.6:

Traceback (most recent call last):
  File "/Users/user/devel/project/./manage.py", line 23, in <module>
    main()
  File "/Users/user/devel/project/./manage.py", line 19, in main
    execute_from_command_line(sys.argv)
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/django/core/management/__init__.py", line 416, in execute
    django.setup()
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/django/apps/registry.py", line 124, in populate
    app_config.ready()
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/django/contrib/admin/apps.py", line 27, in ready
    self.module.autodiscover()
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/django/contrib/admin/__init__.py", line 52, in autodiscover
    autodiscover_modules("admin", register_to=site)
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/django/utils/module_loading.py", line 58, in autodiscover_modules
    import_module("%s.%s" % (app_config.name, module_to_search))
  File "/Users/user/.pyenv/versions/3.12.1/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/Users/user/devel/project/project/users/admin.py", line 7, in <module>
    from allauth.account.decorators import secure_admin_login
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/allauth/account/decorators.py", line 10, in <module>
    from allauth.account import reauthentication
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/allauth/account/reauthentication.py", line 11, in <module>
    from allauth.account.internal.flows.reauthentication import (
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/allauth/account/internal/__init__.py", line 1, in <module>
    from allauth.account.internal import flows
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/allauth/account/internal/flows/__init__.py", line 1, in <module>
    from allauth.account.internal.flows import (
  File "/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/allauth/account/internal/flows/manage_email.py", line 7, in <module>
    from allauth.account.reauthentication import raise_if_reauthentication_required
ImportError: cannot import name 'raise_if_reauthentication_required' from partially initialized module 'allauth.account.reauthentication' (most likely due to a circular import) (/Users/user/.pyenv/versions/3.12.1/envs/project/lib/python3.12/site-packages/allauth/account/reauthentication.py)

It looks like this issue may be due to using secure_admin_login in an admin.py (as is done here in django-cookiecutter, instead of calling secure_admin_login from the urls.py, as is done here in the example project.

I moved the use of 'secure_admin_login` to the urls.py and it resolved the issue.

I tried to alter the example project and move the import in admin.py, but still it did not show up. In any case, I have done a bit more elaborate fix over at 322635f -- but given that I cannot reproduce the issue, could you give the current main branch a try?

That fixes it!

Thanks for confirming, making it official here a96c99e