Visitar URL original
Skip members that raise when listing them for help by rootkiller6788 · Pull Request #699 · google/python-fire · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Skip members that raise when listing them for help
Enumerating a component's members for help or completion runs each
member's getter, including property getters. A getter that raises
anything other than AttributeError currently makes inspect.getmembers
abort, so running a CLI with no args or with --help crashes with the
property's raw traceback instead of showing usage.

Add inspectutils.GetMembers, which keeps the normal getmembers path but
falls back to reading members one at a time and skipping the ones that
raise, and use it where members are enumerated for help.
  • Loading branch information
rootkiller6788 committed Sep 3, 2026
commit ea233b87353262c3639436b7a9c9442091206628
2 changes: 1 addition & 1 deletion fire/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def VisibleMembers(component, class_attrs=None, verbose=False):
if isinstance(component, dict):
members = component.items()
else:
members = inspect.getmembers(component)
members = inspectutils.GetMembers(component)

# If class_attrs has not been provided, compute it.
if class_attrs is None:
Expand Down
2 changes: 1 addition & 1 deletion fire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def _IsHelpShortcut(component_trace, remaining_args):
_, remaining_kwargs, _ = _ParseKeywordArgs(remaining_args, fn_spec)
show_help = target in remaining_kwargs
else:
members = dict(inspect.getmembers(component))
members = dict(inspectutils.GetMembers(component))
show_help = target not in members

if show_help:
Expand Down
27 changes: 27 additions & 0 deletions fire/inspectutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,33 @@ def GetClassAttrsDict(component):
}


def GetMembers(component):
"""Returns a list of (name, member) pairs for the members of component.

Reading a member can run arbitrary code, such as a property getter, which may
raise an exception. inspect.getmembers aborts on any exception that is not an
AttributeError, which would crash Fire while it only intends to enumerate the
members of the component, for example to build help text. This falls back to
reading the members one at a time and skipping any that cannot be read, so a
single broken member does not prevent the rest from being listed.

Args:
component: The object whose members to list.
Returns:
A list of (name, value) pairs for the readable members of component.
"""
try:
return inspect.getmembers(component)
except Exception: # pylint: disable=broad-except
members = []
for name in dir(component):
try:
members.append((name, getattr(component, name)))
except Exception: # pylint: disable=broad-except
continue
return members


def IsCoroutineFunction(fn):
try:
return inspect.iscoroutinefunction(fn)
Expand Down