不能再 运行 pytest 因为 importlib-metadata entry_points() 失败 configparser.DuplicateOptionError

Cannot run pytest anymore because importlib-metadata entry_points() fails with configparser.DuplicateOptionError

pluggypytest 使用。最近 pluggy 改为使用 importlib-metadata 而不是 pkg-resources。从那以后我不能再 运行 测试了。我直接从我的库 (bliss) 源目录启动 pytest 就像我一直做的那样,但现在一开始就失败了:

Traceback (most recent call last):
  File "/opt/conda/envs/testenv/bin/pytest", line 11, in <module>
    sys.exit(main())
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 63, in main
    config = _prepareconfig(args, plugins)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 207, in _prepareconfig
    pluginmanager=pluginmanager, args=args
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/helpconfig.py", line 94, in pytest_cmdline_parse
    config = outcome.get_result()
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 687, in pytest_cmdline_parse
    self.parse(args)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 895, in parse
    self._preparse(args, addopts=addopts)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 836, in _preparse
    self._consider_importhook(args)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 784, in _consider_importhook
    self._mark_plugins_for_rewrite(hook)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 806, in _mark_plugins_for_rewrite
    for name in _iter_rewritable_modules(package_files):
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 625, in _iter_rewritable_modules
    for fn in package_files:
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 802, in <genexpr>
    if any(ep.group == "pytest11" for ep in dist.entry_points)
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/importlib_metadata/__init__.py", line 226, in entry_points
    return EntryPoint._from_text(self.read_text('entry_points.txt'))
  File "/opt/conda/envs/testenv/lib/python3.7/site-packages/importlib_metadata/__init__.py", line 109, in _from_text
    config.read_string(text)
  File "/opt/conda/envs/testenv/lib/python3.7/configparser.py", line 722, in read_string
    self.read_file(sfile, source)
  File "/opt/conda/envs/testenv/lib/python3.7/configparser.py", line 717, in read_file
    self._read(f, source)
  File "/opt/conda/envs/testenv/lib/python3.7/configparser.py", line 1091, in _read
    fpname, lineno)
configparser.DuplicateOptionError: While reading from '<string>' [line 16]: option 'bliss' in section 'console_scripts' already exists

我的 setup.py 看起来像这样:

def main():
  ...
  setup(name=meta['name'],
        ...
        entry_points={
            "console_scripts": [
                "bliss = bliss.shell.cli.main:main",
                ...
            ]
        },
        ...
   )

console_scripts 部分没有重复

我不知道为什么会收到此错误消息...

我将问题缩小到 importlib-metadata,特别是它创建 一个 ConfigParser 对象到 "parse" 一个 entry_points.txt 文件(不 存在 !我不知道它来自哪里!),如果我在 构造函数它使我的测试再次 运行ning。

根据更多信息,console_scripts 有两个这样的条目:

    entry_points={
        "console_scripts": [
            "bliss = bliss.shell.cli.main:main",
            ...
            "Bliss = ...",
        ]

importlib-metadata 当前有一个错误,它以 case-insensitive 方式解析此文件

这是修复该问题的补丁:https://gitlab.com/python-devs/importlib_metadata/merge_requests/76