@memoize on VariantCollection.original_reference_names returns another (equal) collection's result and keeps every collection alive

Aperta Adatta ai principianti
#736 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Leggi varcode/variant_collection.py in corrispondenza di reference_names() e original_reference_names(), poi esamina la posizione del test di regressione e i tre usi di memoize in reference.py. Riproduci il problema con varianti equivalenti create usando genome="hg19" e genome="GRCh37"; il lavoro è terminato quando ogni collezione restituisce il proprio nome di riferimento originale e la cache non conserva più istanze di collezioni.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Problem

VariantCollection.reference_names() and VariantCollection.original_reference_names() are decorated with varcode.common.memoize. That decorator is a hand-written cache: a module-level dict keyed by the call's arguments, which here means self. This causes two problems.

  1. Wrong results. Equal collections share one cache entry:

    • sercol collections hash by length and compare element by element.
    • Variant.__eq__ compares the resolved genome, not original_reference_name.

    So the same calls loaded from an hg19 VCF and from a GRCh37 VCF are equal collections. Whichever is asked first fills the cache, and the other gets its answer: original_reference_names() returns {"GRCh37"} for the hg19 collection, or {"hg19"} for the GRCh37 one.

  2. Memory. The cache keeps a strong reference to every collection it has seen, for the life of the process. #133 (open since 2016) asks to stop memoizing methods in a global dict. These two methods are the remaining instances.

Both methods are a single set comprehension over the variants, so they don't need a cache at all.

Evidence

Read at varcode 96dc77b and sercol b0b8848.

  • varcode/common.py:57-75, memoize: a dict keyed by (args, sorted kwargs).
  • varcode/variant_collection.py:314-337: @memoize on reference_names and original_reference_names.
  • sercol sercol/collection.py:228-240: __hash__ returns hash(len(self)), and __eq__ compares class, length and elements.
  • varcode/variant.py:290-301: Variant.__eq__ compares contig, start, end, ref, alt and genome.
  • varcode/variant.py:170-178: original_reference_name is the UCSC name (hg19) when the genome was given that way, and the Ensembl name otherwise. infer_genome resolves both hg19 and GRCh37 to the same release object.
  • The other uses of memoize decorate module functions with string arguments (reference.py:135, :270, :349). functools.cache does the same job there.

Suggested fix

  • Remove @memoize from both methods.
  • Use functools.cache at the three uses in reference.py, and delete varcode.common.memoize.
  • Add a regression test. Build the same small variants with genome="hg19" and with genome="GRCh37", call original_reference_names() on one collection, then check the other's result.

https://claude.ai/code/session_01JHJhwj29oDqoeu7TYd34Pi

Lingua principale
Python
Stelle
88
Fork
28
Merge medio
4h 48m
PR unite (30g)
62

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di openvax/varcode

Tutte le issue di openvax/varcode

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.