From 501df0dc4e289b04e73a9c64ca1ff2fda335536b Mon Sep 17 00:00:00 2001 From: Mads Jensen Date: Sat, 19 Sep 2020 11:35:49 +0200 Subject: [PATCH] Use in dict rather than "in dict.keys()". Fix linting warnings about "not in". (#8298) * Fixed a few linting warnings for if not x in y. These should have been caught by pylint, but weren't. * Replaced "x in y.keys()" with "x in y". It's much faster, and more Pythonic. --- acme/acme/client.py | 2 +- acme/tests/client_test.py | 2 +- certbot-apache/certbot_apache/_internal/parser.py | 2 +- certbot-apache/tests/centos_test.py | 10 +++++----- certbot-apache/tests/fedora_test.py | 10 +++++----- certbot-apache/tests/gentoo_test.py | 2 +- .../configurators/apache/common.py | 2 +- certbot/certbot/_internal/storage.py | 2 +- certbot/certbot/plugins/storage.py | 2 +- tests/letstest/multitester.py | 2 +- 10 files changed, 18 insertions(+), 18 deletions(-) diff --git a/acme/acme/client.py b/acme/acme/client.py index 19e444749..f66367c38 100644 --- a/acme/acme/client.py +++ b/acme/acme/client.py @@ -801,7 +801,7 @@ class ClientV2(ClientBase): """ # Can't use response.links directly because it drops multiple links # of the same relation type, which is possible in RFC8555 responses. - if not 'Link' in response.headers: + if 'Link' not in response.headers: return [] links = parse_header_links(response.headers['Link']) return [l['url'] for l in links diff --git a/acme/tests/client_test.py b/acme/tests/client_test.py index d433879e8..c84878c42 100644 --- a/acme/tests/client_test.py +++ b/acme/tests/client_test.py @@ -1342,7 +1342,7 @@ class ClientNetworkSourceAddressBindingTest(unittest.TestCase): # test should fail if the default adapter type is changed by requests net = ClientNetwork(key=None, alg=None) session = requests.Session() - for scheme in session.adapters.keys(): + for scheme in session.adapters: client_network_adapter = net.session.adapters.get(scheme) default_adapter = session.adapters.get(scheme) self.assertEqual(client_network_adapter.__class__, default_adapter.__class__) diff --git a/certbot-apache/certbot_apache/_internal/parser.py b/certbot-apache/certbot_apache/_internal/parser.py index c9aebae54..3e842dcdf 100644 --- a/certbot-apache/certbot_apache/_internal/parser.py +++ b/certbot-apache/certbot_apache/_internal/parser.py @@ -799,7 +799,7 @@ class ApacheParser(object): def _parsed_by_parser_paths(self, filep, paths): """Helper function that searches through provided paths and returns True if file path is found in the set""" - for directory in paths.keys(): + for directory in paths: for filename in paths[directory]: if fnmatch.fnmatch(filep, os.path.join(directory, filename)): return True diff --git a/certbot-apache/tests/centos_test.py b/certbot-apache/tests/centos_test.py index 9dc6fa5a7..b7e9c1cb6 100644 --- a/certbot-apache/tests/centos_test.py +++ b/certbot-apache/tests/centos_test.py @@ -140,7 +140,7 @@ class MultipleVhostsTestCentOS(util.ApacheTest): self.assertEqual(mock_get.call_count, 3) self.assertEqual(len(self.config.parser.modules), 4) self.assertEqual(len(self.config.parser.variables), 2) - self.assertTrue("TEST2" in self.config.parser.variables.keys()) + self.assertTrue("TEST2" in self.config.parser.variables) self.assertTrue("mod_another.c" in self.config.parser.modules) def test_get_virtual_hosts(self): @@ -172,11 +172,11 @@ class MultipleVhostsTestCentOS(util.ApacheTest): mock_osi.return_value = ("centos", "7") self.config.parser.update_runtime_variables() - self.assertTrue("mock_define" in self.config.parser.variables.keys()) - self.assertTrue("mock_define_too" in self.config.parser.variables.keys()) - self.assertTrue("mock_value" in self.config.parser.variables.keys()) + self.assertTrue("mock_define" in self.config.parser.variables) + self.assertTrue("mock_define_too" in self.config.parser.variables) + self.assertTrue("mock_value" in self.config.parser.variables) self.assertEqual("TRUE", self.config.parser.variables["mock_value"]) - self.assertTrue("MOCK_NOSEP" in self.config.parser.variables.keys()) + self.assertTrue("MOCK_NOSEP" in self.config.parser.variables) self.assertEqual("NOSEP_VAL", self.config.parser.variables["NOSEP_TWO"]) @mock.patch("certbot_apache._internal.configurator.util.run_script") diff --git a/certbot-apache/tests/fedora_test.py b/certbot-apache/tests/fedora_test.py index e0ee603c3..50831802b 100644 --- a/certbot-apache/tests/fedora_test.py +++ b/certbot-apache/tests/fedora_test.py @@ -134,7 +134,7 @@ class MultipleVhostsTestFedora(util.ApacheTest): self.assertEqual(mock_get.call_count, 3) self.assertEqual(len(self.config.parser.modules), 4) self.assertEqual(len(self.config.parser.variables), 2) - self.assertTrue("TEST2" in self.config.parser.variables.keys()) + self.assertTrue("TEST2" in self.config.parser.variables) self.assertTrue("mod_another.c" in self.config.parser.modules) @mock.patch("certbot_apache._internal.configurator.util.run_script") @@ -172,11 +172,11 @@ class MultipleVhostsTestFedora(util.ApacheTest): mock_osi.return_value = ("fedora", "29") self.config.parser.update_runtime_variables() - self.assertTrue("mock_define" in self.config.parser.variables.keys()) - self.assertTrue("mock_define_too" in self.config.parser.variables.keys()) - self.assertTrue("mock_value" in self.config.parser.variables.keys()) + self.assertTrue("mock_define" in self.config.parser.variables) + self.assertTrue("mock_define_too" in self.config.parser.variables) + self.assertTrue("mock_value" in self.config.parser.variables) self.assertEqual("TRUE", self.config.parser.variables["mock_value"]) - self.assertTrue("MOCK_NOSEP" in self.config.parser.variables.keys()) + self.assertTrue("MOCK_NOSEP" in self.config.parser.variables) self.assertEqual("NOSEP_VAL", self.config.parser.variables["NOSEP_TWO"]) @mock.patch("certbot_apache._internal.configurator.util.run_script") diff --git a/certbot-apache/tests/gentoo_test.py b/certbot-apache/tests/gentoo_test.py index aa923c367..64f7d1062 100644 --- a/certbot-apache/tests/gentoo_test.py +++ b/certbot-apache/tests/gentoo_test.py @@ -91,7 +91,7 @@ class MultipleVhostsTestGentoo(util.ApacheTest): with mock.patch("certbot_apache._internal.override_gentoo.GentooParser.update_modules"): self.config.parser.update_runtime_variables() for define in defines: - self.assertTrue(define in self.config.parser.variables.keys()) + self.assertTrue(define in self.config.parser.variables) @mock.patch("certbot_apache._internal.apache_util.parse_from_subprocess") def test_no_binary_configdump(self, mock_subprocess): diff --git a/certbot-compatibility-test/certbot_compatibility_test/configurators/apache/common.py b/certbot-compatibility-test/certbot_compatibility_test/configurators/apache/common.py index 5d5542ffd..b6fbe2817 100644 --- a/certbot-compatibility-test/certbot_compatibility_test/configurators/apache/common.py +++ b/certbot-compatibility-test/certbot_compatibility_test/configurators/apache/common.py @@ -57,7 +57,7 @@ class Proxy(configurators_common.Proxy): def _prepare_configurator(self): """Prepares the Apache plugin for testing""" - for k in entrypoint.ENTRYPOINT.OS_DEFAULTS.keys(): + for k in entrypoint.ENTRYPOINT.OS_DEFAULTS: setattr(self.le_config, "apache_" + k, entrypoint.ENTRYPOINT.OS_DEFAULTS[k]) diff --git a/certbot/certbot/_internal/storage.py b/certbot/certbot/_internal/storage.py index 05d9e3a8d..84c103901 100644 --- a/certbot/certbot/_internal/storage.py +++ b/certbot/certbot/_internal/storage.py @@ -127,7 +127,7 @@ def write_renewal_config(o_filename, n_filename, archive_dir, target, relevant_d config["renewalparams"].update(relevant_data) - for k in config["renewalparams"].keys(): + for k in config["renewalparams"]: if k not in relevant_data: del config["renewalparams"][k] diff --git a/certbot/certbot/plugins/storage.py b/certbot/certbot/plugins/storage.py index 9123087e7..f3ed14dce 100644 --- a/certbot/certbot/plugins/storage.py +++ b/certbot/certbot/plugins/storage.py @@ -106,7 +106,7 @@ class PluginStorage(object): if not self._initialized: self._initialize_storage() - if not self._classkey in self._data.keys(): + if self._classkey not in self._data: self._data[self._classkey] = {} self._data[self._classkey][key] = value diff --git a/tests/letstest/multitester.py b/tests/letstest/multitester.py index dc070b950..cf9f2899a 100644 --- a/tests/letstest/multitester.py +++ b/tests/letstest/multitester.py @@ -322,7 +322,7 @@ def create_client_instance(ec2_client, target, security_group_id, subnet_id): else: # 32 bit systems machine_type = 'c1.medium' - if 'userdata' in target.keys(): + if 'userdata' in target: userdata = target['userdata'] else: userdata = ''