Skip to content

Commit

Permalink
Merge pull request #4313 from davep/bug/4248/click-markup-crash
Browse files Browse the repository at this point in the history
Fix a crash in `run_action` when an action is an empty tuple
  • Loading branch information
willmcgugan authored Mar 26, 2024
2 parents 482cf24 + 0907da1 commit ba9a913
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3006,11 +3006,18 @@ async def _broker_event(
return False
else:
event.stop()
if isinstance(action, (str, tuple)):
if isinstance(action, str) or (isinstance(action, tuple) and len(action) == 2):
await self.run_action(action, default_namespace=default_namespace) # type: ignore[arg-type]
elif callable(action):
await action()
else:
if isinstance(action, tuple) and self.debug:
# It's a tuple and made it this far, which means it'll be a
# malformed action. This is a no-op, but let's log that
# anyway.
log.warning(
f"Can't parse @{event_name} action from style meta; check your console markup syntax"
)
return False
return True

Expand Down
46 changes: 46 additions & 0 deletions tests/test_issue_4248.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Test https://github.com/Textualize/textual/issues/4248"""

from textual.app import App, ComposeResult
from textual.widgets import Label


async def test_issue_4248() -> None:
"""Various forms of click parameters should be fine."""

bumps = 0

class ActionApp(App[None]):

def compose(self) -> ComposeResult:
yield Label("[@click]click me and crash[/]", id="nothing")
yield Label("[@click=]click me and crash[/]", id="no-params")
yield Label("[@click=()]click me and crash[/]", id="empty-params")
yield Label("[@click=foobar]click me[/]", id="unknown-sans-parens")
yield Label("[@click=foobar()]click me[/]", id="unknown-with-parens")
yield Label("[@click=bump]click me[/]", id="known-sans-parens")
yield Label("[@click=bump()]click me[/]", id="known-empty-parens")
yield Label("[@click=bump(100)]click me[/]", id="known-with-param")

def action_bump(self, by_value: int = 1) -> None:
nonlocal bumps
bumps += by_value

app = ActionApp()
async with app.run_test() as pilot:
assert bumps == 0
await pilot.click("#nothing")
assert bumps == 0
await pilot.click("#no-params")
assert bumps == 0
await pilot.click("#empty-params")
assert bumps == 0
await pilot.click("#unknown-sans-parens")
assert bumps == 0
await pilot.click("#unknown-with-parens")
assert bumps == 0
await pilot.click("#known-sans-parens")
assert bumps == 1
await pilot.click("#known-empty-parens")
assert bumps == 2
await pilot.click("#known-with-param")
assert bumps == 102

0 comments on commit ba9a913

Please sign in to comment.