{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/_config/config.py#L842-L878","documentation":"Raised by the validator produced by is_type_factory (config.py:844-862) when type(value) != _type (strict type equality, not isinstance). Validators like is_int/is_bool/is_float/is_str use this factory, so a value of a derived/subclass type (e.g. np.int64 vs int, or a bool subclass) will fail. The validator is invoked on the default at register time and on every set_option value.","triggerScenarios":"Registering an option with validator=is_int and default np.int64(5); or calling set_option on an int-validated option with True (bool, not int) or with a numpy integer.","commonSituations":"Passing numpy scalars (np.int64, np.float64) to an option validated with is_int/is_float — type(x) != int. Also passing True to an int-validated option, since type(True)==bool. Or a bool-validated option receiving 1/0.","solutions":["Coerce before set_option: set_option('x', int(value)) or bool(value).","Use is_instance_factory or a custom validator if numpy scalars must be accepted.","Register the default with the exact Python type that matches the validator (e.g. plain int 5, not np.int64(5))."],"exampleFix":"# before\nimport numpy as np\ncf.register_option('myapp.count', np.int64(5), validator=cf.is_int)  # ValueError: type 'int' expected\n\n# after\ncf.register_option('myapp.count', int(5), validator=cf.is_int)\npd.set_option('myapp.count', int(some_np_scalar))","handlingStrategy":"type-guard","validationCode":"def coerce_for_validator(value, validator_type):\n    # is_type_factory uses type(x) == _type; coerce exact Python type\n    if validator_type is int and not isinstance(value, int):\n        return int(value)\n    if validator_type is float and not isinstance(value, float):\n        return float(value)\n    if validator_type is bool and not isinstance(value, bool):\n        return bool(value)\n    return value","typeGuard":"def matches_strict_type(value, _type) -> bool:\n    return type(value) is _type","tryCatchPattern":"try:\n    pd.set_option(key, value)\nexcept ValueError as e:\n    if 'must have type' in str(e):\n        pd.set_option(key, type(expected)(value))","preventionTips":["Coerce numpy scalars to native Python types before set_option.","Pass True/False (not 1/0) to bool-validated options, and plain ints to int-validated ones.","If you need isinstance semantics, register with is_instance_factory instead of is_type_factory."],"tags":["config","validator","type-check","numpy-scalars"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}