Skip to content

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 6 commits into from
Jul 7, 2025
Merged

added textual.getters #5930

merged 6 commits into from
Jul 7, 2025

Conversation

willmcgugan
Copy link
Member

@willmcgugan willmcgugan commented Jul 6, 2025

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
Copy link
Contributor

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

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.

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
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.

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
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
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