Skip to content

fix: support non-editable installs (site-packages import precedence)#487

Open
pythoniste wants to merge 1 commit intoboxed:mainfrom
inspyration:fix/353-pydantic-classvar
Open

fix: support non-editable installs (site-packages import precedence)#487
pythoniste wants to merge 1 commit intoboxed:mainfrom
inspyration:fix/353-pydantic-classvar

Conversation

@pythoniste
Copy link
Copy Markdown

When the package-under-test is installed in site-packages as a
non-editable wheel rather than via pip install -e ., mutmut's
trampoline-injected code in the mutants/ directory was never loaded.

Python imported the original (non-mutated) modules from site-packages
because they were already cached in sys.modules. The stats phase found
zero test coverage and all mutants were marked "not checked".

Add _patch_imported_packages() to setup_source_paths(): after adding
mutants/ to sys.path, patch path on every already-imported package
so submodule lookups prefer the mutants tree. Flush leaf modules whose
mutated .py exists on disk so they get re-imported with trampoline code.

Add _copy_parent_init_files() to copy_src_dir(): when paths_to_mutate
targets individual files, parent init.py files were missing from
the mutants tree, preventing Python from recognising it as a package.

Tested on a Pydantic-heavy project (8,996 mutants): stats phase now
finds 477 covered functions and achieves a 70% kill rate, where
previously it found zero coverage.

  When the package-under-test is installed in site-packages as a
  non-editable wheel rather than via `pip install -e .`, mutmut's
  trampoline-injected code in the mutants/ directory was never loaded.

  Python imported the original (non-mutated) modules from site-packages
  because they were already cached in sys.modules. The stats phase found
  zero test coverage and all mutants were marked "not checked".

  Add _patch_imported_packages() to setup_source_paths(): after adding
  mutants/ to sys.path, patch __path__ on every already-imported package
  so submodule lookups prefer the mutants tree. Flush leaf modules whose
  mutated .py exists on disk so they get re-imported with trampoline code.

  Add _copy_parent_init_files() to copy_src_dir(): when paths_to_mutate
  targets individual files, parent __init__.py files were missing from
  the mutants tree, preventing Python from recognising it as a package.

  Tested on a Pydantic-heavy project (8,996 mutants): stats phase now
  finds 477 covered functions and achieves a 70% kill rate, where
  previously it found zero coverage.
@pythoniste
Copy link
Copy Markdown
Author

By the way, the branch name is misleading. At first, I wanted to fix Pydantic and then, I fixed something else.

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.

1 participant