{"record":{"id":"07c215caa0e36d1c","repo":"google/python-fire","slug":"value","errorCode":null,"errorMessage":"{value}","messagePattern":"\\{value\\}","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"fire/parser.py","lineNumber":100,"sourceCode":"  First the AST of the value is updated so that bare-words are turned into\n  strings. Then the resulting AST is evaluated as a literal or container of\n  only containers and literals.\n\n  This allows for the YAML-like syntax {a: b} to represent the dict {'a': 'b'}\n\n  Args:\n    value: A string to be parsed as a literal or container of containers and\n      literals.\n  Returns:\n    The Python value representing the value arg.\n  Raises:\n    ValueError: If the value is not an expression with only containers and\n      literals.\n    SyntaxError: If the value string has a syntax error.\n  \"\"\"\n  root = ast.parse(value, mode='eval')\n  if isinstance(root.body, ast.BinOp):\n    raise ValueError(value)\n\n  for node in ast.walk(root):\n    for field, child in ast.iter_fields(node):\n      if isinstance(child, list):\n        for index, subchild in enumerate(child):\n          if isinstance(subchild, ast.Name):\n            child[index] = _Replacement(subchild)\n\n      elif isinstance(child, ast.Name):\n        replacement = _Replacement(child)\n        setattr(node, field, replacement)\n\n  # ast.literal_eval supports the following types:\n  # strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None\n  # (bytes and set literals only starting with Python 3.2)\n  return ast.literal_eval(root)\n\n","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/google/python-fire/blob/716bbc23d7eca949fdb682172283c8d18f742cb6/fire/parser.py#L82-L118","documentation":"Python Fire's _LiteralEval raises ValueError when a command-line value parses to an AST whose top-level node is a BinOp (an arithmetic/comparison expression like `1+2` or `a-b`). Fire deliberately rejects expressions so they are never evaluated; the public DefaultParseValue catches this ValueError and falls back to treating the value as a plain string. The error only surfaces if you call _LiteralEval (or the parser internals) directly.","triggerScenarios":"Calling fire.parser._LiteralEval('1+2') (or DefaultParseValue's internals bypassed) with a string whose eval-mode AST root is ast.BinOp, e.g. '3*4', 'x-y', '(1,2)+(3,4)'. Also triggered when Fire code paths that expect a literal call _LiteralEval instead of DefaultParseValue.","commonSituations":"Users pass arithmetic like '--count=5+1' expecting Fire to compute it; Fire instead treats '5+1' as the string '5+1'. Developers testing or reusing Fire's parser directly hit the raw ValueError when they assume it behaves like ast.literal_eval with expression support.","solutions":["Use fire.parser.DefaultParseValue instead of _LiteralEval; it catches this ValueError and returns the value as a string.","If you need computed values, do the arithmetic in your own component or accept the value as a string and parse it yourself.","Pass a plain literal (e.g. '6' instead of '5+1') on the command line.","Wrap direct _LiteralEval calls in try/except (ValueError, SyntaxError) and fall back to the raw string, mirroring DefaultParseValue."],"exampleFix":"// before\nfrom fire.parser import _LiteralEval\nvalue = _LiteralEval('1+2')  # ValueError\n\n// after\nfrom fire.parser import DefaultParseValue\nvalue = DefaultParseValue('1+2')  # returns the string '1+2'","handlingStrategy":"validation","validationCode":"import ast\n\ndef is_safe_literal(value):\n    try:\n        root = ast.parse(value, mode='eval')\n    except SyntaxError:\n        return False\n    return not isinstance(root.body, ast.BinOp)\n\n# call _LiteralEval only when is_safe_literal(value) is True","typeGuard":"import ast\n\ndef is_eval_safe_literal(value: str) -> bool:\n    try:\n        root = ast.parse(value, mode='eval')\n    except (SyntaxError, ValueError):\n        return False\n    return not isinstance(root.body, ast.BinOp)","tryCatchPattern":"try:\n    parsed = fire.parser._LiteralEval(value)\nexcept (ValueError, SyntaxError):\n    parsed = value  # fall back to raw string, as DefaultParseValue does","preventionTips":["Prefer DefaultParseValue over _LiteralEval; it already handles this fallback.","Never expect Fire to evaluate arithmetic on the command line; pass precomputed literals.","When reusing the parser, always catch both ValueError and SyntaxError.","Validate values with ast.parse before calling literal-eval style helpers."],"tags":["python","python-fire","arg-parsing","valueerror","cli"],"backgroundTag":"non-literal-expression-rejected","analyzedSha":"716bbc23d7eca949fdb682172283c8d18f742cb6","analyzedAt":"2026-08-28T21:57:47.200Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}