Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_errors():
conditions = [
'foo',
'__name__ == "__main__"',
'chr(-1)',
'getattr(True, "foo")',
'hasattr(str, "foo")',
'isinstance(True, True)',
'globals()',
'locals()',
'().__class__',
]
for condition in conditions:
condition = ast.parse(condition, mode='eval').body
assert not utils.condition_is_always_false(condition)
assert not utils.condition_is_always_true(condition)
('foo and False', True, False),
('foo or False', False, False),
('foo and True', False, False),
('foo or True', False, True),
('False and foo', True, False),
('False and 1', True, False),
('not False', False, True),
('not True', True, False),
('not foo', False, False),
('foo and (False or [])', True, False),
('(foo and bar) or {"a": 1}', False, True),
]
for condition, always_false, always_true in conditions:
condition = ast.parse(condition, mode='eval').body
assert not (always_false and always_true)
assert utils.condition_is_always_false(condition) == always_false
assert utils.condition_is_always_true(condition) == always_true
def check_condition(code, result):
condition = ast.parse(code, mode='eval').body
if result:
assert utils.condition_is_always_true(condition)
else:
assert utils.condition_is_always_false(condition)
def _handle_conditional_node(self, node, name):
if utils.condition_is_always_false(node.test):
self._define(
self.unreachable_code, name, node,
last_node=node.body[-1],
message="unsatisfiable '{name}' condition".format(**locals()),
confidence=100)
elif utils.condition_is_always_true(node.test):
else_body = node.orelse
if else_body:
self._define(
self.unreachable_code, 'else', else_body[0],
last_node=else_body[-1],
message="unreachable 'else' block",
confidence=100)
elif name == 'if':
# Redundant if-condition without else block.
self._define(