OptionList: page down raises `KeyError` when the list has no room to show options

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

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

評価

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

調査の方向性

クラッシュの原因は src/textual/widgets/_option_list.py の _move_page にあり、この関数は _index_to_line を確認せずにインデックス参照しています。scrollable_content_region が空のとき _update_lines() は早期に return するため、そのマップは空のままになります。issue のスニペットで再現してから、上下方向の処理と同じ方法で _move_page にガードを入れてください。そのケースで page up と page down が何もしないこと、そしてそれをカバーするリグレッションテストがあることが完了条件です。

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

説明

Hi! I hit this while writing generated tests against Textual's widgets.

If an OptionList has no content area (for example its border uses all of its height), pressing page down or page up crashes the app with KeyError.

import asyncio
from textual.app import App, ComposeResult
from textual.widgets import OptionList

class PageApp(App):
    # The border uses both rows, so the list has no room to show any options.
    CSS = "OptionList { height: 2; }"

    def compose(self) -> ComposeResult:
        yield OptionList("one", "two", "three")

async def main() -> None:
    app = PageApp()
    async with app.run_test() as pilot:
        option_list = app.query_one(OptionList)
        option_list.highlighted = 0
        print("content region:", option_list.scrollable_content_region)
        await pilot.press("pagedown")

asyncio.run(main())

Output (the same on 8.2.7 and on main at 5e5b7ef):

  File ".../textual/widgets/_option_list.py", line 1026, in action_page_down
    self._move_page(1)
  File ".../textual/widgets/_option_list.py", line 1002, in _move_page
    self._index_to_line[self.highlighted or 0] + direction * height,
KeyError: 0

It seems _update_lines() returns early when scrollable_content_region is empty, so _index_to_line stays empty, and _move_page() only checks for an empty _options.

I'd expect page up / page down to do nothing in that case, as up and down already do.

Textual 8.2.7 and main (5e5b7ef), Python 3.13, macOS.

主要言語
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 を短くまとめたダイジェスト。