Merge "Update log translation hacking check"

This commit is contained in:
Zuul 2018-10-09 19:00:23 +00:00 committed by Gerrit Code Review
commit 2f842981c2
2 changed files with 13 additions and 48 deletions

View File

@ -295,51 +295,9 @@ class CheckForTranslationIssues(BaseASTChecker):
# if the first arg is a reference to a i18n call
elif (isinstance(msg, ast.Name)
and msg.id in self.assignments
and not self._is_raised_later(node, msg.id)):
and msg.id in self.assignments):
self.add_error(msg, message=self.LOGGING_CHECK_DESC)
def _is_raised_later(self, node, name):
def find_peers(node):
node_for_line = node._parent
for _field, value in ast.iter_fields(node._parent._parent):
if isinstance(value, list) and node_for_line in value:
return value[value.index(node_for_line) + 1:]
continue
return []
def is_in_args(node, name):
if (len(node.args) > 0 and isinstance(node.args[0], ast.Name)
and name in (a.id for a in node.args)):
return True
return False
def is_in_kwargs(node, name):
for keyword in node.keywords:
if (isinstance(keyword.value, ast.Name)
and keyword.value.id == name):
return True
return False
peers = find_peers(node)
for peer in peers:
if isinstance(peer, ast.Raise):
if six.PY3:
exc = peer.exc
else:
exc = peer.type
if isinstance(exc, ast.Call):
if is_in_args(exc, name):
return True
elif is_in_kwargs(exc, name):
return True
return False
elif isinstance(peer, ast.Assign):
if name in (t.id for t in peer.targets if hasattr(t, 'id')):
return False
def dict_constructor_with_sequence_copy(logical_line):
"""Should use a dict comprehension instead of a dict constructor.

View File

@ -220,24 +220,28 @@ class HackingTranslations(fixtures.Fixture):
},
{
'code': """
# this should not be an error
# this should be an error even if it'll be raised later.
L = log.getLogger(__name__)
msg = _('text')
L.warning(msg)
raise Exception(msg)
""",
'expected_errors': [],
'expected_errors': [
(4, 10, 'K005'),
],
},
{
'code': """
L = log.getLogger(__name__)
def f():
msg = _('text')
L2.warning(msg)
L.warning(msg)
something = True # add an extra statement here
raise Exception(msg)
""",
'expected_errors': [],
'expected_errors': [
(4, 14, 'K005'),
],
},
{
'code': """
@ -262,6 +266,7 @@ class HackingTranslations(fixtures.Fixture):
raise Exception(msg)
""",
'expected_errors': [
(6, 12, 'K005'),
],
},
{
@ -320,6 +325,8 @@ class HackingTranslations(fixtures.Fixture):
LOG.warning(msg)
raise exception.Unauthorized(message=msg)
""",
'expected_errors': [],
'expected_errors': [
(7, 16, 'K005'),
],
},
]