djangorestframework-simplejwt icon indicating copy to clipboard operation
djangorestframework-simplejwt copied to clipboard

mypy fails due to SimpleJWT trying to access SECRET_KEY

Open PureTryOut opened this issue 1 year ago • 9 comments

Currently our CI is failing due to mypy failing to find SECRET_KEY as used by SimpleJWT.

$ mypy . --check-untyped-defs
Traceback (most recent call last):
Error constructing plugin instance of NewSemanalDjangoPlugin
  File "/home/runner/work/beep-backend/beep-backend/.venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ^^^^^^^^^^^^^^^
  File "/home/runner/work/beep-backend/beep-backend/.venv/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "mypy/main.py", line 95, in main
  File "mypy/main.py", line 174, in run_build
  File "mypy/build.py", line 197, in build
  File "mypy/build.py", line 242, in _build
  File "mypy/build.py", line 503, in load_plugins
  File "mypy/build.py", line 484, in load_plugins_from_config
  File "/home/runner/work/beep-backend/beep-backend/.venv/lib/python3.11/site-packages/mypy_django_plugin/main.py", line 77, in __init__
    self.django_context = DjangoContext(self.plugin_config.django_settings_module)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/beep-backend/beep-backend/.venv/lib/python3.11/site-packages/mypy_django_plugin/django/context.py", line 84, in __init__
    apps, settings = initialize_django(self.django_settings_module)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/beep-backend/beep-backend/.venv/lib/python3.11/site-packages/mypy_django_plugin/django/context.py", line 68, in initialize_django
    apps.populate(settings.INSTALLED_APPS)
  File "/home/runner/work/beep-backend/beep-backend/.venv/lib/python3.11/site-packages/django/apps/registry.py", line 116, in populate
    app_config.import_models()
  File "/home/runner/work/beep-backend/beep-backend/.venv/lib/python3.11/site-packages/django/apps/config.py", line 269, in import_models
    self.models_module = import_module(models_module_name)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/runner/work/beep-backend/beep-backend/.venv/lib/python3.11/site-packages/rest_framework_simplejwt/models.py", line 6, in <module>
    from .settings import api_settings
  File "/home/runner/work/beep-backend/beep-backend/.venv/lib/python3.11/site-packages/rest_framework_simplejwt/settings.py", line 19, in <module>
    "SIGNING_KEY": settings.SECRET_KEY,
                   ^^^^^^^^^^^^^^^^^^^
  File "/home/runner/work/beep-backend/beep-backend/.venv/lib/python3.11/site-packages/django/conf/__init__.py", line 111, in __getattr__
    raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")

django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.

Strangely, this doesn't happen if I run mypy locally, only in CI where setup is basically the same. I tried running DJANGO_SETTINGS_MODULE=<path to settings> mypy . but it did not work. How can I get this package compatible with mypy?

PureTryOut avatar Jul 27 '23 14:07 PureTryOut

Yes, where else would it be?

PureTryOut avatar Oct 02 '23 08:10 PureTryOut

Well in a bare django-admin startproject project the secret key is already set in the settings file. I don't see why I or anyone else would ever move it, so that's why I'm asking where else it would be. Genuine interest, not trying to be snarky or something.

PureTryOut avatar Oct 02 '23 08:10 PureTryOut

@PureTryOut Did you ever get an answer to this? I'm running into an issue and my SECRET_KEY is clearly in my settings.py file.

yellowlabrat avatar Jul 01 '24 12:07 yellowlabrat

Nope, sorry.

PureTryOut avatar Jul 01 '24 15:07 PureTryOut

Thanks for the response! Did you find any workarounds?

yellowlabrat avatar Jul 01 '24 21:07 yellowlabrat

Also no. Mainly due to this problem we stopped using mypy for now, waiting for a fix :shrug:

PureTryOut avatar Jul 02 '24 06:07 PureTryOut

Just in case this helps you or anyone else out in the future, I ended up using a different package called django-rest-knox. https://jazzband.github.io/django-rest-knox/

This person wrote a great tutorial on it as well (I just didn't change any of the Knox settings that they mention): https://blog.devgenius.io/django-rest-knox-token-authentication-f134760a4a7b

yellowlabrat avatar Jul 09 '24 12:07 yellowlabrat

I had the same issue but when I updated django-stubs package to the latest version the problem went away so it should be good now.

desecho avatar Sep 28 '24 22:09 desecho