Fix: In fire/parser.py's _LiteralEval, the AST walk replaces bare ast.Name... - #694
Open
M001N wants to merge 1 commit into
Open
Fix: In fire/parser.py's _LiteralEval, the AST walk replaces bare ast.Name...#694M001N wants to merge 1 commit into
M001N wants to merge 1 commit into
Conversation
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.
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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