Skip to content

Commit fa566e3

Browse files
committed
Ensure compatibility with python2.7
1 parent 4c0f84d commit fa566e3

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

fire/decorators_test.py

+25-21
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,6 @@ def example7(self, arg1, arg2=None, *varargs, **kwargs): # pylint: disable=keyw
9292
return arg1, arg2, varargs, kwargs
9393

9494

95-
if sys.version_info >= (3, 5):
96-
from pathlib import Path
97-
98-
99-
class WithTypeHints(object):
100-
101-
@decorators.UseTypeHints()
102-
def example8(self, a: int, b: str, c, d : float = None):
103-
return a, b, c, d
104-
105-
@decorators.UseTypeHints({list: lambda arg: list(map(int, arg.split(";"))),
106-
Path: Path})
107-
def example9(self, a: Path, b, c: list, d : list = None):
108-
return a, b, c, d
109-
110-
11195
class FireDecoratorsTest(testutils.BaseTestCase):
11296

11397
def testSetParseFnsNamedArgs(self):
@@ -190,19 +174,39 @@ def testSetParseFn(self):
190174
@unittest.skipIf(sys.version_info < (3, 5),
191175
'Type hints were introduced in python 3.5')
192176
def testDefaultTypeHints(self):
177+
# need to hide type hints syntax behind exec
178+
# otherwise old python parser will fail
179+
#pylint: disable=exec-used
180+
exec("""@decorators.UseTypeHints()
181+
def exampleWithSimpleTypeHints(a: int, b: str, c, d : float = None):
182+
return a, b, c, d""")
183+
184+
193185
self.assertEqual(
194-
core.Fire(WithTypeHints,
195-
command=['example8', '1', '2', '3', '--d=4']),
186+
core.Fire(locals()['exampleWithSimpleTypeHints'],
187+
command=['1', '2', '3', '--d=4']),
196188
(1, '2', 3, 4)
197189
)
198190

199191
@unittest.skipIf(sys.version_info < (3, 5),
200192
'Type hints were introduced in python 3.5')
201193
def testCustomTypeHints(self):
194+
# need to hide type hints syntax behind exec
195+
# otherwise old python parser will fail
196+
#pylint: disable=exec-used
197+
exec("""from pathlib import Path
198+
199+
200+
@decorators.UseTypeHints({
201+
list: lambda arg: list(map(int, arg.split(";"))),
202+
Path: Path})
203+
def exampleWithComplexHints(a: Path, b, c: list, d : list = None):
204+
return a, b, c, d""")
205+
202206
self.assertEqual(
203-
core.Fire(WithTypeHints,
204-
command=['example9', '1', '2', '3', '--d=4;5;6']),
205-
(Path('1'), 2, [3], [4, 5, 6])
207+
core.Fire(locals()['exampleWithComplexHints'],
208+
command=['1', '2', '3', '--d=4;5;6']),
209+
(locals()['Path']('1'), 2, [3], [4, 5, 6])
206210
)
207211

208212

0 commit comments

Comments
 (0)