pytest-dev / pytest

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing
https://pytest.org
MIT License
11.86k stars 2.64k forks source link

`_pytest/assertion/rewrite.py` do not import setuptools vendored module, but use package installed in envrionment #12631

Open Czaki opened 1 month ago

Czaki commented 1 month ago

When using pytester to test a package fixture, the assertion rewrite is triggered. It looks like there is also trying to rewrite assertions in setuptools,

However, most recent releases of setuptools depends on typing_extension 4.12.2. This version is vendored by setuptools. but during rewrite:

    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/pytester.py", line 1163, in runpytest_inprocess
      reprec = self.inline_run(*args, **kwargs)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/pytester.py", line 1128, in inline_run
      ret = main([str(x) for x in args], plugins=plugins)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/config/__init__.py", line 153, in main
      config = _prepareconfig(args, plugins)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/config/__init__.py", line 335, in _prepareconfig
      config = pluginmanager.hook.pytest_cmdline_parse(
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/pluggy/_hooks.py", line 513, in __call__
      return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/pluggy/_manager.py", line 120, in _hookexec
      return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/pluggy/_callers.py", line 139, in _multicall
      raise exception.with_traceback(exception.__traceback__)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/pluggy/_callers.py", line 122, in _multicall
      teardown.throw(exception)  # type: ignore[union-attr]
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/helpconfig.py", line 105, in pytest_cmdline_parse
      config = yield
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/pluggy/_callers.py", line 103, in _multicall
      res = hook_impl.function(*args)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/config/__init__.py", line 1141, in pytest_cmdline_parse
      self.parse(args)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/config/__init__.py", line 1490, in parse
      self._preparse(args, addopts=addopts)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/config/__init__.py", line 1377, in _preparse
      self.pluginmanager.load_setuptools_entrypoints("pytest11")
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/pluggy/_manager.py", line 421, in load_setuptools_entrypoints
      plugin = ep.load()
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/importlib_metadata/__init__.py", line 183, in load
      module = import_module(match.group('module'))
    File "/opt/hostedtoolcache/Python/3.9.19/x64/lib/python3.9/importlib/__init__.py", line 127, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
    File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
    File "<frozen importlib._bootstrap>", line 972, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
    File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
    File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
    File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
    File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/assertion/rewrite.py", line 178, in exec_module
      exec(co, module.__dict__)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/setuptools/_vendor/typeguard/__init__.py", line 4, in <module>
      from ._checkers import TypeCheckerCallable as TypeCheckerCallable
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/_pytest/assertion/rewrite.py", line 178, in exec_module
      exec(co, module.__dict__)
    File "/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/setuptools/_vendor/typeguard/_checkers.py", line 71, in <module>
      from typing_extensions import Any as SubclassableAny
  ImportError: cannot import name 'Any' from 'typing_extensions' (/home/runner/work/napari/napari/.tox/py39-linux-pyqt5-cov/lib/python3.9/site-packages/typing_extensions.py)

it looks importing from global typing_extension, not vendored one.

It creates failure on our CI as we have minimum requirement test steep, and our typing_extension minimum version is much smaller than required setuptools.

Maybe pytest should not rewrite setuptools content?

https://github.com/napari/napari/issues/7107

https://github.com/napari/napari/actions/runs/10002793073/job/27649005717?pr=7106

RonnyPfannschmidt commented 1 month ago

This is a issue with setuptools 70.x

Please try the latest version or report with it

Czaki commented 1 month ago

This is with setuptools==71.0.3 so latest available version

RonnyPfannschmidt commented 1 month ago

Then I hope the next Patch to it fixed it,the 70.x series had a chain of unplanned issues around the vendoring