Change list() cast#1401
Change list() cast#1401tvst merged 14 commits intostreamlit:developfrom arnaudmiribel:multiselect_default_list
Conversation
lib/streamlit/DeltaGenerator.py
Outdated
| if not isinstance(default_values, list): | ||
| default_values = [default_values] | ||
| default_values = ( | ||
| list(default_values) if default_values else [default_values] |
There was a problem hiding this comment.
Added the if in order to handle when default_values=None
There was a problem hiding this comment.
That inline if is a bit hard to read.
How about:
if default_values is None:
default_values = [None]
else:
default_values = list(default_values)
There was a problem hiding this comment.
Updated now. Became even more necessary as I now have if/elif/else! Thanks.
tvst
left a comment
There was a problem hiding this comment.
Thanks for the fix!
Can you also add a test for this? Should be fairly simple to add a test case in lib/tests/streamlit/multiselect_test.py for tuple and iterator inputs. See the decorator of def test_option_types()
lib/streamlit/DeltaGenerator.py
Outdated
| if not isinstance(default_values, list): | ||
| default_values = [default_values] | ||
| default_values = ( | ||
| list(default_values) if default_values else [default_values] |
There was a problem hiding this comment.
That inline if is a bit hard to read.
How about:
if default_values is None:
default_values = [None]
else:
default_values = list(default_values)
| if is_type(default_values, "numpy.ndarray") or is_type( | ||
| default_values, "pandas.core.series.Series" | ||
| ): | ||
| default_values = list(default_values) |
There was a problem hiding this comment.
Support for pd and np:
This if is done before others because calling if not x (done right below) when x is of type pd.Series() or np.array() throws a ValueError exception.
Also added protos of that in the tests.
There was a problem hiding this comment.
Cool, so I just added what you wrote here to the code for future reference.
| elif not default_values: | ||
| default_values = [default_values] |
There was a problem hiding this comment.
Useful for empty objects e.g. x in ("", None, {})
Tests added! Created a new function dedicated to tests for defaults iterator types |
| if is_type(default_values, "numpy.ndarray") or is_type( | ||
| default_values, "pandas.core.series.Series" | ||
| ): | ||
| default_values = list(default_values) |
There was a problem hiding this comment.
Cool, so I just added what you wrote here to the code for future reference.
* develop: Add "make mini-devel" to install minimal dev dependencies (i.e. doesn't install all the test dependencies) (#1407) Fixing date_input | min and max selectable date issues (#1426) Torch Tensorbase hash func (#1394) Change list() cast (#1401) Add geo layers to DeckGlJsonChart (#1306) Clean up use of LoDash (#1404) Replace st.beta.*/st.experimental.* with st.beta_*/st.experimental_* (#1403) Release 0.59.0 (#1405) Setting textarea height and unit tests (#1411)
* feature/plugins: black reformatting Add "make mini-devel" to install minimal dev dependencies (i.e. doesn't install all the test dependencies) (streamlit#1407) Fixing date_input | min and max selectable date issues (streamlit#1426) Torch Tensorbase hash func (streamlit#1394) Change list() cast (streamlit#1401) Component template tweaks Components: alpha 2 cleanup (streamlit#1425) Fix dataframe support Add geo layers to DeckGlJsonChart (streamlit#1306) Clean up use of LoDash (streamlit#1404) Replace st.beta.*/st.experimental.* with st.beta_*/st.experimental_* (streamlit#1403) Release 0.59.0 (streamlit#1405) Setting textarea height and unit tests (streamlit#1411)
Issue: 1397 - Multiselect check for defaults goes wrong if not a list
Description:
list(iterable)instead of[iterable]to avoid errors (see Issue) because ifiterableis a tuple, then it converts not to a list, but to a list of tuple. Also from SO it looks like a preferred practice overallContribution License Agreement
By submiting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.