Skip to content

added textual.getters#5930

Merged
willmcgugan merged 6 commits into
mainfrom
getters
Jul 7, 2025
Merged

added textual.getters#5930
willmcgugan merged 6 commits into
mainfrom
getters

Conversation

@willmcgugan

@willmcgugan willmcgugan commented Jul 6, 2025

Copy link
Copy Markdown
Member

Good for DRY

  • Adds getters.query_one
  • Adds getters.child_by_id

These create nicely typed properties which return a Widget.

class MyWidget(Widget):
    @property
    def my_input(self) -> Input:
        return self.query_one("#my_input", Input)

Can be replaced with:

class MyWidget(Widget):
    my_input = getters.query_one("#my_input", Input)

Works nicely, just don't look behind the curtain.

@willmcgugan willmcgugan merged commit db623fa into main Jul 7, 2025
23 checks passed
@willmcgugan willmcgugan deleted the getters branch July 7, 2025 12:30

@python-and-novella python-and-novella left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find a mistake in error info.

Comment thread src/textual/getters.py
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}")

Copy link
Copy Markdown
Contributor

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 be self.child_id.

Comment thread src/textual/getters.py
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"

Copy link
Copy Markdown
Contributor

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 be self.child_id.

@python-and-novella

Copy link
Copy Markdown
Contributor

query_one's __get__ is using obj.query_one,
I think it is maybe better for child_by_id's __get__ to use obj.get_child_by_id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants