Removed unnecessary parantheses in yield statements
The 'yield' statement is not a function. So it must always be followed by a space when yielding a value. Change-Id: Ie2aa1c4822d8adf721ba71d467b63209bede6fb7
This commit is contained in:
parent
c1716490cd
commit
fbb69db7c7
|
@ -67,6 +67,7 @@ Nova Specific Commandments
|
|||
generate UUID instead of uuid4().
|
||||
- [N358] Return must always be followed by a space when returning a value.
|
||||
- [N359] Check for redundant import aliases.
|
||||
- [N360] Yield must always be followed by a space when yielding a value.
|
||||
|
||||
Creating Unit Tests
|
||||
-------------------
|
||||
|
|
|
@ -99,6 +99,7 @@ log_remove_context = re.compile(
|
|||
return_not_followed_by_space = re.compile(r"^\s*return(?:\(|{|\"|'|#).*$")
|
||||
uuid4_re = re.compile(r"uuid4\(\)($|[^\.]|\.hex)")
|
||||
redundant_import_alias_re = re.compile(r"import (?:.*\.)?(.+) as \1$")
|
||||
yield_not_followed_by_space = re.compile(r"^\s*yield(?:\(|{|\[|\"|').*$")
|
||||
|
||||
|
||||
class BaseASTChecker(ast.NodeVisitor):
|
||||
|
@ -817,6 +818,23 @@ def no_redundant_import_alias(logical_line):
|
|||
yield (0, "N359: Import alias should not be redundant.")
|
||||
|
||||
|
||||
def yield_followed_by_space(logical_line):
|
||||
"""Yield should be followed by a space.
|
||||
|
||||
Yield should be followed by a space to clarify that yield is
|
||||
not a function. Adding a space may force the developer to rethink
|
||||
if there are unnecessary parentheses in the written code.
|
||||
|
||||
Not correct: yield(x), yield(a, b)
|
||||
Correct: yield x, yield (a, b), yield a, b
|
||||
|
||||
N360
|
||||
"""
|
||||
if yield_not_followed_by_space.match(logical_line):
|
||||
yield (0,
|
||||
"N360: Yield keyword should be followed by a space.")
|
||||
|
||||
|
||||
def factory(register):
|
||||
register(import_no_db_in_virt)
|
||||
register(no_db_session_in_public_api)
|
||||
|
@ -859,3 +877,4 @@ def factory(register):
|
|||
register(check_uuid4)
|
||||
register(return_followed_by_space)
|
||||
register(no_redundant_import_alias)
|
||||
register(yield_followed_by_space)
|
||||
|
|
|
@ -802,3 +802,25 @@ class HackingTestCase(test.NoDBTestCase):
|
|||
import ab.cd.efg as d.efg
|
||||
"""
|
||||
self._assert_has_no_errors(code, checks.no_redundant_import_alias)
|
||||
|
||||
def test_yield_followed_by_space(self):
|
||||
code = """
|
||||
yield(x, y)
|
||||
yield{"type": "test"}
|
||||
yield[a, b, c]
|
||||
yield"test"
|
||||
yield'test'
|
||||
"""
|
||||
errors = [(x + 1, 0, 'N360') for x in range(5)]
|
||||
self._assert_has_errors(code, checks.yield_followed_by_space,
|
||||
expected_errors=errors)
|
||||
code = """
|
||||
yield x
|
||||
yield (x, y)
|
||||
yield {"type": "test"}
|
||||
yield [a, b, c]
|
||||
yield "test"
|
||||
yield 'test'
|
||||
yieldx_func(a, b)
|
||||
"""
|
||||
self._assert_has_no_errors(code, checks.yield_followed_by_space)
|
||||
|
|
Loading…
Reference in New Issue