{"id":"6bb6ca401b913dcb","repo":"pallets/flask","slug":"failed-to-parse-arguments-as-literal-values-app","errorCode":null,"errorMessage":"Failed to parse arguments as literal values: {app_name!r}.","messagePattern":"Failed to parse arguments as literal values: (.+?)\\.","errorType":"exception","errorClass":"NoAppException","httpStatus":null,"severity":"error","filePath":"src/flask/cli.py","lineNumber":159,"sourceCode":"        if not isinstance(expr.func, ast.Name):\n            raise NoAppException(\n                f\"Function reference must be a simple name: {app_name!r}.\"\n            )\n\n        name = expr.func.id\n\n        # Parse the positional and keyword arguments as literals.\n        try:\n            args = [ast.literal_eval(arg) for arg in expr.args]\n            kwargs = {\n                kw.arg: ast.literal_eval(kw.value)\n                for kw in expr.keywords\n                if kw.arg is not None\n            }\n        except ValueError:\n            # literal_eval gives cryptic error messages, show a generic\n            # message with the full expression instead.\n            raise NoAppException(\n                f\"Failed to parse arguments as literal values: {app_name!r}.\"\n            ) from None\n    else:\n        raise NoAppException(\n            f\"Failed to parse {app_name!r} as an attribute name or function call.\"\n        )\n\n    try:\n        attr = getattr(module, name)\n    except AttributeError as e:\n        raise NoAppException(\n            f\"Failed to find attribute {name!r} in {module.__name__!r}.\"\n        ) from e\n\n    # If the attribute is a function, call it with any args and kwargs\n    # to get the real application.\n    if inspect.isfunction(attr):\n        try:","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/pallets/flask/blob/6a2f545bfd8ed31e19066a299296917e034aca58/src/flask/cli.py#L141-L177","documentation":"A `NoAppException` from `find_app_by_string` when the factory-call form was parsed but one of its arguments failed `ast.literal_eval`. Flask only accepts Python literals (strings, numbers, tuples, lists, dicts, booleans, None) as factory arguments — it will not evaluate variables, function calls, or expressions like `os.environ['X']`, since the app string is data, not executed code. literal_eval's ValueError is caught and replaced with this clearer message.","triggerScenarios":"`flask --app 'module:create_app(config)' run` where `config` is a bare name (not a quoted string), or arguments like `create_app(os.getenv(\"ENV\"))`, `create_app(1+1)`, or any non-literal expression in the argument list.","commonSituations":"Forgetting quotes around a string argument — compounded by shells stripping the inner quotes so Flask receives `create_app(dev)` instead of `create_app(\"dev\")`; trying to compute config dynamically inside the app string instead of inside the factory; passing objects that can't be expressed as literals.","solutions":["Quote string arguments and protect them from the shell: `flask --app 'myapp:create_app(\"production\")' run` (on Windows cmd: `flask --app \"myapp:create_app('production')\" run`).","Move dynamic logic into the factory itself (read env vars inside create_app) and call it with literals or no arguments.","Restrict arguments to literal types; pass a config name string and resolve it to an object inside the factory."],"exampleFix":"# before\nflask --app 'myapp:create_app(os.environ[\"CONFIG\"])' run\n\n# after\nflask --app 'myapp:create_app(\"production\")' run\n# or resolve inside the factory:\ndef create_app(config_name=None):\n    config_name = config_name or os.environ.get(\"CONFIG\", \"development\")","handlingStrategy":"validation","validationCode":"import ast\nfor arg in ['\"prod\"', '8080', 'True']:\n    ast.literal_eval(arg)  # raises if not a literal","typeGuard":"def args_are_literals(arg_strings):\n    import ast\n    try:\n        for a in arg_strings:\n            ast.literal_eval(a)\n        return True\n    except (ValueError, SyntaxError):\n        return False","tryCatchPattern":null,"preventionTips":["Pass only Python literals to the factory in --app: strings, numbers, tuples, lists, dicts, booleans, None","Never reference variables, os.environ, or function calls inside the parentheses — they cannot be evaluated","Mind shell quoting: use --app 'module:create_app(\"prod\")' so inner quotes reach Flask intact","For anything non-literal, read it from environment variables inside the factory instead"],"tags":["flask","cli","parsing","configuration"],"analyzedSha":"6a2f545bfd8ed31e19066a299296917e034aca58","analyzedAt":"2026-07-31T19:13:49.921Z","schemaVersion":2}