Repository navigation
Fix get_defining_class() raising AttributeError for a module-level lambda - #1861
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
There was a problem hiding this comment.
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()), whereinspect.getmodule()returnsNoneandgetattr(None, ...)raised; - an unbound method of a nested class, e.g.
Outer.Inner.meth, whose qualname prefixOuter.Innerisn'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_providerorcompleteris a module-level lambda.
tleonhardt
left a comment
There was a problem hiding this comment.
@mokshupadhyay
LGTM. Can either merge as-is and I'll add a CHANGELOG.md entry or you can add one first.
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, sogetattrraisedAttributeError. A module-level lambda used as achoices_providerorcompletertherefore broke tab completion: the completion returned no items and reported the error.Real results of
get_defining_classonmainand with this change (a module-levelf = lambda x: xand a module-leveldef g(x)):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 returnsNonefor a function that is not defined on a class.Test:
test_get_defining_class_module_level_lambdaintests/test_utils_defining_class.py; it fails onmainand passes now. The full test suite passes (3229 passed, 5 skipped), andruff checkandruff format --checkare clean on the two changed files. I did not runmake check(its type checkers) ormake docs-test. I did not edit any Markdown file (AGENTS.md asks to ask first) and left the changelog for the maintainer.