Skip to content

Fix get_defining_class() raising AttributeError for a module-level lambda - #1861

Merged
tleonhardt merged 1 commit into
python-cmd2:mainfrom
mokshupadhyay:fix/get-defining-class-lambda
Dec 3, 2026
Merged

tleonhardt merged 1 commit into
python-cmd2:mainfrom
mokshupadhyay:fix/get-defining-class-lambda

Conversation

@mokshupadhyay

Copy link
Copy Markdown
Contributor

cmd2.utils.get_defining_class() finds the class that defines a method by looking up the name from the function's __qualname__ in its module. For a lambda defined at module level the __qualname__ is <lambda>, which is not an attribute of the module, so getattr raised AttributeError. A module-level lambda used as a choices_provider or completer therefore broke tab completion: the completion returned no items and reported the error.

Real results of get_defining_class on main and with this change (a module-level f = lambda x: x and a module-level def g(x)):

f (module-level lambda)   main: AttributeError: module '__main__' has no attribute '<lambda>'   after: None
g (module-level function) main: None                                                              after: None   (unchanged)

The fix passes a default to the getattr (None), and only returns the owner when it is a class; every other case continues down to the existing fallback, which returns None for a function that is not defined on a class.

Test: test_get_defining_class_module_level_lambda in tests/test_utils_defining_class.py; it fails on main and passes now. The full test suite passes (3229 passed, 5 skipped), and ruff check and ruff format --check are clean on the two changed files. I did not run make check (its type checkers) or make docs-test. I did not edit any Markdown file (AGENTS.md asks to ask first) and left the changelog for the maintainer.

…mbda

A lambda defined at module level has a __qualname__ of "<lambda>", which is not an
attribute of its module, so getattr() raised AttributeError. When such a lambda was
passed as choices_provider or completer, completion failed with that error. Return
None, as for any other function without a defining class.
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.76%. Comparing base (e3e623a) to head (5478d43).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1861   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files          23       23           
  Lines        6777     6777           
=======================================
  Hits         6761     6761           
  Misses         16       16           
Flag Coverage Δ
unittests 99.76% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@tleonhardt tleonhardt left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for this, it's a clean fix. Giving the getattr a default is the right shape: every case that isn't a class now falls through to the existing __objclass__ fallback, and the test covers both the plain lambda and a partial of it, which is how choices_provider values often arrive.

The default also fixes two other crashes the same line had, which might be worth mentioning in the changelog entry:

  • a function whose module can't be found (e.g. one created with exec()), where inspect.getmodule() returns None and getattr(None, ...) raised;
  • an unbound method of a nested class, e.g. Outer.Inner.meth, whose qualname prefix Outer.Inner isn't a module attribute.

One suggestion about that second case (inline), and a CHANGELOG request: please add an entry under ## 4.5.0 (TBD) / Bug Fixes. Something like:

Tab completion no longer fails when a choices_provider or completer is a module-level lambda.

Comment thread cmd2/utils.py Resolved

@tleonhardt tleonhardt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@mokshupadhyay
LGTM. Can either merge as-is and I'll add a CHANGELOG.md entry or you can add one first.

@tleonhardt
tleonhardt merged commit 5a00ea2 into python-cmd2:main Dec 3, 2026
28 checks passed
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.

2 participants