xlab-steampunk / redfish-client-python

Minimalistic Redfish API client
Apache License 2.0
4 stars 8 forks source link

Add a custom __dir__ implementation to Resource to support auto-completion #33

Closed nickovs closed 2 years ago

nickovs commented 2 years ago

Interactive Python environments such as IPython and modern versions of regular (C) Python support auto-completion for object attributes by inspecting the list returned by dir(). Under the hood the built-in function attempts to call __dir__() to see if the object has a custom list of attributes. This PR adds an implementation of a custom __dir__() method for Resource objects that returns the known attributes of the resource as well as the regular methods for resources, allowing for auto-completion as the resource hierarchy is browsed interactively. This is even more effective if the client connects with lazy_load=False, but will work one layer deep without this option.

The implementation here hides both internal methods (i.e. those beginning with _ characters) and also Redfish internal attributes (those beginning with @) in the result set, although these are still accessible directly.

Tests for the new method have been added to tests/test_resource.py.

Note: due to a known bug in the jedi library, modern versions of IPython have problems performing auto-completion on objects that offer custom implementations of __dir__. This bug can be avoided by executing %config IPCompleter.use_jedi = False to disable use of the buggy library.

matejart commented 2 years ago

Thank you again for your contribution, @nickovs. We have now pushed the new version 0.3.7 to pypi. Apologies that it took this long.