`DataTable.RowLabelSelected.__rich_repr__` raises `AttributeError` when the row has no label

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

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

評価

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

調査の方向性

クラッシュは textual/widgets/_data_table.py 内の DataTable.RowLabelSelected の __rich_repr__ で発生しており、約676行目で self.label.plain を呼び出しています。_on_click を確認してください。これは row.label を渡しており、ラベルのない行では None になります。issue の再現手順を実行したうえで、pretty_repr(event) が例外を出さずに label=None を出力すること、そして label の型注釈が Text | None を許容していることを確認してください。

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

説明

Hi! I hit this while writing generated tests against Textual's widgets. In a table where some rows have labels, clicking the label cell of a row added without one posts RowLabelSelected with label=None, and the message's repr raises.

from rich.pretty import pretty_repr
from textual.app import App, ComposeResult
from textual.widgets import DataTable

class RowLabelApp(App):
    def compose(self) -> ComposeResult:
        yield DataTable()

    def on_mount(self) -> None:
        table = self.query_one(DataTable)
        table.add_column("A")
        table.add_row("x", label="one")
        table.add_row("y")  # no label

    def on_data_table_row_label_selected(self, event: DataTable.RowLabelSelected) -> None:
        print("message:", pretty_repr(event))

if __name__ == "__main__":
    RowLabelApp().run()  # then click the label cell of the second row

Expected: RowLabelSelected(row_key=..., row_index=1, label=None).

Actual (on Textual 8.2.7, and main at 5e5b7ef):

  File "textual/widgets/_data_table.py", line 676, in __rich_repr__
    yield "label", self.label.plain
AttributeError: 'NoneType' object has no attribute 'plain'

label is annotated as Text, but _on_click passes row.label, which is None for an unlabelled row. I think the same repr is used when messages are logged to textual console, though I've only checked it directly.

I'd suggest yielding None for a missing label and widening the annotation to Text | None. I'm happy to send a PR.

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