{"record":{"id":"ea914f480a3d689b","repo":"pandas-dev/pandas","slug":"provide-an-even-amount-of-arguments-as-option-cont","errorCode":null,"errorMessage":"Provide an even amount of arguments as option_context(pat, val, pat, val...).","messagePattern":"Provide an even amount of arguments as option_context\\(pat, val, pat, val\\.\\.\\.\\)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/_config/config.py","lineNumber":507,"sourceCode":"\n    Notes\n    -----\n    For all available options, please view the :ref:`User Guide <options.available>`\n    or use ``pandas.describe_option()``.\n\n    Examples\n    --------\n    >>> from pandas import option_context\n    >>> with option_context(\"display.max_rows\", 10, \"display.max_columns\", 5):\n    ...     pass\n    >>> with option_context({\"display.max_rows\": 10, \"display.max_columns\": 5}):\n    ...     pass\n    \"\"\"\n    if len(args) == 1 and isinstance(args[0], dict):\n        args = tuple(kv for item in args[0].items() for kv in item)\n\n    if len(args) % 2 != 0 or len(args) < 2:\n        raise ValueError(\n            \"Provide an even amount of arguments as \"\n            \"option_context(pat, val, pat, val...).\"\n        )\n\n    ops = tuple(zip(args[::2], args[1::2], strict=True))\n    undo: tuple[tuple[Any, Any], ...] = ()\n    try:\n        undo = tuple((pat, get_option(pat)) for pat, val in ops)\n        for pat, val in ops:\n            set_option(pat, val)\n        yield\n    finally:\n        for pat, val in undo:\n            set_option(pat, val)\n\n\ndef register_option(\n    key: str,","sourceCodeStart":489,"sourceCodeEnd":525,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/_config/config.py#L489-L525","documentation":"Raised by the `option_context` context manager when, after flattening a single-dict argument, the positional args are odd in number or fewer than 2. `option_context` expects `(pat, val, pat, val, ...)` pairs (or one dict) so it can snapshot the current values, apply the new ones for the `with` block, and restore them on exit.","triggerScenarios":"`with pd.option_context('display.max_rows'):` (value missing), `with pd.option_context('a', 1, 'b'):` (unbalanced), or `with pd.option_context():` (empty).","commonSituations":"Forgetting a value; refactoring that drops an arg; passing a single string instead of a pair.","solutions":["Pass key/value pairs: `with pd.option_context('display.max_rows', 10): ...`.","Pass a single dict: `with pd.option_context({'display.max_rows': 10, 'display.max_columns': 5}): ...`.","Ensure the total arg count (after dict flattening) is even and >= 2."],"exampleFix":"# before\nwith pd.option_context('display.max_rows'):\n    ...\n\n# after\nwith pd.option_context('display.max_rows', 10):\n    ...\n# or\nwith pd.option_context({'display.max_rows': 10}):\n    ...","handlingStrategy":"validation","validationCode":"def option_context_args(*args):\n    if len(args) == 1 and isinstance(args[0], dict):\n        args = tuple(kv for item in args[0].items() for kv in item)\n    if len(args) % 2 != 0 or len(args) < 2:\n        raise ValueError('option_context needs (pat, val)+ pairs or a single dict')\n    return args\npairs = option_context_args(*user_args)","typeGuard":"null","tryCatchPattern":"try:\n    with pd.option_context(*ctx_args):\n        ...\nexcept ValueError as e:\n    if 'even amount' in str(e):\n        # rebuild ctx_args as pairs/dict and retry\n        ...\n    raise","preventionTips":["Pass a single dict to `option_context` to avoid pair-count bugs.","Centralize context-manager arg construction in a helper that asserts even length.","Unit-test wrappers around option_context with odd-length inputs."],"tags":["config","option-context","argument-count","api-misuse"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}