Skip to content

Commit 63aba49

Browse files
jgarstdbieber
authored andcommitted
Support annotations in functions. (google#8)
* Fix error with annotated functions. Calling inspect.getargspec on an annotated function throws an error in Python 3. inspect.getfullargspec should be used instead. * Change if/elif to if/else if else allows better static analysis.
1 parent 48dd662 commit 63aba49

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

fire/fire_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ def testFirePartialNamedArgsOneMissing(self):
9898
self.assertEqual(
9999
fire.Fire(tc.MixedDefaults, 'identity --alpha 1'), (1, '0'))
100100

101+
def testFireAnnotatedArgs(self):
102+
self.assertEqual(fire.Fire(tc.Annotations, 'double 5'), 10)
103+
self.assertEqual(fire.Fire(tc.Annotations, 'triple 5'), 15)
104+
101105
def testFireProperties(self):
102106
self.assertEqual(fire.Fire(tc.TypedProperties, 'alpha'), True)
103107
self.assertEqual(fire.Fire(tc.TypedProperties, 'beta'), (1, 2, 3))

fire/inspectutils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,18 @@ def GetArgSpec(fn):
7272
fn, skip_arg = _GetArgSpecFnInfo(fn)
7373

7474
try:
75-
argspec = inspect.getargspec(fn)
75+
76+
if six.PY2:
77+
argspec = inspect.getargspec(fn)
78+
keywords = argspec.keywords
79+
else:
80+
argspec = inspect.getfullargspec(fn)
81+
keywords = argspec.varkw
82+
7683
args = argspec.args
7784
defaults = argspec.defaults or ()
7885
varargs = argspec.varargs
79-
keywords = argspec.keywords
86+
8087
except TypeError:
8188
args = []
8289
defaults = ()

fire/test_components.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ def sum(self, alpha=0, beta=0):
7575
def identity(self, alpha, beta='0'):
7676
return alpha, beta
7777

78+
class Annotations(object):
79+
80+
def double(self, count=0):
81+
return 2 * count
82+
83+
def triple(self, count=0):
84+
return 3 * count
85+
86+
double.__annotations__ = {'count': float}
87+
triple.__annotations__ = {'count': float}
88+
7889

7990
class TypedProperties(object):
8091

0 commit comments

Comments
 (0)