if not callable(type_func):
raise ValueError('%r is not callable' % (type_func,))
+ if type_func is FileType:
+ raise ValueError('%r is a FileType class object, instance of it'
+ ' must be passed' % (type_func,))
+
# raise an error if the metavar does not match the type
if hasattr(self, "_get_formatter"):
try:
m.assert_called_with('foo', *args)
+class TestFileTypeMissingInitialization(TestCase):
+ """
+ Test that add_argument throws an error if FileType class
+ object was passed instead of instance of FileType
+ """
+
+ def test(self):
+ parser = argparse.ArgumentParser()
+ with self.assertRaises(ValueError) as cm:
+ parser.add_argument('-x', type=argparse.FileType)
+
+ self.assertEqual(
+ '%r is a FileType class object, instance of it must be passed'
+ % (argparse.FileType,),
+ str(cm.exception)
+ )
+
+
class TestTypeCallable(ParserTestCase):
"""Test some callables as option/argument types"""