diff options
author | zygocephalus <grrrr@protonmail.com> | 2019-06-07 23:08:36 +0300 |
---|---|---|
committer | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2019-06-07 13:08:36 -0700 |
commit | 03d5831a2d62c68654ec223168e574cd546efbf6 (patch) | |
tree | e1d26a353a8269cfaf0fd2f1518bf7907c16f116 /Lib/test/test_argparse.py | |
parent | 1f9531764cc0f8dbca1d8f429d162dc28282f4b4 (diff) | |
download | cpython-git-03d5831a2d62c68654ec223168e574cd546efbf6.tar.gz |
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
Diffstat (limited to 'Lib/test/test_argparse.py')
-rw-r--r-- | Lib/test/test_argparse.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py index 9d68f40571..bcf2cc9b26 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""" |