Watchers of computed reactives run in a different order on each run

Aperta Adatta ai principianti
#6,790 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Inizia da Reactive._compute, dove la issue individua l'iterazione sui nomi dei reactive calcolati. Trova i test esistenti dei reactive e aggiungi un test di regressione che registri le chiamate dei watcher quando cambia un reactive sorgente. È completato quando l'ordine dei watcher è deterministico tra esecuzioni con valori diversi di PYTHONHASHSEED e i test pertinenti passano.

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

Descrizione

Hi! I hit this while writing tests that record the order of watcher calls. Reactive._compute loops over obj._reactives.keys() & obj._computes, a set of strings, so the order of the computes, and of the watchers they trigger, depends on string hashing, which Python randomises per process.

import asyncio

import textual
from textual.app import App
from textual.reactive import reactive

class ComputeApp(App):
    value = reactive(0)
    alpha = reactive(0)
    beta = reactive(0)
    gamma = reactive(0)
    delta = reactive(0)

    def __init__(self) -> None:
        super().__init__()
        self.calls: list[str] = []

    def compute_alpha(self) -> int:
        return self.value + 1

    def compute_beta(self) -> int:
        return self.value + 2

    def compute_gamma(self) -> int:
        return self.value + 3

    def compute_delta(self) -> int:
        return self.value + 4

    def watch_alpha(self) -> None:
        self.calls.append("alpha")

    def watch_beta(self) -> None:
        self.calls.append("beta")

    def watch_gamma(self) -> None:
        self.calls.append("gamma")

    def watch_delta(self) -> None:
        self.calls.append("delta")

async def main() -> None:
    app = ComputeApp()
    async with app.run_test() as pilot:
        app.calls.clear()
        app.value = 10
        await pilot.pause()
        print(app.calls)

asyncio.run(main())

Run five times with different PYTHONHASHSEED values, on main (5e5b7ef), Python 3.13:

PYTHONHASHSEED=0: ['alpha', 'delta', 'beta', 'gamma']
PYTHONHASHSEED=1: ['beta', 'delta', 'gamma', 'alpha']
PYTHONHASHSEED=2: ['delta', 'gamma', 'alpha', 'beta']
PYTHONHASHSEED=3: ['gamma', 'beta', 'delta', 'alpha']
PYTHONHASHSEED=4: ['gamma', 'alpha', 'beta', 'delta']

8.2.7 gives a different mix for the same seeds. Without PYTHONHASHSEED set, the order changes from run to run.

The values are right either way, since reading a computed reactive computes it. Only the order of the watchers moves. I expected the same order on every run. A one-line fix is to sort the names in _compute, which makes the order alphabetical. Declaration order would be nicer, but it would mean walking every reactive on each change, so I went for sorting. Happy to do it the other way if you prefer.

Lingua principale
Python
Stelle
37.7k
Fork
1.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

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 Textualize/textual

Tutte le issue di Textualize/textual

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.