Skip to content

Resolve the defining class of unbound nested methods - #1862

Closed
tleonhardt wants to merge 9 commits into
mainfrom
fix/nested-class-method-owner
Closed

tleonhardt wants to merge 9 commits into
mainfrom
fix/nested-class-method-owner

Conversation

@tleonhardt

@tleonhardt tleonhardt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Follow-up to the nested-class review comment on #1861.

An unbound completer such as ColorPlugin.Commands.complete_color needs the registered ColorPlugin.Commands instance as self. Looking up ColorPlugin.Commands as a single module attribute returned None, so cmd2 passed the application and completion returned no matches.

get_defining_class() now walks the qualified name from the module, one attribute at a time. A missing owner still falls through to the existing descriptor fallback.

Factory-created callbacks can share a qualified name. For these callbacks, cmd2 checks which classes in the factory’s hierarchy actually installed the function or partial. A standalone callback receives the application.

When a command set’s factory registers an inline completion callback, that callback receives the set. This holds for prebuilt and lazily built parsers, including registration through add_argument(), action setters, and parser helpers. Inline namespace providers registered with with_argparser or with_annotated also receive the set during command execution.

When an application and command sets share a callback, completion uses the command’s own receiver if it matches. Attached subcommands use their registered owner, including when a different command set owns the parent command. For an unrelated command, cmd2 considers the application and registered command sets together. It prefers a unique instance of an installing class, then a unique instance of a subclass. An ambiguous match follows the existing completion error path. Receiver matching uses class identity along the method resolution order. This supports concrete methods on protocols without requiring @runtime_checkable. The hierarchy walk also handles unhashable metaclasses.

When a parser is copied, cmd2 uses the original partial to look up its owner and invokes the copy with its own bound arguments. Two partials around the same callback can therefore read choices from different receivers. The original identity is used only within the process, so actions with picklable partials still round-trip through pickle. Applications can register custom parameters named completer_source and choices_provider_source.

Regression tests exercise nested and inherited methods, subclass-installed providers, standalone and installed partials, shared callbacks on app commands and attached subcommands, sibling command sets, and an unhashable class in a diamond hierarchy. Regression tests also check the choices returned through app.complete() and the namespace values observed by a running command. Additional tests cover shared callbacks with one or two eligible live owners, concrete protocol methods, custom parameters, and pickling a partial provider.

Verification

  • make check and make docs-test passed.
  • make test with branch coverage passed with 3,265 tests passing and 3 skipped.
  • Compared with main, tests covered all 92 changed executable production lines and all 38 measured branch outcomes (100%).

@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 (041e2a8) to head (36c8ec6).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1862   +/-   ##
=======================================
  Coverage   99.76%   99.76%           
=======================================
  Files          23       23           
  Lines        6777     6840   +63     
=======================================
+ Hits         6761     6824   +63     
  Misses         16       16           
Flag Coverage Δ
unittests 99.76% <100.00%> (+<0.01%) ⬆️

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.

Skip qualified-name traversal for local functions so choices providers returned by CommandSet methods receive the application. Cover the nested factory case with a regression test.
Check whether the enclosing class stores the exact function returned by a factory. This gives class-assigned choices providers the registered command set while standalone callbacks from the same factory receive the application.
Search the factory class hierarchy for the callback and match the underlying function when a parser copies an installed partial. Cover both receiver regressions through tab completion.
Keep the original partial identity for receiver lookup when argparse copies a parser. Resolve shared providers using the command receiver and registered instances, and visit factory subclasses by identity so unhashable metaclasses work.
…commands

Use the active command receiver before looking for a registered fallback. Keep partial callback identity across parser copies while preserving action pickling and custom completion parameters.
Track the declaration scope of argparse callbacks to recognize inline parser-factory providers. Match live owners by nominal class identity so application fallbacks and concrete protocol providers work.
Recognize local callbacks when they are registered through action setters, parser helpers, or command decorators. Exercise both tab completion and command execution with distinct application and command set state.
@tleonhardt

Copy link
Copy Markdown
Member Author

I'm going to abandon this. Initially I thought this was going to be a simple fix for an obscure edge case. But the code complexity for supporting this obscure edge case was more than I anticipated.

@tleonhardt tleonhardt closed this Dec 3, 2026
@tleonhardt
tleonhardt deleted the fix/nested-class-method-owner branch December 3, 2026 23:13
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.

1 participant