Skip to content

Change list() cast#1401

Merged
tvst merged 14 commits intostreamlit:developfrom
arnaudmiribel:multiselect_default_list
May 7, 2020
Merged

Change list() cast#1401
tvst merged 14 commits intostreamlit:developfrom
arnaudmiribel:multiselect_default_list

Conversation

@arnaudmiribel
Copy link
Copy Markdown
Collaborator


Issue: 1397 - Multiselect check for defaults goes wrong if not a list

Description:

  • Use list(iterable) instead of [iterable] to avoid errors (see Issue) because if iterable is a tuple, then it converts not to a list, but to a list of tuple. Also from SO it looks like a preferred practice overall

Contribution License Agreement

By submiting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.

if not isinstance(default_values, list):
default_values = [default_values]
default_values = (
list(default_values) if default_values else [default_values]
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the if in order to handle when default_values=None

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.

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)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated now. Became even more necessary as I now have if/elif/else! Thanks.

@arnaudmiribel arnaudmiribel marked this pull request as ready for review April 30, 2020 09:12
Copy link
Copy Markdown
Contributor

@tvst tvst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

if not isinstance(default_values, list):
default_values = [default_values]
default_values = (
list(default_values) if default_values else [default_values]
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.

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)

@arnaudmiribel arnaudmiribel marked this pull request as draft May 4, 2020 13:51
Comment on lines +1724 to +1727
if is_type(default_values, "numpy.ndarray") or is_type(
default_values, "pandas.core.series.Series"
):
default_values = list(default_values)
Copy link
Copy Markdown
Collaborator Author

@arnaudmiribel arnaudmiribel May 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

Cool, so I just added what you wrote here to the code for future reference.

Comment on lines +1728 to +1729
elif not default_values:
default_values = [default_values]
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful for empty objects e.g. x in ("", None, {})

@arnaudmiribel
Copy link
Copy Markdown
Collaborator Author

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()

Tests added! Created a new function dedicated to tests for defaults iterator types def test_default_types() checking for tuples, iterators and pd.Series() or np.array()

@arnaudmiribel arnaudmiribel marked this pull request as ready for review May 4, 2020 16:11
@arnaudmiribel arnaudmiribel requested a review from tvst May 4, 2020 16:12
Comment on lines +1724 to +1727
if is_type(default_values, "numpy.ndarray") or is_type(
default_values, "pandas.core.series.Series"
):
default_values = list(default_values)
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.

Cool, so I just added what you wrote here to the code for future reference.

@tvst tvst merged commit 7851176 into streamlit:develop May 7, 2020
tconkling added a commit that referenced this pull request May 11, 2020
* 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)
tconkling added a commit to tconkling/streamlit that referenced this pull request May 11, 2020
* 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)
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.

2 participants