`run_test()` doesn't install the eager task factory, so tasks start in a different order than under `run()`

オープン
#6,792 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
22/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
testing

調査の方向性

Compare how run_async() installs asyncio.eager_task_factory with how run_test() starts the app in the Textual app module, then reproduce the ordering script from the issue. Done means run_test() matches run() and the three failing tests in tests/command_palette/test_declare_sources.py pass. Those tests depend on the screen-startup behaviour tracked in #6444, so that needs settling first, and a maintainer should confirm the direction before you start.

索引モデルが issue の本文から書いたものです。

説明

Hi! I hit this while comparing an app's behaviour under run() with the same app under run_test(). Since #6102, run_async() sets asyncio.eager_task_factory on the loop (Python 3.12+), but run_test() doesn't, so a task created in a handler starts at a different point depending on how the app is run. #6492 mentions this too.

import asyncio
import sys

import textual
from textual.app import App

class OrderApp(App[list]):
    def on_mount(self) -> None:
        self.order = []

        async def task() -> None:
            self.order.append("task started")

        asyncio.create_task(task())
        self.order.append("on_mount continued")
        self.set_timer(0.1, lambda: self.exit(self.order))

async def under_run_test() -> list:
    app = OrderApp()
    async with app.run_test() as pilot:
        await pilot.pause(0.2)
    return app.order

print(f"Textual {textual.__version__}, Python {sys.version.split()[0]}")
print("run():     ", OrderApp().run(headless=True))
print("run_test():", asyncio.run(under_run_test()))

Output on Textual 8.2.7 and on main (5e5b7ef), Python 3.13:

Textual 8.2.7, Python 3.13.18
run():      ['task started', 'on_mount continued']
run_test(): ['on_mount continued', 'task started']

So a test can pass while the real app behaves differently, or the other way round. #6444 is one example: it crashes under run() but not under run_test().

The obvious fix is to set the same factory in run_test() (and restore the previous one on exit). I tried that on main and it's not quite enough on its own:

  • The order above then matches run().
  • Three tests in tests/command_palette/test_declare_sources.py fail (test_no_screen_command_sources, test_screen_command_sources, test_app_and_screen_command_sources_combine). They push a screen in on_mount whose own on_mount opens the command palette. With the eager factory, run_test() gives a screen stack of ['Screen', 'CommandPalette', 'PaletteOnMount'], so the palette ends up under the screen that opened it. Under run() the same app gives ['Screen', 'PaletteOnMount', 'CommandPalette']. I think that's the same problem as #6444 (a screen's children start before the screen is on the stack), so there may be a startup difference between the two paths beyond the task factory.
  • The rest of the suite is unchanged.

Is run_test() meant to match run() here? If so, I'm happy to open a PR that sets the factory, but the three tests above would need #6444 (or a change to how run_test() starts the app) first, so I wanted to check which way you'd prefer.

主要言語
Python
スター
37.7k
フォーク
1.5k
PR マージ指標
30日以内にマージされた PR はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Textualize/textual のほかの issue

Textualize/textual の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。