From: ankitagrawal Date: Thu, 14 May 2015 12:19:44 +0000 (-0700) Subject: Remove use of contextlib.nested (api-tests) X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=8ee3020c47cd7eb716216852a90c3db595e2c3c6;p=openstack-build%2Fneutron-build.git Remove use of contextlib.nested (api-tests) Removed use of contextlib.nested call from codebase, as it has been deprecated since Python 2.7. There are also known issues with contextlib.nested that were addressed by the native support for multiple "with" variables. For instance, if the first object is created but the second one throws an exception, the first object's __exit__ is never called. For more information see https://docs.python.org/2/library/contextlib.html#contextlib.nested contextlib.nested is also not compatible with Python 3. Multi-patch set for easier chunks. This one addresses the tests from neutron/tests/unit/api directory. Line continuation markers (e.g. '\') had to be used or syntax errors were thrown. While using parentheses is the preferred way for multiple line statements, but in case of long with statements backslashes are acceptable. Partial-Bug: 1428424 Change-Id: I09673f9d4c7f07d3043804676fef018905dd1239 --- diff --git a/neutron/hacking/checks.py b/neutron/hacking/checks.py index e013b7deb..c2eabbd77 100644 --- a/neutron/hacking/checks.py +++ b/neutron/hacking/checks.py @@ -146,7 +146,6 @@ def check_no_contextlib_nested(logical_line, filename): # these issues. It should be removed completely # when bug 1428424 is closed. ignore_dirs = [ - "neutron/tests/unit/api", "neutron/tests/unit/db", "neutron/tests/unit/extensions", "neutron/tests/unit/plugins", diff --git a/neutron/tests/unit/api/rpc/handlers/test_securitygroups_rpc.py b/neutron/tests/unit/api/rpc/handlers/test_securitygroups_rpc.py index 7c8b79f67..672806209 100644 --- a/neutron/tests/unit/api/rpc/handlers/test_securitygroups_rpc.py +++ b/neutron/tests/unit/api/rpc/handlers/test_securitygroups_rpc.py @@ -12,7 +12,6 @@ # License for the specific language governing permissions and limitations # under the License. -import contextlib import mock from neutron.api.rpc.handlers import securitygroups_rpc @@ -24,12 +23,8 @@ class SecurityGroupServerRpcApiTestCase(base.BaseTestCase): def test_security_group_rules_for_devices(self): rpcapi = securitygroups_rpc.SecurityGroupServerRpcApi('fake_topic') - with contextlib.nested( - mock.patch.object(rpcapi.client, 'call'), - mock.patch.object(rpcapi.client, 'prepare'), - ) as ( - rpc_mock, prepare_mock - ): + with mock.patch.object(rpcapi.client, 'call') as rpc_mock,\ + mock.patch.object(rpcapi.client, 'prepare') as prepare_mock: prepare_mock.return_value = rpcapi.client rpcapi.security_group_rules_for_devices('context', ['fake_device'])