|
17 | 17 | from __future__ import absolute_import
|
18 | 18 | from __future__ import division
|
19 | 19 | from __future__ import print_function
|
| 20 | +import sys |
| 21 | +import unittest |
20 | 22 |
|
21 | 23 | from fire import core
|
22 | 24 | from fire import decorators
|
@@ -90,6 +92,18 @@ def example7(self, arg1, arg2=None, *varargs, **kwargs): # pylint: disable=keyw
|
90 | 92 | return arg1, arg2, varargs, kwargs
|
91 | 93 |
|
92 | 94 |
|
| 95 | +if sys.version_info >= (3, 5): |
| 96 | + class WithTypeHints(object): |
| 97 | + |
| 98 | + @decorators.UseTypeHints() |
| 99 | + def example8(self, a: int, b: str, c, d : float = None): |
| 100 | + return a, b, c, d |
| 101 | + |
| 102 | + @decorators.UseTypeHints({list: lambda arg: list(map(int, arg.split(";")))}) |
| 103 | + def example9(self, a: str, b, c: list, d : list = None): |
| 104 | + return a, b, c, d |
| 105 | + |
| 106 | + |
93 | 107 | class FireDecoratorsTest(testutils.BaseTestCase):
|
94 | 108 |
|
95 | 109 | def testSetParseFnsNamedArgs(self):
|
@@ -169,6 +183,24 @@ def testSetParseFn(self):
|
169 | 183 | command=['example7', '1', '--arg2=2', '3', '4', '--kwarg=5']),
|
170 | 184 | ('1', '2', ('3', '4'), {'kwarg': '5'}))
|
171 | 185 |
|
| 186 | + @unittest.skipIf(sys.version_info < (3, 5), |
| 187 | + 'Type hints were introduced in python 3.5') |
| 188 | + def testDefaultTypeHints(self): |
| 189 | + self.assertEqual( |
| 190 | + core.Fire(WithTypeHints, |
| 191 | + command=['example8', '1', '2', '3', '--d=4']), |
| 192 | + (1, '2', 3, 4) |
| 193 | + ) |
| 194 | + |
| 195 | + @unittest.skipIf(sys.version_info < (3, 5), |
| 196 | + 'Type hints were introduced in python 3.5') |
| 197 | + def testCustomTypeHints(self): |
| 198 | + self.assertEqual( |
| 199 | + core.Fire(WithTypeHints, |
| 200 | + command=['example9', '1', '2', '3', '--d=4;5;6']), |
| 201 | + ('1', 2, [3], [4, 5, 6]) |
| 202 | + ) |
| 203 | + |
172 | 204 |
|
173 | 205 | if __name__ == '__main__':
|
174 | 206 | testutils.main()
|
0 commit comments