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