Description
beehave's missing-literal check fails to find negative integer literals like -2010 in test function bodies.
Root Cause
Python's AST parser represents -2010 as:
UnaryOp(op=USub(), operand=Constant(value=2010))
The _extract_body_nodes() function in discover.py walks the AST with ast.walk() and only checks isinstance(node, ast.Constant). It finds Constant(2010) but never finds the value -2010 because the negation is a unary operation wrapper.
Steps to Reproduce
- Create a feature file with Example step containing a negative number:
Given the error code -2010
- Write a test with
-2010 literally in the body
- Run
beehave check
Expected Behavior
-2010 is found as a literal in the test body.
Actual Behavior
Error: missing-literal: literal '-2010' not found in function body
Fix
In discover.py:_extract_body_nodes(), add handling for ast.UnaryOp with ast.USub:
for node in ast.walk(parsed):
if isinstance(node, ast.Constant):
literal_scan(node, node.value)
elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
if isinstance(node.operand, ast.Constant):
literal_scan(node, -node.operand.value)
This also affects ast.UnaryOp with ast.UAdd (+1) and ast.Invert (~1).
Description
beehave's
missing-literalcheck fails to find negative integer literals like-2010in test function bodies.Root Cause
Python's AST parser represents
-2010as:The
_extract_body_nodes()function indiscover.pywalks the AST withast.walk()and only checksisinstance(node, ast.Constant). It findsConstant(2010)but never finds the value-2010because the negation is a unary operation wrapper.Steps to Reproduce
Given the error code -2010-2010literally in the bodybeehave checkExpected Behavior
-2010is found as a literal in the test body.Actual Behavior
Error:
missing-literal: literal '-2010' not found in function bodyFix
In
discover.py:_extract_body_nodes(), add handling forast.UnaryOpwithast.USub:This also affects
ast.UnaryOpwithast.UAdd(+1) andast.Invert(~1).