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

AppTest fails on re-run if selectbox options are non-strings #7679

Closed
3 of 4 tasks
NakulK48 opened this issue Nov 6, 2023 · 1 comment
Closed
3 of 4 tasks

AppTest fails on re-run if selectbox options are non-strings #7679

NakulK48 opened this issue Nov 6, 2023 · 1 comment

Comments

@NakulK48
Copy link

NakulK48 commented Nov 6, 2023

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

In regular Streamlit, the options argument of a selectbox can be any iterable, as long as there is a format_func provided to go from the item type to a string.

However, testing.v1.element_tree contains the function:

    @property
    def index(self) -> int | None:
        """The index of the current selection. (int)"""
        if self.value is None:
            return None

        if len(self.options) == 0:
            return 0
        return self.options.index(str(self.value))

What ends up happening here is that self.value is of the type of the actual element (populated during the first call to run(), and self.options is a list of strings. As a result, Python raises an error because the value is not in the list of options.

Reproducible Code Example

def test_broken_streamlit():
    def func():
        from dataclasses import dataclass

        @dataclass
        class Foo:
            name: str

        import streamlit as st
        st.selectbox("foo", options=[Foo("bar"), Foo("baz")], format_func=lambda x: x.name)

    at = AppTest.from_function(func)
    at.run()
    at.run()

(the example is contrived for simplicity: more likely you'd do something like at.text_input("some_element").input("boo").run())

Steps To Reproduce

No response

Expected Behavior

Either options should be a list of Foo (this seems more sensible to me), or value should be the formatted string

Current Behavior

self = Selectbox(_value=InitialValue(), label='foo', options=['bar', 'baz'])

    @property
    def index(self) -> int | None:
        """The index of the current selection. (int)"""
        if self.value is None:
            return None

        if len(self.options) == 0:
            return 0
>       return self.options.index(str(self.value))
E       ValueError: "Foo(name='bar')" is not in list

../../mambaforge/envs/dlf-ui/lib/python3.10/site-packages/streamlit/testing/v1/element_tree.py:930: ValueError

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.28.0
  • Python version: 3.10.8
  • Operating System:
  • Browser: AppTest

Additional Information

No response

@NakulK48 NakulK48 added status:needs-triage Has not been triaged by the Streamlit team type:bug Something isn't working labels Nov 6, 2023
Copy link

github-actions bot commented Nov 6, 2023

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Visits

@mayagbarnes mayagbarnes added feature:app-testing feature:st.selectbox priority:P3 and removed status:needs-triage Has not been triaged by the Streamlit team labels Nov 6, 2023
AnOctopus added a commit that referenced this issue Mar 7, 2024
## Describe your changes
Fixes issues with selecting items in widgets when `format_func` is used,
by storing the `format_func` in session state during app tests and using
it to convert the provided values into the ones stored by the protobuf.

## GitHub Issue Link (if applicable)
#7679 and #8019 

## Testing Plan

App tests added
zyxue pushed a commit to zyxue/streamlit that referenced this issue Apr 16, 2024
## Describe your changes
Fixes issues with selecting items in widgets when `format_func` is used,
by storing the `format_func` in session state during app tests and using
it to convert the provided values into the ones stored by the protobuf.

## GitHub Issue Link (if applicable)
streamlit#7679 and streamlit#8019 

## Testing Plan

App tests added
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants