Skip to content

Handle exceptions from property getters during member enumeration#673

Open
BhargavKumarNath wants to merge 1 commit into
google:masterfrom
BhargavKumarNath:fix/property-getter-exception
Open

Handle exceptions from property getters during member enumeration#673
BhargavKumarNath wants to merge 1 commit into
google:masterfrom
BhargavKumarNath:fix/property-getter-exception

Conversation

@BhargavKumarNath

Copy link
Copy Markdown

Problem

When a component has a @property whose getter raises any exception
other than AttributeError, calling fire.Fire(component) crashes on
both bare invocation and --help. The raw traceback is shown instead
of usage information.

Reproducer (fire 0.7.1, Python 3.13):

import fire

class App:
    @property
    def status(self):
        raise RuntimeError("backend unavailable")
    def greet(self, who="world"):
        return f"hi {who}"

fire.Fire(App())

$ python app.py --help
RuntimeError: backend unavailable
$ python app.py # bare invocation
RuntimeError: backend unavailable
$ python app.py greet # works fine
hi world

Root Cause

inspect.getmembers in Python 3.13 tightened its internal exception
handling to only suppress AttributeError. Previously it suppressed
all exceptions from attribute getters. Fire calls inspect.getmembers
unguarded in two places during member enumeration for help rendering
and help shortcut detection.

Fix

Add GetSafeMembers to inspectutils, which mirrors the behaviour of
inspect.getmembers but catches all exceptions from attribute getters,
falling back to None for any member that raises. Replace the two
unguarded call sites in completion.VisibleMembers and
core._IsHelpShortcut with GetSafeMembers.

Members whose getters raise are preserved in the output as
(name, None), so the member name remains visible in help output
without crashing.

Testing

Three unit tests added to inspectutils_test.py:

  • raising property: member present with value None
  • working property: member present with correct value
  • mixed properties: both cases handled correctly in one component

Full test suite: 262 passed, 2 pre-existing failures in
main_test.py unrelated to this change (Windows/Python 3.13
environment issues with regex escaping and temp file permissions).

Fixes #672

inspect.getmembers in Python 3.13 only suppresses AttributeError when
calling attribute getters. Any other exception (RuntimeError, ValueError,
etc.) propagates uncaught, causing fire.Fire to crash on --help and bare
invocation when a component has a property whose getter raises.

Add GetSafeMembers to inspectutils, which wraps getattr in a broad
exception handler and falls back to None for any attribute that raises.
Replace the two unguarded inspect.getmembers call sites in core.py
(_IsHelpShortcut) and completion.py (VisibleMembers) with GetSafeMembers.

Fixes google#672
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Uncaught exception when a component has a property whose getter raises (intended?)

1 participant