Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for action targets #4546

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.63.2] - 2024-05-23

### Fixed

- Fixed issue with namespaces in links https://github.com/Textualize/textual/pull/4546

## [0.63.1] - 2024-05-22

### Fixed
Expand Down Expand Up @@ -1996,6 +2002,7 @@ https://textual.textualize.io/blog/2022/11/08/version-040/#version-040
- New handler system for messages that doesn't require inheritance
- Improved traceback handling

[0.63.2]: https://github.com/Textualize/textual/compare/v0.63.1...v0.63.2
[0.63.1]: https://github.com/Textualize/textual/compare/v0.63.0...v0.63.1
[0.63.0]: https://github.com/Textualize/textual/compare/v0.62.0...v0.63.0
[0.62.0]: https://github.com/Textualize/textual/compare/v0.61.1...v0.62.0
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "textual"
version = "0.63.1"
version = "0.63.2"
homepage = "https://github.com/Textualize/textual"
repository = "https://github.com/Textualize/textual"
documentation = "https://textual.textualize.io/"
Expand Down
19 changes: 9 additions & 10 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3038,7 +3038,7 @@ async def on_event(self, event: events.Event) -> None:
await super().on_event(event)

def _parse_action(
self, action: str, default_namespace: DOMNode
self, action: str | ActionParseResult, default_namespace: DOMNode
) -> tuple[DOMNode, str, tuple[object, ...]]:
"""Parse an action.

Expand All @@ -3051,7 +3051,11 @@ def _parse_action(
Returns:
A tuple of (node or None, action name, tuple of parameters).
"""
destination, action_name, params = actions.parse(action)
if isinstance(action, tuple):
destination, action_name, params = action
else:
destination, action_name, params = actions.parse(action)

action_target: DOMNode | None = None
if destination:
if destination not in self._action_targets:
Expand Down Expand Up @@ -3098,14 +3102,9 @@ async def run_action(
Returns:
True if the event has been handled.
"""
if isinstance(action, str):
action_target, action_name, params = self._parse_action(
action, self if default_namespace is None else default_namespace
)
else:
# assert isinstance(action, tuple)
_, action_name, params = action
action_target = self
action_target, action_name, params = self._parse_action(
action, self if default_namespace is None else default_namespace
)

if action_target.check_action(action_name, params):
return await self._dispatch_action(action_target, action_name, params)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_data_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ def compose(self) -> ComposeResult:

def on_mount(self) -> None:
table = self.query_one(DataTable)
table.border_title = "[@click=test_link]Border Link[/]"
table.border_title = "[@click=app.test_link]Border Link[/]"

def action_test_link(self) -> None:
self.link_clicked = True
Expand Down
7 changes: 3 additions & 4 deletions tests/test_issue_4248.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ async def test_issue_4248() -> None:
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")
yield Label("[@click=app.bump]click me[/]", id="known-sans-parens")
yield Label("[@click=app.bump()]click me[/]", id="known-empty-parens")
yield Label("[@click=app.bump(100)]click me[/]", id="known-with-param")

def action_bump(self, by_value: int = 1) -> None:
nonlocal bumps
Expand Down
21 changes: 17 additions & 4 deletions tests/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,35 @@
from textual.widgets import Label


async def test_links():
async def test_links() -> None:
"""Regression test for https://github.com/Textualize/textual/issues/4536"""
messages: list[str] = []

class LinkLabel(Label):
def action_bell_message(self, message: str) -> None:
nonlocal messages
messages.append(f"label {message}")

class HomeScreen(Screen[None]):
def compose(self) -> ComposeResult:
yield Label("[@click=app.bell_message('hi')]Ring the bell![/]")
yield LinkLabel("[@click=app.bell_message('foo')]Ring the bell![/]")
yield LinkLabel("[@click=screen.bell_message('bar')]Ring the bell![/]")
yield LinkLabel("[@click=bell_message('baz')]Ring the bell![/]")

def action_bell_message(self, message: str) -> None:
nonlocal messages
messages.append(f"screen {message}")

class ScreenNamespace(App[None]):
def get_default_screen(self) -> HomeScreen:
return HomeScreen()

def action_bell_message(self, message: str) -> None:
nonlocal messages
messages.append(message)
messages.append(f"app {message}")

async with ScreenNamespace().run_test() as pilot:
await pilot.click(offset=(5, 0))
assert messages == ["hi"]
await pilot.click(offset=(5, 1))
await pilot.click(offset=(5, 2))
assert messages == ["app foo", "screen bar", "label baz"]
Loading