Skip to content

Omit self from instance method completions - #714

Open
suryakant-source wants to merge 2 commits into
google:masterfrom
suryakant-source:suryakant-source-patch-1
Open

suryakant-source wants to merge 2 commits into
google:masterfrom
suryakant-source:suryakant-source-patch-1

Conversation

@suryakant-source

Copy link
Copy Markdown

Fixes #382

Problem

For a class such as

class Foo:
    def do_stuff(self, arg1): ...

python example.py -- --completion offers --self as a flag (opts="--arg1 --self ..."). Accessing an instance method on the class gives a plain function, and its first argument (self) was treated like any other flag.

Change

_Commands now drops the first argument's flag for plain instance methods found on a class. Static methods and class methods are unaffected (checked with inspect.getattr_static). If a method has no positional args, nothing is dropped.

Testing

  • New test testInstanceMethodCompletionsOmitSelf covers instance, static and class methods. It fails without the change (('bar', '--self') is generated) and passes with it.
  • Full suite on Python 3.10: 262 passed.

I used an AI coding assistant while preparing this change, and I reviewed the diff and test results myself.

@google-cla

google-cla Bot commented Oct 15, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request updates the command completion logic in fire/completion.py to omit the implicit self argument of instance methods when generating command completions, and adds corresponding unit tests in fire/completion_test.py. Feedback suggests catching a broader Exception instead of just AttributeError in _IsUnboundMethod to improve robustness against mock objects or custom descriptors.

Comment thread fire/completion.py
Comment on lines +449 to +452
try:
attr = inspect.getattr_static(owner, name)
except AttributeError:
return False

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To make _IsUnboundMethod more robust against unexpected exceptions (e.g., when dealing with mock objects, custom descriptors, or environments with restricted/modified inspect modules), consider catching Exception instead of just AttributeError.

Suggested change
try:
attr = inspect.getattr_static(owner, name)
except AttributeError:
return False
try:
attr = inspect.getattr_static(owner, name)
except Exception:
return False

@suryakant-source

Copy link
Copy Markdown
Author

@googlebot I signed it!

1 similar comment
@suryakant-source

Copy link
Copy Markdown
Author

@googlebot I signed it!

This branch has not been deployed

No deployments
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.

BUG: When using a Class for grouping commands --self shows as a flag

1 participant