diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-06-07 21:11:59 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-07 21:11:59 (GMT) |
commit | 606ac581e2451c420117c55632f0fe13d4cec2cd (patch) | |
tree | cb208cda0774022e3c142b55bb40b37b794df111 | |
parent | 6c9effabe0054c80a38b6d02cc52ec7b581e5f91 (diff) | |
download | cpython-606ac581e2451c420117c55632f0fe13d4cec2cd.zip cpython-606ac581e2451c420117c55632f0fe13d4cec2cd.tar.gz cpython-606ac581e2451c420117c55632f0fe13d4cec2cd.tar.bz2 |
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
(cherry picked from commit 03d5831a2d62c68654ec223168e574cd546efbf6)
Co-authored-by: zygocephalus <grrrr@protonmail.com>
-rw-r--r-- | Lib/argparse.py | 4 | ||||
-rw-r--r-- | Lib/test/test_argparse.py | 18 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2019-06-04-14-44-41.bpo-37150.TTzHxj.rst | 1 |
3 files changed, 23 insertions, 0 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index ef888f0..9a67b41 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: diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 9d68f40..bcf2cc9 100644 --- a/Lib/test/test_argparse.py +++ b/Lib/test/test_argparse.py @@ -1619,6 +1619,24 @@ class TestFileTypeOpenArgs(TestCase): 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""" diff --git a/Misc/NEWS.d/next/Library/2019-06-04-14-44-41.bpo-37150.TTzHxj.rst b/Misc/NEWS.d/next/Library/2019-06-04-14-44-41.bpo-37150.TTzHxj.rst new file mode 100644 index 0000000..c5be46e --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-06-04-14-44-41.bpo-37150.TTzHxj.rst @@ -0,0 +1 @@ +`argparse._ActionsContainer.add_argument` now throws error, if someone accidentally pass FileType class object instead of instance of FileType as `type` argument
\ No newline at end of file |