运行测试并覆盖“TypeError:Generic[...] 的参数必须全部是类型变量”

问题描述 投票:0回答:1

django (python) 项目上使用 PyCharm 运行覆盖率测试时,出现以下错误:

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)

根据我读到的内容,这可能是打字和asgiref之间的版本冲突。以下是我正在使用的版本:

typing_extensions 4.8.0
asgiref 3.7.2
Python 3.9.13
Django 3.2.20

更奇怪的是,从终端运行相同的覆盖率测试(使用 libcoverage.py),一切正常。

coverage run ./manage.py test -v 3  

根据 Jetbrains 的说法,PyCharm IDE 使用完全相同的库进行覆盖...解决方案是什么?

django django-testing python-3.9 coverage.py
1个回答
0
投票

终端中最有可能的

coverage
正在使用不同版本的Python。尝试
coverage debug sys
获取相关信息的转储。寻找这样的行:

% 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
© www.soinside.com 2019 - 2024. All rights reserved.