django / asgiref

ASGI specification and utilities
https://asgi.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
1.47k stars 209 forks source link

Parameters to Generic[...] must all be type variables #416

Closed AnyByte closed 12 months ago

AnyByte commented 12 months ago

Got this error only while debugging my Django application in Docker container with Pycharm. When debugging locally with Pycharm or starting application normally with Pycharm in Docker the error does not appear.

Python 3.9

Django==4.2.6 asgiref==3.7.2 Pycharm 2023.2.3 Pro

Log:

Connected to pydev debugger (build 232.10072.31)
python-BaseException
Traceback (most recent call last):
  File "/opt/project/backend/manage.py", line 11, in main
    from django.core.management import execute_from_command_line
  File "/opt/venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 16, in <module>
    from django.apps import apps
  File "/opt/venv/lib/python3.9/site-packages/django/apps/__init__.py", line 1, in <module>
    from .config import AppConfig
  File "/opt/venv/lib/python3.9/site-packages/django/apps/config.py", line 13, in <module>
    class AppConfig:
  File "/opt/venv/lib/python3.9/site-packages/django/apps/config.py", line 62, in AppConfig
    def default_auto_field(self):
  File "/opt/venv/lib/python3.9/site-packages/django/utils/functional.py", line 27, in __init__
    from django.utils.deprecation import RemovedInDjango50Warning
  File "/opt/venv/lib/python3.9/site-packages/django/utils/deprecation.py", line 4, in <module>
    from asgiref.sync import iscoroutinefunction, markcoroutinefunction, sync_to_async
  File "/opt/venv/lib/python3.9/site-packages/asgiref/sync.py", line 130, in <module>
    class AsyncToSync(Generic[_P, _R]):
  File "/usr/lib64/python3.9/typing.py", line 277, in inner
    return func(*args, **kwds)
  File "/usr/lib64/python3.9/typing.py", line 997, in __class_getitem__
    raise TypeError(
TypeError: Parameters to Generic[...] must all be type variables
Gracefully stopping... (press Ctrl+C again to force)
AnyByte commented 12 months ago

It seems like in Python 3.9 https://github.com/django/asgiref/blob/ebb1edbb222afe7775e79927cafd2025c78d971a/asgiref/sync.py#L12-L25 imports Generic that doesn't support non-type variables like: https://github.com/django/asgiref/blob/ebb1edbb222afe7775e79927cafd2025c78d971a/asgiref/sync.py#L113

andrewgodwin commented 12 months ago

We have a compatibility fix in place for this already - the _P, a ParamSpec, is what it's complaining about, but if you run the file under 3.9 or earlier then it uses a compatibility shim:

https://github.com/django/asgiref/blob/main/asgiref/sync.py#L30-L33

I suspect the PyCharm debugger has something to do with this failing, and unfortunately since it's a commercial product, I'm not willing to spend too much time working out exactly why it is failing. If you or someone else can work out a fix that doesn't break compatibility with other runtimes, I'm happy to apply it, but unfortunately I don't think we can solve this at the project level, so I'm going to close this for now.