Skip to content

Fix: In fire/parser.py's _LiteralEval, the AST walk replaces bare ast.Name... - #694

Open
M001N wants to merge 1 commit into
google:masterfrom
M001N:oss-engine/36bdcf82-f0390c88
Open

Fix: In fire/parser.py's _LiteralEval, the AST walk replaces bare ast.Name...#694
M001N wants to merge 1 commit into
google:masterfrom
M001N:oss-engine/36bdcf82-f0390c88

Conversation

@M001N

@M001N M001N commented Aug 16, 2026

Copy link
Copy Markdown

Summary

Added a helper _IsNegativeBareword(node) that detects ast.UnaryOp(USub, Name) where the Name isn't True/False/None. In the existing bareword-replacement walk (both the list-field branch and the single-field branch), when a child matches this pattern, the ENTIRE UnaryOp node is replaced with a string Constant of '-' + the identifier name (e.g. '-b'), instead of only replacing the inner Name. Genuine numeric negation (UnaryOp(USub, Constant(2))) doesn't match the pattern (operand is Constant, not Name) and is left untouched, so ast.literal_eval still evaluates it as int -2.

Problem

google/python-fire issue reference: #229

Root Cause

In fire/parser.py's _LiteralEval, the AST walk replaces bare ast.Name nodes with string ast.Constant nodes to support YAML-like unquoted-string syntax. For the token '-b', the parser produces ast.UnaryOp(op=USub, operand=ast.Name('b')). The walk replaced only the inner Name('b') with Constant('b'), leaving UnaryOp(USub, Constant('b')) -- an invalid 'negative string' expression that ast.literal_eval rejects with ValueError. DefaultParseValue's outer except then discarded all list structure and returned the raw input string.

Testing

PASS for parser_test.py (21/21 passed, including new test covering '[a,-b,c]' -> ['a','-b','c'], '[1,-2,3]' -> [1,-2,3] with -2 as int, and standalone '-b' -> '-b'). Full suite: 260 passed, 2 failed, but both failures (main_test.py::MainModuleTest::testArgPassing, main_test.py::MainModuleFileTest::testFileNameFire) are pre-existing, unrelated Windows-environment issues (regex pattern error and a PermissionError writing to a Windows temp file during module import) with no connection to parser.py.

Related Issue

#229

DefaultParseValue('[a,-b,c]') previously fell back to treating the
whole value as a plain string, because the bareword-replacement AST
walk in _LiteralEval replaced the inner Name node of
UnaryOp(USub, Name('b')) with a string Constant, leaving an invalid
UnaryOp(USub, Constant('b')) that ast.literal_eval rejects.

Now the walk detects a UnaryOp(USub, Name) -- i.e. a unary minus
applied to a bare identifier -- and replaces the whole UnaryOp node
with a single string Constant '-' + name, so it parses as the string
'-b' instead. Numeric negation (UnaryOp(USub, Constant(2))) is
untouched and still evaluates to the int -2.
@google-cla

google-cla Bot commented Aug 16, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant