Skip to content

Fix IndexError building a single-select selector with no options - #1849

Merged
petar-qb merged 3 commits into
mckinsey:mainfrom
winklemad:fix/empty-single-select-default-value
Aug 31, 2026
Merged

petar-qb merged 3 commits into
mckinsey:mainfrom
winklemad:fix/empty-single-select-default-value

Conversation

@winklemad

Copy link
Copy Markdown
Contributor

Description

Building a single-select selector with no options raised IndexError:

import vizro.models as vm
vm.RadioItems().build()           # IndexError: list index out of range
vm.Dropdown(multi=False).build()  # IndexError: list index out of range

options defaults to [] on RadioItems, Dropdown and Checklist, so a default-constructed single-select selector hits this. get_dict_options_and_value derived the single-select default value as list_values[0] with no empty guard; it now falls back to None when there is nothing to select. Multi-select already returned the empty list, and populated selectors are unchanged.

Added test_radio_items_build_empty_options and test_dropdown_build_empty_options_single (both fail on main with IndexError, pass with this change).

Screenshot

n/a (crash fix)

Notice

  • I acknowledge and agree that, by checking this box and clicking "Submit Pull Request":

    • I submit this contribution under the Apache 2.0 license and represent that I am entitled to do so on behalf of myself, my employer, or relevant third parties, as applicable.
    • I certify that (a) this contribution is my original creation and / or (b) to the extent it is not my original creation, I am authorized to submit this contribution on behalf of the original creator(s) or their licensees.
    • I certify that the use of this contribution as authorized by the Apache 2.0 license does not violate the intellectual property rights of anyone else.
    • I have not referenced individuals, products or companies in any commits, directly or indirectly.
    • I have not added data or restricted code in any commits, directly or indirectly.

get_dict_options_and_value derived the single-select default value as
list_values[0] with no empty guard. options default to [] on RadioItems,
Dropdown and Checklist, so building a single-select selector without options
(RadioItems().build(), Dropdown(multi=False).build()) raised IndexError. Fall
back to None when there is nothing to select. Multi-select already returned the
empty list, and populated selectors are unchanged.
Copilot AI lite review requested due to automatic review settings August 31, 2026 01:59

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

🟢 Approval recommended

Pull request overview

Fixes an IndexError when building single-select categorical selectors with empty options by ensuring a safe default value is used, and adds regression tests to prevent reintroducing the crash.

Changes:

  • Update get_dict_options_and_value to return None as the default single-select value when options is empty.
  • Add unit tests covering empty-options builds for RadioItems and single-select Dropdown.
  • Add a changelog entry describing the fix.
File summaries
File Description
vizro-core/src/vizro/models/_components/form/_form_utils.py Guard single-select default value derivation for empty options to avoid IndexError.
vizro-core/tests/unit/vizro/models/_components/form/test_radio_items.py Add regression test asserting empty-options RadioItems.build() returns value=None.
vizro-core/tests/unit/vizro/models/_components/form/test_dropdown.py Add regression test asserting empty-options single-select Dropdown.build() returns value=None.
vizro-core/changelog.d/20260831_120000_winklemad.md Document the crash fix in the changelog.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@petar-qb petar-qb 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.

Great as always @winklemad! 🚀 Thanks for another contribution 😃

@petar-qb
petar-qb merged commit 3258c39 into mckinsey:main Aug 31, 2026
60 of 62 checks passed
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.

3 participants