From 03d5831a2d62c68654ec223168e574cd546efbf6 Mon Sep 17 00:00:00 2001 From: zygocephalus Date: Fri, 7 Jun 2019 23:08:36 +0300 Subject: bpo-37150: Throw ValueError if FileType class object was passed in add_argument (GH-13805) There is a possibility that someone (like me) accidentally will omit parentheses with `FileType` arguments after `FileType`, and parser will contain wrong file until someone will try to use it. Example: ```python parser = argparse.ArgumentParser() parser.add_argument('-x', type=argparse.FileType) ``` https://bugs.python.org/issue37150 --- Lib/argparse.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'Lib/argparse.py') diff --git a/Lib/argparse.py b/Lib/argparse.py index ef888f063b..9a67b41ae0 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1361,6 +1361,10 @@ class _ActionsContainer(object): 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: -- cgit v1.2.1