{"record":{"id":"e2b7663b42d5459b","repo":"pandas-dev/pandas","slug":"value-must-have-type-type","errorCode":null,"errorMessage":"Value must have type '{_type}'","messagePattern":"Value must have type '(.+?)'","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/_config/config.py","lineNumber":860,"sourceCode":"\n\ndef is_type_factory(_type: type[Any]) -> Callable[[Any], None]:\n    \"\"\"\n\n    Parameters\n    ----------\n    `_type` - a type to be compared against (e.g. type(x) == `_type`)\n\n    Returns\n    -------\n    validator - a function of a single argument x , which raises\n                ValueError if type(x) is not equal to `_type`\n\n    \"\"\"\n\n    def inner(x: object) -> None:\n        if type(x) != _type:\n            raise ValueError(f\"Value must have type '{_type}'\")\n\n    return inner\n\n\ndef is_instance_factory(_type: type | tuple[type, ...]) -> Callable[[Any], None]:\n    \"\"\"\n\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):","sourceCodeStart":842,"sourceCodeEnd":878,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/_config/config.py#L842-L878","documentation":"Raised by the validator returned from `is_type_factory(_type)` (the `inner` closure at pandas/_config/config.py:860). It performs a STRICT type check: `type(x) != _type` (exact type equality, not `isinstance`). It is attached to options whose validator is an `is_type_factory(...)` and runs whenever `set_option`/`option_context`/DictWrapper assignment applies a new value to such an option.","triggerScenarios":"`set_option(key, value)` where `key`'s validator is `is_type_factory(bool)` and you pass an int/string; or passing a subclass where the exact type differs (because the check is not isinstance-based).","commonSituations":"Passing `1`/`0` instead of `True`/`False` for a bool option; passing a numpy scalar where a Python `int`/`bool` is required; passing a `str` where an `int` is required; assuming isinstance-style leniency when the validator is strict.","solutions":["Pass a value whose exact type matches the registered `_type` (e.g. real `bool`, not truthy ints).","Inspect the option's validator: `pd._config.config._get_registered_option(key).validator` to learn the required type.","Convert explicitly: `bool(x)`, `int(x)`, `str(x)` before setting."],"exampleFix":"# before\npd.set_option('display.unicode.east_asian_width', 1)   # validator is is_type_factory(bool)\n# ValueError: Value must have type '<class 'bool'>'\n\n# after\npd.set_option('display.unicode.east_asian_width', True)","handlingStrategy":"type-guard","validationCode":"import pandas as pd\nfrom pandas._config.config import _get_registered_option\ndef coerce_for_option(key: str, value):\n    opt = _get_registered_option(key)\n    validator = getattr(opt, 'validator', None) if opt else None\n    # Best-effort: ask the validator; if it raises, coerce common cases\n    if validator is not None:\n        try:\n            validator(value)\n        except (ValueError, TypeError):\n            # naive coercion hints for strict-type validators\n            for caster in (bool, int, float, str):\n                try:\n                    validator(caster(value))\n                    return caster(value)\n                except (ValueError, TypeError):\n                    continue\n            raise\n    return value\npd.set_option('display.unicode.east_asian_width', coerce_for_option('display.unicode.east_asian_width', 1))","typeGuard":"def matches_option_type(key: str, value) -> bool:\n    import pandas._config.config as c\n    opt = c._get_registered_option(key)\n    if not opt or not opt.validator:\n        return True\n    try:\n        opt.validator(value)\n        return True\n    except Exception:\n        return False","tryCatchPattern":"try:\n    pd.set_option(key, value)\nexcept ValueError as e:\n    if 'Value must have type' in str(e):\n        # coerce to the exact required type and retry\n        ...\n    raise","preventionTips":["Inspect the option validator with `_get_registered_option(key).validator` to learn the exact required type.","Remember validators are strict (`type(x) == _type`), not isinstance-based — pass real bool/int/str.","Normalize config-file values with explicit constructors (`bool(...)`, `int(...)`) before set_option.","For bool options pass `True`/`False`, never truthy ints like `1`."],"tags":["config","validator","type-check","set-option"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}