Merge "Update log translation hacking check"
This commit is contained in:
commit
2f842981c2
@ -295,51 +295,9 @@ class CheckForTranslationIssues(BaseASTChecker):
|
|||||||
|
|
||||||
# if the first arg is a reference to a i18n call
|
# if the first arg is a reference to a i18n call
|
||||||
elif (isinstance(msg, ast.Name)
|
elif (isinstance(msg, ast.Name)
|
||||||
and msg.id in self.assignments
|
and msg.id in self.assignments):
|
||||||
and not self._is_raised_later(node, msg.id)):
|
|
||||||
self.add_error(msg, message=self.LOGGING_CHECK_DESC)
|
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):
|
def dict_constructor_with_sequence_copy(logical_line):
|
||||||
"""Should use a dict comprehension instead of a dict constructor.
|
"""Should use a dict comprehension instead of a dict constructor.
|
||||||
|
@ -220,24 +220,28 @@ class HackingTranslations(fixtures.Fixture):
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
'code': """
|
'code': """
|
||||||
# this should not be an error
|
# this should be an error even if it'll be raised later.
|
||||||
L = log.getLogger(__name__)
|
L = log.getLogger(__name__)
|
||||||
msg = _('text')
|
msg = _('text')
|
||||||
L.warning(msg)
|
L.warning(msg)
|
||||||
raise Exception(msg)
|
raise Exception(msg)
|
||||||
""",
|
""",
|
||||||
'expected_errors': [],
|
'expected_errors': [
|
||||||
|
(4, 10, 'K005'),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'code': """
|
'code': """
|
||||||
L = log.getLogger(__name__)
|
L = log.getLogger(__name__)
|
||||||
def f():
|
def f():
|
||||||
msg = _('text')
|
msg = _('text')
|
||||||
L2.warning(msg)
|
L.warning(msg)
|
||||||
something = True # add an extra statement here
|
something = True # add an extra statement here
|
||||||
raise Exception(msg)
|
raise Exception(msg)
|
||||||
""",
|
""",
|
||||||
'expected_errors': [],
|
'expected_errors': [
|
||||||
|
(4, 14, 'K005'),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'code': """
|
'code': """
|
||||||
@ -262,6 +266,7 @@ class HackingTranslations(fixtures.Fixture):
|
|||||||
raise Exception(msg)
|
raise Exception(msg)
|
||||||
""",
|
""",
|
||||||
'expected_errors': [
|
'expected_errors': [
|
||||||
|
(6, 12, 'K005'),
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -320,6 +325,8 @@ class HackingTranslations(fixtures.Fixture):
|
|||||||
LOG.warning(msg)
|
LOG.warning(msg)
|
||||||
raise exception.Unauthorized(message=msg)
|
raise exception.Unauthorized(message=msg)
|
||||||
""",
|
""",
|
||||||
'expected_errors': [],
|
'expected_errors': [
|
||||||
|
(7, 16, 'K005'),
|
||||||
|
],
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user