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
Inaccurate Attribute Listing with dir(obj) for Classes Using available_if Conditional Method Decorator #28558
Comments
A simple solution should be straightforward where we override the def __dir__(self):
original_dir = super().__dir__()
return [attr for attr in original_dir if hasattr(self, attr)] We may want to find ways to be smarter if anyone just uses |
## 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.
im working on this. |
I think it would be useful to fix this. Though I'd not be surprised if it got hairy real quick, doing magic like Some cases that we should consider and make sure we don't make any of them worse (ideally we'd fix all of them but maybe we can't):
The proposed edit: though I am not sure if it is really a bug for |
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
## 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.
Inaccurate Attribute Listing with dir(obj) in classes with conditional methods, I overwrote the python method _dir_ in all the classes that contain conditional methods
Describe the bug
When utilizing the
available_if
decorator from SciKit Learn to conditionally expose methods based on specific object state or conditions, we observe that thedir(obj)
function may return inaccurate results. Specifically,dir(obj)
continues to list methods that should be conditionally hidden based on theavailable_if
decorator's logic. This discrepancy arises because the__dir__
method on the affected classes does not dynamically account for this conditional availability. As a result, users and consuming code may be misled about the actual methods available for use on instances of the class at runtime, potentially leading to unexpectedAttributeErrors
when accessing supposedly available methods.Steps/Code to Reproduce
I will test with the SVC, but it can apply to other classes.
Expected Results
It should print out the following:
Methods decorated with
available_if
and whose conditions raise anAttributeError
should not appear in the list returned bydir(obj)
.Actual Results
It should print out the following:
Methods decorated with
available_if
and whose conditions raise anAttributeError
appears in the list returned bydir(obj)
.Traceback when you show the error itself (comes from the check in
available_if
.Versions
The text was updated successfully, but these errors were encountered: