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

get_option in SelectionList not working #3903

Closed
stefgmz opened this issue Dec 19, 2023 · 4 comments · Fixed by #3988
Closed

get_option in SelectionList not working #3903

stefgmz opened this issue Dec 19, 2023 · 4 comments · Fixed by #3988
Assignees

Comments

@stefgmz
Copy link
Contributor

stefgmz commented Dec 19, 2023

The function get_option in SelectionList does not work as intended. The following raises OptionDoesNotExist

from textual.app import App
from textual.widgets import SelectionList
from textual.widgets.selection_list import Selection
class QuickApp(App):

    def compose(self):
        yield SelectionList(
            Selection("a", 1, id="a")
        )

    def on_mount(self):
        list = self.query_one(SelectionList)
        list.get_option("a")

I looked through the code and it looks like _option_ids is never set in OptionList but gets evaluated in get_option_index.

@davep
Copy link
Contributor

davep commented Dec 19, 2023

This would seem to be down to the internals of the underlying OptionList not being fully-built until the widget itself is fully displayed (which it won't be in on_mount). Compare the above with:

from textual.app import App
from textual.widgets import SelectionList
from textual.widgets.selection_list import Selection

class QuickApp(App):

    BINDINGS = [
        ("f", "find"),
    ]

    def compose(self):
        yield SelectionList(
            Selection("a", 1, id="a")
        )

    def action_find(self):
        self.notify(f"{self.query_one(SelectionList).get_option('a')}")

if __name__ == "__main__":
    QuickApp().run()

Once the app is up and running, press f.

(aside: IIRC this isn't the only widget in Textual where this can be a gotcha).

@Textualize Textualize deleted a comment from github-actions bot Dec 19, 2023
@rodrigogiraoserrao
Copy link
Contributor

Given Dave's clarification, I'm not sure we can do a lot here...
Maybe in get_option raise a different exception saying it's too early to query options by ID?

@willmcgugan
Copy link
Collaborator

This should work. The Mount message is a guarantee that child widgets are ready to be used. We don't want to deviate from that.

@rodrigogiraoserrao rodrigogiraoserrao self-assigned this Jan 9, 2024
rodrigogiraoserrao added a commit that referenced this issue Jan 9, 2024
This builds option IDs earlier than the first render, which means that IDs can be used to fetch options earlier.
Related issue: #3903.
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

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 a pull request may close this issue.

4 participants