Skip to content

assertion rewrite: use sys.stdlib_module_names to skip stdlib modules early, replacing the _in_find_spec flag#14633

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-pytest-with-python-lazy-imports
Draft

assertion rewrite: use sys.stdlib_module_names to skip stdlib modules early, replacing the _in_find_spec flag#14633
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-pytest-with-python-lazy-imports

Conversation

Copilot AI commented Jun 21, 2026

Copy link
Copy Markdown

With PYTHON_LAZY_IMPORTS=all (Python 3.15+), accessing a lazily-imported attribute inside find_spec triggers a recursive find_spec call. The previous fix used a _in_find_spec boolean guard that blocked all re-entrant calls globally.

Changes

  • _early_rewrite_bailout: Add an early check against sys.stdlib_module_names (a built-in frozenset) to bail out immediately for stdlib modules — before calling fnmatch_ex, which is what triggers the lazy import resolution of fnmatch.fnmatch and causes the recursion. Stdlib modules are never assertion-rewritten anyway, so this is also a small performance win.
  • find_spec: Remove the _in_find_spec boolean flag and the associated try/finally wrapper — they are no longer needed since the recursion is broken at the stdlib check in _early_rewrite_bailout.
  • Tests: Replace the reentrancy-guard spy test with a direct assertion that find_spec returns None for stdlib modules (fnmatch, os, re).

The recursion chain that caused the crash:

find_spec("test_foo")
  → _early_rewrite_bailout("test_foo")
    → fnmatch_ex(pat, path)           # accesses fnmatch.fnmatch lazily
      → find_spec("fnmatch")          # triggered by lazy import resolution
        → _early_rewrite_bailout("fnmatch")
          → fnmatch_ex(…)             # infinite recursion

With the stdlib check, _early_rewrite_bailout("fnmatch") returns True immediately — breaking the cycle without any mutable per-call state.

Copilot AI changed the title [WIP] Fix pytest not working with PYTHON_LAZY_IMPORTS Fix AssertionRewritingHook crashing with PYTHON_LAZY_IMPORTS=all Jun 21, 2026
Copilot AI requested a review from RonnyPfannschmidt June 21, 2026 14:41
@ofek

ofek commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

@RonnyPfannschmidt Thanks for the suggestion! Hopefully this works 🙏



@pytest.mark.skipif(
sys.version_info < (3, 15),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not technically required. on < 3.15, it does just does nothing. it can still be checked.

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

@soxofaan @ofek I'm on the playground with my daughter id appreciate if you guys gave this one a spin to see if it works in practice

Copilot AI changed the title Fix AssertionRewritingHook crashing with PYTHON_LAZY_IMPORTS=all assertion rewrite: use sys.stdlib_module_names to skip stdlib modules early, replacing the _in_find_spec flag Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants