Skip to content

Conversation

@sfc-gh-tszerszen
Copy link
Contributor

📚 Context

Please describe the project or issue background here

  • What kind of change does this PR introduce?

This PR adds step parameter to st.time_input as a keyword only optional param.

  • Bugfix
  • Feature
  • Refactoring
  • Other, please describe:

🧠 Description of Changes

  • Add bullet points summarizing your changes here

    • This is a breaking API change
    • This is a visible (user-facing) change

Revised:

Insert screenshot of your updated UI/code here

Current:

Insert screenshot of existing UI/code here

🧪 Testing Done

  • Screenshots included
  • Added/Updated unit tests
  • Added/Updated e2e tests

🌐 References

Does this depend on other work, documents, or tickets?

  • Issue: Closes #XXXX

Contribution License Agreement

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

@sfc-gh-tszerszen sfc-gh-tszerszen added the security-assessment-completed Security assessment has been completed for PR label Feb 6, 2023
@sfc-gh-tszerszen sfc-gh-tszerszen self-assigned this Feb 6, 2023
@sfc-gh-tszerszen sfc-gh-tszerszen force-pushed the add-step-parameter-to-st-time-input branch 2 times, most recently from 36009e6 to 9f2fa46 Compare February 7, 2023 16:09
Copy link
Contributor

@sfc-gh-mnowotka sfc-gh-mnowotka left a comment

Choose a reason for hiding this comment

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

I think we should implement some type checking.

*, # keyword-only arguments:
disabled: bool = False,
label_visibility: LabelVisibility = "visible",
step: Union[int, timedelta] = timedelta(minutes=15),
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd extract 15 to the constant, for example DEFAULT_STEP_MINUTES=15

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, it was repeated in two places, so I've extracted it to constants 👍

def test_st_time_input_with_step(self):
"""Test st.time_input with step."""
value = datetime.time(9, 00)
st.time_input("Set an alarm for", value, step=datetime.timedelta(minutes=5))
Copy link
Contributor

Choose a reason for hiding this comment

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

What will happen if I do step=True or step="foo" or step=(90,) ?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see any part of code that would actually check the type in runtime.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In most of our components we don't do such checking and just rely on Type Hints and runtime exceptions, in my opinion it'd be a bit of over engineering, however I can add some type checking if you insist. But in such case I guess we need some approval for further messages send to user from product, since this is outside of the spec.

I'm leaving this as is for now and waiting for a response from you

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say that all I'm asking boils down to changing your code from:

        if isinstance(step, timedelta):
            step = step.seconds
        if step < 60 or step > timedelta(hours=23).seconds:
            raise StreamlitAPIException(
                f"`step` must be between 60 seconds and 23 hours but is currently set to {step} seconds."
            )

to:

        if isinstance(step, timedelta):
            step = step.seconds
        elif not isinstance(step, int):
            raise StreamlitAPIException(f"`step` can only be `int` or `timedelta` but {type(step)} is provided")
        if step < 60 or step > timedelta(hours=23).seconds:
            raise StreamlitAPIException(
                f"`step` must be between 60 seconds and 23 hours but is currently set to {step} seconds."
            )

So we are talking about adding two lines of code :) IMO pretty cheap stuff to check, even if not done elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's cheap and as I wrote I'm not going to insist on that, thank you for the example and the message, I've updated it with a small change

        if not isinstance(step, (int, timedelta)):
            raise StreamlitAPIException(
                f"`step` can only be `int` or `timedelta` but {type(step)} is provided."
            )

this is called before any step conversion

Copy link
Contributor

@sfc-gh-mnowotka sfc-gh-mnowotka left a comment

Choose a reason for hiding this comment

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

LGTM

})

it("has step parameter which defaults to 900 seconds (15 minutes)", () => {
const props = getProps()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we get these tests to use the new react testing library?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thank you for noticing this 🙇 , I've just updated the tests to use RTL instead of Enzyme, can you please re-review it? 👍

@sfc-gh-tszerszen sfc-gh-tszerszen force-pushed the add-step-parameter-to-st-time-input branch 5 times, most recently from 6440deb to ebb3158 Compare February 14, 2023 14:47
props.element,
"12:08",
{ fromUi: true }
"12:45",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this test is specifically to test for ui changes. I am thinking that you can maybe call fireEvent here (https://testing-library.com/docs/dom-testing-library/api-events/) to test this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for this suggestion, I'm a bit stuck, because I'm firing change event on baseui time input and seems like its not triggering setStringValue method, however when testing manually the timepicker works good 🤔

props.element,
"12:08",
{ fromUi: true }
"12:45",
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto

Copy link
Contributor

@willhuang1997 willhuang1997 left a comment

Choose a reason for hiding this comment

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

2 of the tests are not properly testing what I would expect. Need them to call onChange in order to actually test the functionality.

@willhuang1997
Copy link
Contributor

Everything else LGTM though

@sfc-gh-tszerszen sfc-gh-tszerszen force-pushed the add-step-parameter-to-st-time-input branch from 82b7979 to 5a3ba73 Compare February 17, 2023 15:01
@sfc-gh-tszerszen
Copy link
Contributor Author

@willhuang1997 I have reverted these 2 tests back to Enzyme, so this PR can get merge after QA, can you please accept it? And we will fix these 2 cases in separate PR

@mayagbarnes
Copy link
Collaborator

Hey @sfc-gh-tszerszen 👋🏼
I took a pass at converting those couple of outstanding tests and think I've come up with something reasonable (commit link) - hope it helps!
Also, this is on the feature/time-input-tests branch of this repo just in case you want to access/cherry-pick.

Copy link
Contributor

@sfc-gh-wihuang sfc-gh-wihuang left a comment

Choose a reason for hiding this comment

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

Giving approval but would be great to look if we could look into a better way of selecting certain divs.

Copy link
Contributor

@willhuang1997 willhuang1997 left a comment

Choose a reason for hiding this comment

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

Giving approval but would be great to look if we could look into a better way of selecting certain divs.

@mayagbarnes
Copy link
Collaborator

mayagbarnes commented Feb 22, 2023

Took another pass at it after adding className to the proper div through BaseWeb - think RTL translation is much cleaner now 👍🏼
(See updated feature/time-input-tests branch of this repo)

@sfc-gh-tszerszen sfc-gh-tszerszen force-pushed the add-step-parameter-to-st-time-input branch from 1f66c0a to c46e1b3 Compare February 23, 2023 13:38
@sfc-gh-tszerszen
Copy link
Contributor Author

Thank you @mayagbarnes I allowed myself to re-use your work so in this PR TimeInput.test.tsx is completely rewritten to RTL 👍

@sfc-gh-tszerszen sfc-gh-tszerszen force-pushed the add-step-parameter-to-st-time-input branch from c46e1b3 to c0d1ee4 Compare February 23, 2023 22:13
@sfc-gh-tszerszen sfc-gh-tszerszen force-pushed the add-step-parameter-to-st-time-input branch from c0d1ee4 to 88363e9 Compare March 7, 2023 15:48
@sfc-gh-mnowotka sfc-gh-mnowotka merged commit 2313f2a into develop Mar 9, 2023
tconkling added a commit to tconkling/streamlit that referenced this pull request Mar 9, 2023
* develop:
  Allow control of st.pyplot() width (streamlit#6067)
  `withMapboxToken`: add types (streamlit#6275)
  Add step parameter to st.time_input (streamlit#6071)
  Enable no-else-return rule in ESLint (streamlit#6205)
  Fix `make tstypecheck` (streamlit#6273)
tconkling added a commit to tconkling/streamlit that referenced this pull request Mar 9, 2023
# By Kamil Breguła (2) and others
# Via GitHub
* develop:
  Allow control of st.pyplot() width (streamlit#6067)
  `withMapboxToken`: add types (streamlit#6275)
  Add step parameter to st.time_input (streamlit#6071)
  Enable no-else-return rule in ESLint (streamlit#6205)
  Fix `make tstypecheck` (streamlit#6273)

# Conflicts:
#	frontend/src/hocs/withMapboxToken/withMapboxToken.test.tsx
#	frontend/src/hocs/withMapboxToken/withMapboxToken.tsx
@sfc-gh-kmcgrady sfc-gh-kmcgrady deleted the add-step-parameter-to-st-time-input branch October 5, 2023 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants