From 36bbc90a369762193d6fcd5fb3e88c6cdd9980fe Mon Sep 17 00:00:00 2001 From: "ChangBo Guo(gcb)" Date: Wed, 18 Mar 2015 20:18:52 +0800 Subject: [PATCH] Fixes nits in check_no_contextlib_nested Currently, check will not work if nested is imported like below: from contextlib import nested with nested(...... This commit adds the case and add unit test for it. Change-Id: I60aa280054d9950b1d2edc9e23505c3395f96fb0 --- cinder/hacking/checks.py | 3 ++- cinder/tests/test_hacking.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cinder/hacking/checks.py b/cinder/hacking/checks.py index 972748658..762d7a099 100644 --- a/cinder/hacking/checks.py +++ b/cinder/hacking/checks.py @@ -46,6 +46,7 @@ no_audit_log = re.compile(r"(.)*LOG\.audit(.)*") # imports, we will need to add them to the regex below. oslo_namespace_imports = re.compile(r"from[\s]*oslo[.](concurrency|db" "|config|utils|serialization|log)") +no_contextlib_nested = re.compile(r"\s*with (contextlib\.)?nested\(") def no_vi_headers(physical_line, line_number, lines): @@ -143,7 +144,7 @@ def check_no_contextlib_nested(logical_line): "the with-statement supports multiple nested objects. See https://" "docs.python.org/2/library/contextlib.html#contextlib.nested " "for more information.") - if "with contextlib.nested" in logical_line: + if no_contextlib_nested.match(logical_line): yield(0, msg) diff --git a/cinder/tests/test_hacking.py b/cinder/tests/test_hacking.py index 365152b31..32e57e2a5 100644 --- a/cinder/tests/test_hacking.py +++ b/cinder/tests/test_hacking.py @@ -167,5 +167,9 @@ class HackingTestCase(test.TestCase): def test_no_contextlib_nested(self): self.assertEqual(1, len(list(checks.check_no_contextlib_nested( "with contextlib.nested(")))) + self.assertEqual(1, len(list(checks.check_no_contextlib_nested( + " with nested(")))) + self.assertEqual(0, len(list(checks.check_no_contextlib_nested( + "with my.nested(")))) self.assertEqual(0, len(list(checks.check_no_contextlib_nested( "with foo as bar")))) -- 2.45.2