Skip to content

Commit 88f3923

Browse files
dbiebercopybara-github
authored andcommitted
enable test for callable object usage
PiperOrigin-RevId: 260045817 Change-Id: I700cef5d0676fae7c79221beafcf07373f268a46
1 parent 05080ec commit 88f3923

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

fire/helptext.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,10 @@ def UsageText(component, trace=None, verbose=False):
498498

499499
if callable(component):
500500
callable_items = _GetCallableUsageItems(spec, metadata)
501-
continuations.append(' '.join(callable_items))
501+
if callable_items:
502+
continuations.append(' '.join(callable_items))
503+
elif trace:
504+
continuations.append(trace.separator)
502505
availability_lines.extend(_GetCallableAvailabilityLines(spec))
503506

504507
if continuations:

fire/helptext_test.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -355,18 +355,16 @@ def testUsageOutputFunctionWithDocstring(self):
355355
textwrap.dedent(expected_output).lstrip('\n'),
356356
usage_output)
357357

358-
@testutils.skip('The functionality is not implemented yet')
359358
def testUsageOutputCallable(self):
360-
# This is both a group and a command!
361-
component = tc.CallableWithKeywordArgument
362-
t = trace.FireTrace(component, name='CallableWithKeywordArgument')
363-
usage_output = helptext.UsageText(component, trace=t, verbose=True)
364-
# TODO(joejoevictor): We need to handle the case for keyword args as well
365-
# i.e. __call__ method of CallableWithKeywordArgument
359+
# This is both a group and a command.
360+
component = tc.CallableWithKeywordArgument()
361+
t = trace.FireTrace(component, name='CallableWithKeywordArgument',
362+
separator='@')
363+
usage_output = helptext.UsageText(component, trace=t, verbose=False)
366364
expected_output = '''
367-
Usage: CallableWithKeywordArgument <command>
368-
369-
Available commands: print_msg
365+
Usage: CallableWithKeywordArgument <command> | <flags>
366+
available commands: print_msg
367+
flags are accepted
370368
371369
For detailed information on this command, run:
372370
CallableWithKeywordArgument -- --help'''

fire/inspectutils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ class with an __init__ method.
8686
return fn, skip_arg
8787

8888

89+
def Py2GetArgSpec(fn):
90+
"""A wrapper around getargspec that tries both fn and fn.__call__."""
91+
try:
92+
return inspect.getargspec(fn) # pylint: disable=deprecated-method
93+
except TypeError:
94+
if hasattr(fn, '__call__'):
95+
return inspect.getargspec(fn.__call__) # pylint: disable=deprecated-method
96+
raise
97+
98+
8999
def GetFullArgSpec(fn):
90100
"""Returns a FullArgSpec describing the given callable."""
91101

@@ -94,7 +104,7 @@ def GetFullArgSpec(fn):
94104

95105
try:
96106
if six.PY2:
97-
args, varargs, varkw, defaults = inspect.getargspec(fn) # pylint: disable=deprecated-method
107+
args, varargs, varkw, defaults = Py2GetArgSpec(fn)
98108
kwonlyargs = kwonlydefaults = None
99109
annotations = getattr(fn, '__annotations__', None)
100110
else:

fire/test_components.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ def print_msg(self, msg):
336336
print(msg)
337337

338338

339+
callable_with_keyword_argument = CallableWithKeywordArgument()
340+
341+
339342
class ClassWithDocstring(object):
340343
"""Test class for testing help text output.
341344

0 commit comments

Comments
 (0)