-
Notifications
You must be signed in to change notification settings - Fork 788
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
OptionList
duplicate ID detection not working as expected
#3455
Comments
I found a quick workaround would be: except Exception as error:
self.query_one(Log).write_line(f"{error!r}")
self.query_one(OptionList)._contents.pop() The exception is raised by textual/src/textual/widgets/_option_list.py Lines 543 to 546 in 6d79e77
|
Yeah, pretty sure the duplicate check needs to go waaaaaaay before anything gets added. |
Smoking gun. diff --git a/tests/option_list/test_option_list_create.py b/tests/option_list/test_option_list_create.py
index 7e0bbc49..98130d75 100644
--- a/tests/option_list/test_option_list_create.py
+++ b/tests/option_list/test_option_list_create.py
@@ -119,5 +119,8 @@ async def test_add_later() -> None:
async def test_create_with_duplicate_id() -> None:
"""Adding an option with a duplicate ID should be an error."""
async with OptionListApp().run_test() as pilot:
+ option_list = pilot.app.query_one(OptionList)
+ assert option_list.option_count == 5
with pytest.raises(DuplicateID):
- pilot.app.query_one(OptionList).add_option(Option("dupe", id="3"))
+ option_list.add_option(Option("dupe", id="3"))
+ assert option_list.option_count == 5``` with that change the tests it fails. |
Simply put: until now the OptionList was adding the new options and then checking if the ID was a duplicate. If some code was to then catch DuplicateID and treat it as a pass this would then have an unintended side-effect that the duplicate had been added anyway. The ultimate effect of this being that once there was an attempt to add a duplicate, nothing more could be added to that OptionList. Fixes Textualize#3455.
Don't forget to star the repository! Follow @textualizeio for Textual updates. |
Consider the following code:
If you run it and press keys 0-9, you'll find that corresponding options are added to the
OptionList
.However, if you start the app afresh, then press (for example) 2 twice, resulting in a logged
DuplicateID
exception (expected and desired), after than you can't add any more options to the list, and the attempted added option is always reported as having the original duplicate ID:In the above I pressed 2 twice and then a number of other number keys.
It would appear that
OptionList
is getting every confused in itsid
tracking and/or duplicate detection.The text was updated successfully, but these errors were encountered: