{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/_config/config.py#L867-L903","documentation":"Raised by the is_instance_factory validator returned for pandas configuration options registered via register_option(). When you call pd.set_option() (or pd.option_context) with a value that is not an instance of the type the option requires, this ValueError fires. The concrete instance-based validator shipped in the library is is_text = is_instance_factory((str, bytes)), used for the 'display.encoding' option (config.py:938, display.py:61).","triggerScenarios":"Calling pd.set_option('display.encoding', <non-str-bytes>) — e.g. an int or None — runs the is_text validator and hits line 885. Any third-party code that registers its own option with validator=cf.is_instance_factory(SomeType) and then sets an incompatible value triggers the same path.","commonSituations":"Setting display.encoding to None to 'reset' it; passing an int or bool where a string codec name is expected; dynamic config where a variable of the wrong type flows into set_option.","solutions":["Pass a str or bytes value, e.g. pd.set_option('display.encoding', 'utf-8').","If you need the default back, use pd.reset_option('display.encoding') instead of setting None.","Check the registered validator with pd.describe_option('display.encoding') before setting."],"exampleFix":"# before\npd.set_option('display.encoding', None)\n\n# after\npd.reset_option('display.encoding')","handlingStrategy":"validation","validationCode":"import pandas as pd\nvalue = get_value_from_config()\nif not isinstance(value, (str, bytes)):\n    raise TypeError(f'display.encoding must be str/bytes, got {type(value)}')\npd.set_option('display.encoding', value)","typeGuard":"def is_valid_encoding(value: object) -> bool:\n    return isinstance(value, (str, bytes))","tryCatchPattern":null,"preventionTips":["Validate config values against their option's validator before calling set_option.","Use reset_option() rather than setting None to restore defaults.","Describe an option first with pd.describe_option(key) to learn its accepted type."],"tags":["config","validation","set-option","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}