Run Tests with Coverage "TypeError: Parameters to Generic[...] must all be type variables"

188 Views Asked by At

When running tests with coverage using PyCharm on django (python) project, im having the following error:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2023.1.2\plugins\python\helpers\pycharm\django_test_manage.py", line 6, in <module>
    from django.core.management import ManagementUtility
  File "C:\Users\User\Project\Code\venv39-master\lib\site-packages\django\core\management\__init__.py", line 13, in <module>
    from django.apps import apps
  File "C:\Users\User\Project\Code\venv39-master\lib\site-packages\django\apps\__init__.py", line 1, in <module>
    from .config import AppConfig
  File "C:\Users\User\Project\Code\venv39-master\lib\site-packages\django\apps\config.py", line 7, in <module>
    from django.utils.deprecation import RemovedInDjango41Warning
  File "C:\Users\User\Project\Code\venv39-master\lib\site-packages\django\utils\deprecation.py", line 5, in <module>
    from asgiref.sync import sync_to_async
  File "C:\Users\User\Project\Code\venv39-master\lib\site-packages\asgiref\sync.py", line 130, in <module>
    class AsyncToSync(Generic[_P, _R]):
  File "C:\Program Files\Python39\lib\typing.py", line 277, in inner
    return func(*args, **kwds)
  File "C:\Program Files\Python39\lib\typing.py", line 997, in __class_getitem__
    raise TypeError(
TypeError: Parameters to Generic[...] must all be type variables
class SimpleTest(TestCase):
    def test_basic_addition(self):
        self.assertEqual(1 + 1, 2)

According to what i have read, its probably a version conflict between typing and asgiref. Here are the versions I'm using:

typing_extensions 4.8.0
asgiref 3.7.2
Python 3.9.13
Django 3.2.20

Whats weirder is that running the the same test with coverage (using lib coverage.py) from terminal, everything works.

coverage run ./manage.py test -v 3  

According to Jetbrains, the PyCharm IDE uses the exact same library for coverage...What could be the solution?

% coverage debug sys                                                     
-- sys -------------------------------------------------------
coverage_version: 7.3.2
coverage_module: C:\Users\User\Project\Code\venv39-master\lib\site-packages\coverage\__init__.py
python: 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]
platform: Windows-10-10.0.22621-SP0
implementation: CPython
executable: C:\Users\User\Project\Code\venv39-master\Scripts\python.exe
path: C:\Users\User\Project\Code\venv39-master\Scripts\coverage.exe
       C:\Program Files\Python39\python39.zip
       C:\Program Files\Python39\DLLs
       C:\Program Files\Python39\lib
       C:\Program Files\Python39
       C:\Users\User\Project\Code\venv39-master
       C:\Users\User\Project\Code\venv39-master\lib\site-packages
1

There are 1 best solutions below

1
On

Most likely coverage in the terminal is using a different version of Python. Try coverage debug sys to get a dump of relevant information. Look for lines like this:

% coverage debug sys
-- sys -------------------------------------------------------
               coverage_version: 7.3.2
                coverage_module: /usr/local/virtualenvs/lab3/lib/python3.10/site-packages/coverage/__init__.py                         python: 3.10.13 (main, Aug 25 2023, 06:52:26) [Clang 14.0.3 (clang-1403.0.22.14.1)]
                       platform: macOS-14.1.1-x86_64-i386-64bit
                 implementation: CPython
                     executable: /usr/local/virtualenvs/lab3/bin/python
                           path: /usr/local/virtualenvs/lab3/bin
                                 /usr/local/pyenv/pyenv/versions/3.10.13/lib/python310.zip
                                 /usr/local/pyenv/pyenv/versions/3.10.13/lib/python3.10
                                 /usr/local/pyenv/pyenv/versions/3.10.13/lib/python3.10/lib-dynload
                                 /usr/local/virtualenvs/lab3/lib/python3.10/site-packages