{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/_config/config.py#L489-L525","documentation":"Raised by option_context (config.py:506-510) as a ValueError when the number of positional args is odd or fewer than 2. Like set_option it accepts (pattern, value) pairs or a single dict, which is unwrapped before the check.","triggerScenarios":"with pd.option_context('display.max_columns'): ... (1 arg), or an odd-length tuple of pairs.","commonSituations":"Forgetting the value, or passing an odd number of args after refactoring a context block.","solutions":["Pass pairs: with pd.option_context('display.max_columns', 10, 'display.max_rows', 20): ....","Pass a dict: with pd.option_context({'display.max_columns': 10}): ....","Validate len(args) is even and >= 2 before constructing the call."],"exampleFix":"# before\nwith pd.option_context('display.max_columns'):  # ValueError\n    ...\n\n# after\nwith pd.option_context('display.max_columns', 10):\n    ...","handlingStrategy":"validation","validationCode":"def safe_option_context(*args):\n    assert len(args) >= 2 and len(args) % 2 == 0, 'option_context needs (pat, val) pairs'\n    return pd.option_context(*args)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer the dict form: pd.option_context({'a': 1, 'b': 2}).","Wrap dynamic context building in a helper that validates pair count.","Lint option_context calls in review for missing values."],"tags":["config","option-context","argument-validation"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}