-
-
Notifications
You must be signed in to change notification settings - Fork 928
added textual.getters #5930
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
Merged
Merged
added textual.getters #5930
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
title: "textual.getters" | ||
--- | ||
|
||
::: textual.getters |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
""" | ||
Descriptors to define properties on your widget, screen, or App. | ||
|
||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Generic, overload | ||
|
||
from textual.css.query import NoMatches, QueryType, WrongType | ||
from textual.dom import DOMNode | ||
from textual.widget import Widget | ||
|
||
|
||
class query_one(Generic[QueryType]): | ||
"""Create a query one property. | ||
|
||
A query one property calls [Widget.query_one][textual.dom.DOMNode.query_one] when accessed, and returns | ||
a widget. If the widget doesn't exist, then the property will raise the same exceptions as `Widget.query_one`. | ||
|
||
|
||
Example: | ||
```python | ||
from textual import getters | ||
|
||
class MyScreen(screen): | ||
|
||
# Note this is at the class level | ||
output_log = getters.query_one("#output", RichLog) | ||
|
||
def compose(self) -> ComposeResult: | ||
with containers.Vertical(): | ||
yield RichLog(id="output") | ||
|
||
def on_mount(self) -> None: | ||
self.output_log.write("Screen started") | ||
# Equivalent to the following line: | ||
# self.query_one("#output", RichLog).write("Screen started") | ||
``` | ||
|
||
|
||
""" | ||
|
||
selector: str | ||
expect_type: type[Widget] | ||
|
||
@overload | ||
def __init__(self, selector: str) -> None: | ||
self.selector = selector | ||
self.expect_type = Widget | ||
|
||
@overload | ||
def __init__(self, selector: type[QueryType]) -> None: | ||
self.selector = selector.__name__ | ||
self.expect_type = selector | ||
|
||
@overload | ||
def __init__(self, selector: str, expect_type: type[QueryType]) -> None: | ||
self.selector = selector | ||
self.expect_type = expect_type | ||
|
||
@overload | ||
def __init__(self, selector: type[QueryType], expect_type: type[QueryType]) -> None: | ||
self.selector = selector.__name__ | ||
self.expect_type = expect_type | ||
|
||
def __init__( | ||
self, | ||
selector: str | type[QueryType], | ||
expect_type: type[QueryType] | None = None, | ||
) -> None: | ||
if expect_type is None: | ||
self.expect_type = Widget | ||
else: | ||
self.expect_type = expect_type | ||
if isinstance(selector, str): | ||
self.selector = selector | ||
else: | ||
self.selector = selector.__name__ | ||
self.expect_type = selector | ||
|
||
@overload | ||
def __get__( | ||
self: "query_one[QueryType]", obj: DOMNode, obj_type: type[DOMNode] | ||
) -> QueryType: ... | ||
|
||
@overload | ||
def __get__( | ||
self: "query_one[QueryType]", obj: None, obj_type: type[DOMNode] | ||
) -> "query_one[QueryType]": ... | ||
|
||
def __get__( | ||
self: "query_one[QueryType]", obj: DOMNode | None, obj_type: type[DOMNode] | ||
) -> QueryType | Widget | "query_one": | ||
"""Get the widget matching the selector and/or type.""" | ||
if obj is None: | ||
return self | ||
query_node = obj.query_one(self.selector, self.expect_type) | ||
return query_node | ||
|
||
|
||
class child_by_id(Generic[QueryType]): | ||
"""Create a child_by_id property, which returns the child with the given ID. | ||
|
||
This is similar using [query_one][textual.getters.query_one] with an id selector, except that | ||
only the immediate children are considered. It is also more efficient, as it doesn't need to search the DOM. | ||
|
||
|
||
Example: | ||
```python | ||
from textual import getters | ||
|
||
class MyScreen(screen): | ||
|
||
# Note this is at the class level | ||
output_log = getters.child_by_id("#output", RichLog) | ||
|
||
def compose(self) -> ComposeResult: | ||
yield RichLog(id="output") | ||
|
||
def on_mount(self) -> None: | ||
self.output_log.write("Screen started") | ||
``` | ||
|
||
|
||
""" | ||
|
||
child_id: str | ||
expect_type: type[Widget] | ||
|
||
@overload | ||
def __init__(self, child_id: str) -> None: | ||
self.child_id = child_id | ||
self.expect_type = Widget | ||
|
||
@overload | ||
def __init__(self, child_id: str, expect_type: type[QueryType]) -> None: | ||
self.child_id = child_id | ||
self.expect_type = expect_type | ||
|
||
def __init__( | ||
self, | ||
child_id: str, | ||
expect_type: type[QueryType] | None = None, | ||
) -> None: | ||
if expect_type is None: | ||
self.expect_type = Widget | ||
else: | ||
self.expect_type = expect_type | ||
self.child_id = child_id | ||
|
||
@overload | ||
def __get__( | ||
self: "child_by_id[QueryType]", obj: DOMNode, obj_type: type[DOMNode] | ||
) -> QueryType: ... | ||
|
||
@overload | ||
def __get__( | ||
self: "child_by_id[QueryType]", obj: None, obj_type: type[DOMNode] | ||
) -> "child_by_id[QueryType]": ... | ||
|
||
def __get__( | ||
self: "child_by_id[QueryType]", obj: DOMNode | None, obj_type: type[DOMNode] | ||
) -> QueryType | Widget | "child_by_id": | ||
"""Get the widget matching the selector and/or type.""" | ||
if obj is None: | ||
return self | ||
child = obj._nodes._get_by_id(self.child_id) | ||
if child is None: | ||
raise NoMatches(f"No child found with id={id!r}") | ||
if not isinstance(child, self.expect_type): | ||
if not isinstance(child, self.expect_type): | ||
raise WrongType( | ||
f"Child with id={id!r} is wrong type; expected {self.expect_type}, got" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the |
||
f" {type(child)}" | ||
) | ||
return child |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pytest | ||
|
||
from textual import containers, getters | ||
from textual.app import App, ComposeResult | ||
from textual.css.query import NoMatches, WrongType | ||
from textual.widget import Widget | ||
from textual.widgets import Input, Label | ||
|
||
|
||
async def get_getters() -> None: | ||
"""Check the getter descriptors work, and return expected errors.""" | ||
|
||
class QueryApp(App): | ||
label1 = getters.query_one("#label1", Label) | ||
label2 = getters.child_by_id("label2", Label) | ||
label1_broken = getters.query_one("#label1", Input) | ||
label2_broken = getters.child_by_id("label2", Input) | ||
label1_missing = getters.query_one("#foo", Widget) | ||
label2_missing = getters.child_by_id("bar", Widget) | ||
|
||
def compose(self) -> ComposeResult: | ||
with containers.Vertical(): | ||
yield Label(id="label1", classes=".red") | ||
yield Label(id="label2", classes=".green") | ||
|
||
app = QueryApp() | ||
async with app.run_test(): | ||
|
||
assert isinstance(app.label1, Label) | ||
assert app.label1.id == "label1" | ||
|
||
assert isinstance(app.label2, Label) | ||
assert app.label2.id == "label2" | ||
|
||
with pytest.raises(WrongType): | ||
app.label1_broken | ||
|
||
with pytest.raises(WrongType): | ||
app.label2_broken | ||
|
||
with pytest.raises(NoMatches): | ||
app.label1_missing | ||
|
||
with pytest.raises(NoMatches): | ||
app.label2_missing |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the
id
should beself.child_id
.