{"record":{"id":"27434e71871a4e33","repo":"pandas-dev/pandas","slug":"value-must-be-an-instance-of-type-repr","errorCode":null,"errorMessage":"Value must be an instance of {type_repr}","messagePattern":"Value must be an instance of (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/_config/config.py","lineNumber":885,"sourceCode":"\n    Parameters\n    ----------\n    `_type` - the type to be checked against\n\n    Returns\n    -------\n    validator - a function of a single argument x , which raises\n                ValueError if x is not an instance of `_type`\n\n    \"\"\"\n    if isinstance(_type, tuple):\n        type_repr = \"|\".join(map(str, _type))\n    else:\n        type_repr = f\"'{_type}'\"\n\n    def inner(x: object) -> None:\n        if not isinstance(x, _type):\n            raise ValueError(f\"Value must be an instance of {type_repr}\")\n\n    return inner\n\n\ndef is_one_of_factory(legal_values: Sequence) -> Callable[[Any], None]:\n    callables = [c for c in legal_values if callable(c)]\n    legal_values = [c for c in legal_values if not callable(c)]\n\n    def inner(x: object) -> None:\n        if x not in legal_values:\n            if not any(c(x) for c in callables):\n                uvals = [str(lval) for lval in legal_values]\n                pp_values = \"|\".join(uvals)\n                msg = f\"Value must be one of {pp_values}\"\n                if len(callables):\n                    msg += \" or a callable\"\n                raise ValueError(msg)\n","sourceCodeStart":867,"sourceCodeEnd":903,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/_config/config.py#L867-L903","documentation":"Raised by `is_instance_factory` when a value passed to a registered pandas option fails an `isinstance` check against the configured type(s). pandas uses these validators to enforce option value types at `set_option` time. The message names the expected type (or a pipe-joined list when the validator accepts a tuple). It is a hard ValueError meant to stop invalid configuration from silently corrupting downstream behavior.","triggerScenarios":"Calling `pd.set_option('display.max_rows', 'ten')` (string instead of int), `pd.set_option('mode.sim_interactive', 1)` (int instead of bool), or any `set_option`/option_context whose value fails the validator registered via `is_instance_factory` (used widely for display, indexing, and copy-on-write options).","commonSituations":"Loading config from YAML/ENV as strings and forwarding it straight into set_option; using numpy types where python builtins are expected (e.g. np.int64 vs int); interactive notebooks where a user reassigns an option with a wrong type.","solutions":["Read the message's expected type and coerce the value to that exact python type before calling set_option.","If loading from text config, write a small mapper that casts known options (int options -> int(), bool options -> str.lower() in {'true','1'}) before applying.","Confirm the option's registered validator with `pd.describe_option(name)` to see the allowed type before assigning."],"exampleFix":"// before\npd.set_option('display.max_rows', '100')\n\n// after\npd.set_option('display.max_rows', 100)","handlingStrategy":"validation","validationCode":"def safe_set_option(name, value):\n    import pandas as pd\n    pd.describe_option(name)  # raises if unknown\n    # describe_option prints the registered type in its docstring\n    pd.set_option(name, value)","typeGuard":"def is_int_like(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool)","tryCatchPattern":"try:\n    pd.set_option(name, value)\nexcept ValueError as e:\n    # log and fall back to a known-good default\n    pd.set_option(name, default)","preventionTips":["Centralize option setting behind one typed config loader.","Cast values from config files to the documented type before calling set_option."],"tags":["config","type-validation","set-option"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}