Skip to content

missing-literal: negative integer values parsed as UnaryOp, not Constant #18

@nullhack

Description

@nullhack

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

  1. Create a feature file with Example step containing a negative number: Given the error code -2010
  2. Write a test with -2010 literally in the body
  3. 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions