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

Make st.help more resilient with conditional members #8228

Merged
merged 4 commits into from Mar 1, 2024

Conversation

kmcgrady
Copy link
Collaborator

@kmcgrady kmcgrady commented Mar 1, 2024

Describe your changes

st.help leverages dir(object) to determine the members in the object to expose to the docstring. If an object makes a member conditional based on internal state, st.help can throw an AttributeError. Ideally, in these cases, the developer of the object would implement the __dir__ method to handle conditional members, but Streamlit can be more resilient in handling this situation.

The example that brought this up was in sklearn, and I created scikit-learn/scikit-learn#28558 with them to better handle this scenario.

Testing Plan

  • Unit Tests should cover the change with the member available and unavailable

Contribution License Agreement

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

else:
member_docs = None
member_value = human_readable_value
except Exception:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I set any exception vs just AttributeError to be extra cautious

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm worried that doing something like this might swallow legitimate errors rather than just the ones that are normal/expected

Would probably prefer to only exclude the expected AttributeError case here


def __getattribute__(self, name):
if name == "say_hello" and not self.available:
raise AttributeError(f"{name} is not accessible when x is even")
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: the error message here seems unrelated to how we actually decide whether to throw an error in this method

el = self.get_delta_from_queue().new_element.doc_string
self.assertEqual("ConditionalHello", el.type)
member_names = [member.name for member in el.members]
self.assertTrue("say_hello" in member_names)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit (here and below): I think there's an assertIn method that makes this more explicit

return object.__getattribute__(self, name)

def say_hello(self):
print("Hello")
Copy link
Collaborator

Choose a reason for hiding this comment

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

super nit: doesn't really matter since this method is never called, but it might be slightly preferable to have the body of this method be a no-op to not have a print statement in the code

@kmcgrady kmcgrady merged commit 33680ba into develop Mar 1, 2024
36 checks passed
@kmcgrady kmcgrady deleted the fix/help-unavailable-attrs branch March 1, 2024 23:49
zyxue pushed a commit to zyxue/streamlit that referenced this pull request Apr 16, 2024
## Describe your changes

`st.help` leverages `dir(object)` to determine the members in the object
to expose to the docstring. If an object makes a member conditional
based on internal state, `st.help` can throw an `AttributeError`.
Ideally, in these cases, the developer of the object would implement the
`__dir__` method to handle conditional members, but Streamlit can be
more resilient in handling this situation.

The example that brought this up was in sklearn, and I created
scikit-learn/scikit-learn#28558 with them to
better handle this scenario.

## Testing Plan

- Unit Tests should cover the change with the member available and
unavailable

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants