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_python_rule():
with pytest.raises(SyntaxError):
for exp in SYNTAXERROR_EXPRESSIONS:
PythonExpressionRule(exp)
with pytest.raises(InvalidExpression):
for exp in INVALID_EXPRESSIONS:
PythonExpressionRule(exp)
for exp in VALID_EXPRESSIONS:
PythonExpressionRule(exp)
rule = PythonExpressionRule(RULE_EXPRESSION)
_test_rule_with_stats(rule)
for expression, result in EXPRESSIONS_TO_EVALUATE:
rule = PythonExpressionRule(expression)
assert result == rule.run_check(STATS_TO_EVALUATE), \
'Expression fails: "%s" != %s' % (expression, result)
def _raise_not_allowed_node(self, node):
raise InvalidExpression(
"'%s' definition not allowed in python expressions"
% node.__class__.__name__
)
def check(self, expression):
if not isinstance(expression, six.string_types):
raise InvalidExpression("Python expressions must be defined as strings")
if not expression:
raise InvalidExpression("Empty python expression")
try:
tree = ast.parse(expression)
except SyntaxError as e:
raise e
if not tree.body:
raise InvalidExpression("Empty python expression")
elif len(tree.body) > 1:
raise InvalidExpression(
"Python expressions must be a single line expression"
)
start_node = tree.body[0]
if not isinstance(start_node, ast.Expr):
raise InvalidExpression(
"Python string must be an expression: '%s' found"
% start_node.__class__.__name__
)
self._check_node(start_node)
def check(self, expression):
if not isinstance(expression, six.string_types):
raise InvalidExpression("Python expressions must be defined as strings")
if not expression:
raise InvalidExpression("Empty python expression")
try:
tree = ast.parse(expression)
except SyntaxError as e:
raise e
if not tree.body:
raise InvalidExpression("Empty python expression")
elif len(tree.body) > 1:
raise InvalidExpression(
"Python expressions must be a single line expression"
)
start_node = tree.body[0]
if not isinstance(start_node, ast.Expr):
raise InvalidExpression(
try:
tree = ast.parse(expression)
except SyntaxError as e:
raise e
if not tree.body:
raise InvalidExpression("Empty python expression")
elif len(tree.body) > 1:
raise InvalidExpression(
"Python expressions must be a single line expression"
)
start_node = tree.body[0]
if not isinstance(start_node, ast.Expr):
raise InvalidExpression(
"Python string must be an expression: '%s' found"
% start_node.__class__.__name__
)
self._check_node(start_node)
def check(self, expression):
if not isinstance(expression, six.string_types):
raise InvalidExpression("Python expressions must be defined as strings")
if not expression:
raise InvalidExpression("Empty python expression")
try:
tree = ast.parse(expression)
except SyntaxError as e:
raise e
if not tree.body:
raise InvalidExpression("Empty python expression")
elif len(tree.body) > 1:
raise InvalidExpression(
"Python expressions must be a single line expression"
)
start_node = tree.body[0]
def check(self, expression):
if not isinstance(expression, six.string_types):
raise InvalidExpression("Python expressions must be defined as strings")
if not expression:
raise InvalidExpression("Empty python expression")
try:
tree = ast.parse(expression)
except SyntaxError as e:
raise e
if not tree.body:
raise InvalidExpression("Empty python expression")
elif len(tree.body) > 1:
raise InvalidExpression(
"Python expressions must be a single line expression"
)
start_node = tree.body[0]
if not isinstance(start_node, ast.Expr):
raise InvalidExpression(
"Python string must be an expression: '%s' found"
% start_node.__class__.__name__
)
self._check_node(start_node)