From 6341d1382e0ab14043396e8a9fbb18c27a3e236c Mon Sep 17 00:00:00 2001 From: ymdatta Date: Sun, 28 Oct 2018 17:21:39 +0530 Subject: [PATCH 1/4] Add support for optparse's 'float' and 'complex' types. This helps normalize_from_setuptools, to handle all optparse's standard-option-types. Fixes #452 --- src/flake8/options/manager.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/flake8/options/manager.py b/src/flake8/options/manager.py index 3f4e883..0ded13a 100644 --- a/src/flake8/options/manager.py +++ b/src/flake8/options/manager.py @@ -156,6 +156,10 @@ class Option(object): value = self.normalize(value) if self.type == "int" or self.action == "count": return int(value) + elif self.type == "float": + return float(value) + elif self.type == "complex": + return complex(value) if self.action in ("store_true", "store_false"): value = str(value).upper() if value in ("1", "T", "TRUE", "ON"): From ccd9beb26dac68ada62d3c5fd58c0e4ffa3d79c3 Mon Sep 17 00:00:00 2001 From: ymdatta Date: Sun, 28 Oct 2018 22:16:03 +0530 Subject: [PATCH 2/4] Added tests for optparse's 'float' and 'complex' types --- tests/unit/test_option.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/unit/test_option.py b/tests/unit/test_option.py index 76b4341..7c90512 100644 --- a/tests/unit/test_option.py +++ b/tests/unit/test_option.py @@ -23,6 +23,14 @@ def test_to_optparse(): assert optparse_opt.action == 'count' +def test_to_support_optparses_standard_types(): + """Show that optparse converts float and complex types correctly.""" + opt = manager.Option('-t', '--test') + + assert type(opt.normalize_from_setuptools(float(2))) == float + assert type(opt.normalize_from_setuptools(complex(2))) == complex + + @mock.patch('optparse.Option') def test_to_optparse_creates_an_option_as_we_expect(Option): # noqa: N803 """Show that we pass all keyword args to optparse.Option.""" From 3b161305003448f67e5f9c4847ff196e8d2ac716 Mon Sep 17 00:00:00 2001 From: ymdatta Date: Fri, 9 Nov 2018 08:39:47 +0530 Subject: [PATCH 3/4] test_option:Modify the tests to check support for optparse's types. --- tests/unit/test_option.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/unit/test_option.py b/tests/unit/test_option.py index 7c90512..1cb9dae 100644 --- a/tests/unit/test_option.py +++ b/tests/unit/test_option.py @@ -23,12 +23,14 @@ def test_to_optparse(): assert optparse_opt.action == 'count' -def test_to_support_optparses_standard_types(): +@pytest.mark.parametrize('opttype,str_val,expected', [ + ('float', '2', 2.0), + ('complex', '2', (2+0j)), +]) +def test_to_support_optparses_standard_types(opttype, str_val, expected): """Show that optparse converts float and complex types correctly.""" - opt = manager.Option('-t', '--test') - - assert type(opt.normalize_from_setuptools(float(2))) == float - assert type(opt.normalize_from_setuptools(complex(2))) == complex + opt = manager.Option('-t', '--test', type=opttype) + assert opt.normalize_from_setuptools(str_val) == expected @mock.patch('optparse.Option') From cc20e35058da474bd56eea7cf893a872506926e6 Mon Sep 17 00:00:00 2001 From: ymdatta Date: Fri, 4 Jan 2019 10:32:06 +0530 Subject: [PATCH 4/4] Fixed linting errors --- tests/unit/test_option.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_option.py b/tests/unit/test_option.py index 1cb9dae..eb4f95d 100644 --- a/tests/unit/test_option.py +++ b/tests/unit/test_option.py @@ -25,7 +25,7 @@ def test_to_optparse(): @pytest.mark.parametrize('opttype,str_val,expected', [ ('float', '2', 2.0), - ('complex', '2', (2+0j)), + ('complex', '2', (2 + 0j)), ]) def test_to_support_optparses_standard_types(opttype, str_val, expected): """Show that optparse converts float and complex types correctly."""