Skip to content

Commit

Permalink
Use "dir()" to get available methods of object (#215)
Browse files Browse the repository at this point in the history
* Use dir() instead of inspect.getmembers() to get available methods of the object
* Add test case for #149

Ref: #149
  • Loading branch information
meshde authored Feb 21, 2020
1 parent 192c020 commit 1ac5105
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def _GetMember(component, args):
Raises:
FireError: If we cannot consume an argument to get a member.
"""
members = dict(inspect.getmembers(component))
members = dir(component)
arg = args[0]
arg_names = [
arg,
Expand All @@ -641,7 +641,7 @@ def _GetMember(component, args):

for arg_name in arg_names:
if arg_name in members:
return members[arg_name], [arg], args[1:]
return getattr(component, arg_name), [arg], args[1:]

raise FireError('Could not consume arg:', arg)

Expand Down
5 changes: 5 additions & 0 deletions fire/fire_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,11 @@ def testTraceErrors(self):
with self.assertRaisesFireExit(2):
fire.Fire(tc.InstanceVars, command=['--arg1=a1', '--arg2=a2', '-', 'jog'])

def testClassWithDefaultMethod(self):
self.assertEqual(
fire.Fire(tc.DefaultMethod, command=['double', '10']), 20
)


if __name__ == '__main__':
testutils.main()
10 changes: 10 additions & 0 deletions fire/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,13 @@ def off(self):
def set(self, value):
self.pixels[self._row][self._col] = value
return self

class DefaultMethod(object):

def double(self, number):
return 2 * number

def __getattr__(self, name):
def _missing():
return "Undefined Function"
return _missing

0 comments on commit 1ac5105

Please sign in to comment.