Scoped reload to measure source imported before the coverage engine starts
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 28/100
- Tipo di issue
- Funzionalità
- Chiarezza
- Da chiarire
- Stato di attività
- Tranquilla
- Stack tecnologico
- python
- Ambito
- testing-qa
Direzione di ricerca
Start by reviewing the existing pth/embed machinery and the plugins documentation, then trace how --cov, source, and source_pkgs are resolved when coverage starts. Compare the snapshot-and-diff and source-discovery approaches, including the listed reload hazards; done means an agreed opt-in design with clear scope, safeguards, and documentation.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
Explore a scoped, opt-in mechanism for pytest-cov to measure code that was imported before the coverage engine started — the classic "plugin imports the project under test" problem — by reloading only the affected source modules once coverage is active.
This is a solution-focused follow-up to #635 (and the cluster #682, #414, #439, #397, #372, #359, #587). In #635 the conclusion was essentially "use coverage run -m pytest, or manually pre-start via the pth/env-var machinery, or scrub sys.modules yourself in the offending plugin," and @ionelmc rightly cautioned that broad sys.modules manipulation is a "big no-no" (it invites blame reports like #620). The goal here is to discuss whether a narrowly-scoped reload could sidestep that objection.
The limitation
When a plugin is imported before pytest-cov's engine starts (entry-point plugins, or anything pytest-enabler-style that imports the project under test to decide whether to enable coverage), every line executed at import time — import, class, def, decorators — is recorded as missed, because it never executes again under coverage. The plugins docs document this and state there's currently "no way to measure your pytest plugin."
Concretely: pytest-perf is itself a pytest plugin, so its own runner.py import-time lines are unmeasurable via pytest --cov; a 100% diff-coverage gate can't be met without either coverage run -m pytest or a per-project reload hack (jaraco/pytest-perf#18, jaraco/pytest-perf#23).
Approaches worth exploring
Both aim to reload only modules that (a) were imported before coverage started and (b) fall under the configured --cov/source — never stdlib, never third-party, never the whole sys.modules.
-
Snapshot-and-diff. Capture the set of
sys.moduleskeys as early as possible (e.g. via the pth/embed machinery that already exists for subprocess start-up). After the coverage engine is active, reload only the modules that appeared since the snapshot and that match the source configuration — i.e. exactly the modules a plugin pulled in early. This bounds the blast radius to plugin-activated source. -
Source-discovery. From the resolved
--cov/source/source_pkgsconfiguration, discover the target modules and, for any already present insys.moduleswhen coverage starts,importlib.reloadjust those. Reloading is scoped strictly to the code under measurement.
Caveats to discuss
- Reloading has real hazards: module-level singletons, registries, dataclass/enum identity, C extensions, and already-bound references to pre-reload objects. Scoping to source-under-measurement reduces but doesn't eliminate this.
- It would need to be opt-in (a config flag), off by default, and clearly documented as "measures import-time lines at the cost of a scoped reload."
- The existing
coverage run -m pytestremains the clean answer for those who can wrap the invocation; this is for the plugin-activation workflows (pytest-enabler et al.) where wrapping is undesirable, per #635.
Question
Would a scoped, opt-in reload — bounded to the configured source and to modules imported before the engine started — be an acceptable direction, or are the reload hazards fundamental enough that documenting coverage run -m pytest should remain the only recommendation?
- Lingua principale
- Python
- Stelle
- 2.1k
- Fork
- 253
- Merge medio
- 12g 4h
- PR unite (30g)
- 4
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pytest-dev/pytest-cov
-
Celery tests require package; test blows up if Celery not installed instead of skipping the tests gracefullyForse già presa @dchaudhari7177 l’ha presa 98 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
pytest-dev/pytest-cov#750 ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
pytest-dev/pytest-cov#617 · 1 commento ·
-
Only set dynamic context on coveragepy cores supporting it, perhaps?Forse già presa @terminalchai l’ha presa 12 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 45/100
pytest-dev/pytest-cov#755 · 3 commenti ·
-
INTERNALERROR when generating html report using pytest-cov 7.1.0 with jinjaForse già presa @daleselaji-dev l’ha presa 84 giorni fa. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
pytest-dev/pytest-cov#748 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
pytest-dev/pytest-cov#746 · 2 commenti ·
Tutte le issue di pytest-dev/pytest-cov
Issue simili
-
good first issue test
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
ANCORATE/EncryptHome#8 ·
-
PluginProvider.storeInstance races and clobbers the dedup cache for concurrent loadsForse già presa @Wilson0511 l’ha presa oggi. Apertaneeds-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 2 giorni
-
Bug: failed Together chat calls do not record operation duration or error.typeForse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
community documentation good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100