Skip to content

pytest does not work with PYTHON_LAZY_IMPORTS=all #14632

Open

Description

@sobolevn

Python version: 3.15.0b2+dev
Pytest version: 9.1.1

PYTHON_LAZY_IMPORTS=all pytest --help produces:

Traceback (most recent call last):
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/config/__init__.py", line 50, in <module>
    from .exceptions import UsageError as UsageError
ImportError: lazy import of '_pytest.config.exceptions.UsageError' raised an exception during resolution

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/sobolev/Desktop/msgspec/.venv/bin/pytest", line 10, in <module>
    sys.exit(console_main())
             ~~~~~~~~~~~~^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/config/__init__.py", line 223, in console_main
    code = main()
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/config/__init__.py", line 193, in main
    config = _prepareconfig(new_args, plugins)
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/config/__init__.py", line 361, in _prepareconfig
    config: Config = pluginmanager.hook.pytest_cmdline_parse(
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        pluginmanager=pluginmanager, args=args
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/pluggy/_hooks.py", line 512, in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
           ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/pluggy/_manager.py", line 120, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
           ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/pluggy/_callers.py", line 167, in _multicall
    raise exception
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/pluggy/_callers.py", line 139, in _multicall
    teardown.throw(exception)
    ~~~~~~~~~~~~~~^^^^^^^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/helpconfig.py", line 124, in pytest_cmdline_parse
    config = yield
             ^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/pluggy/_callers.py", line 121, in _multicall
    res = hook_impl.function(*args)
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/config/__init__.py", line 1193, in pytest_cmdline_parse
    except UsageError:
           ^^^^^^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/assertion/rewrite.py", line 111, in find_spec
    if self._early_rewrite_bailout(name, state):
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/assertion/rewrite.py", line 229, in _early_rewrite_bailout
    if fnmatch_ex(pat, path):
       ~~~~~~~~~~^^^^^^^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/pathlib.py", line 452, in fnmatch_ex
    return fnmatch.fnmatch(name, pattern)
           ^^^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/assertion/rewrite.py", line 111, in find_spec
    if self._early_rewrite_bailout(name, state):
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/assertion/rewrite.py", line 229, in _early_rewrite_bailout
    if fnmatch_ex(pat, path):
       ~~~~~~~~~~^^^^^^^^^^^
  File "/Users/sobolev/Desktop/msgspec/.venv/lib/python3.15/site-packages/_pytest/pathlib.py", line 452, in fnmatch_ex
    return fnmatch.fnmatch(name, pattern)
           ^^^^^^^
ImportCycleError: cannot import name 'fnmatch' (most likely due to a circular import)                

The same happens for all other invokations.

Activity

  1. RonnyPfannschmidt commented on Jun 21, 2026

    @RonnyPfannschmidt
    Member

    Assertion rewrite doesn't expect it's import hook to Recurse that way

    We may need a hardheaded skip list for that import mode

  2. added 4 commits that reference this issue on Sep 1, 2026
    f401303
    3921d14
    b21e48e
    3337196
  3. added a commit that references this issue on Sep 15, 2026
    a7d678d
  4. added a commit that references this issue on Sep 23, 2026
    8511b5f
  5. added 3 commits that reference this issue on Oct 4, 2026
    2c8b363
    cf954f5
    b200c43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions