{"record":{"id":"a9d0f828ef91be38","repo":"pypa/pip","slug":"unexpected-type-type-value-name-expected-a9d0f8","errorCode":null,"errorMessage":"Unexpected type {type(value).__name__} (expected {expected_type.__name__})","messagePattern":"Unexpected type (.+?) \\(expected (.+?)\\)","errorType":"validation","errorClass":"PylockValidationError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/packaging/pylock.py","lineNumber":112,"sourceCode":"\n\ndef _toml_dict_factory(data: list[tuple[str, Any]]) -> dict[str, Any]:\n    return {\n        _toml_key(key): _toml_value(key, value)\n        for key, value in data\n        if value is not None\n    }\n\n\ndef _get(d: Mapping[str, Any], expected_type: type[_T], key: str) -> _T | None:\n    \"\"\"Get a value from the dictionary and verify it's the expected type.\"\"\"\n    if (value := d.get(key)) is None:\n        return None\n    if not isinstance(value, expected_type) or (\n        # Special case: bool is a subclass of int, but TOML distinguishes the two\n        expected_type is int and isinstance(value, bool)\n    ):\n        raise PylockValidationError(\n            f\"Unexpected type {type(value).__name__} \"\n            f\"(expected {expected_type.__name__})\",\n            context=key,\n        )\n    return value\n\n\ndef _get_required(d: Mapping[str, Any], expected_type: type[_T], key: str) -> _T:\n    \"\"\"Get a required value from the dictionary and verify it's the expected type.\"\"\"\n    if (value := _get(d, expected_type, key)) is None:\n        raise _PylockRequiredKeyError(key)\n    return value\n\n\ndef _get_sequence(\n    d: Mapping[str, Any], expected_item_type: type[_T], key: str\n) -> Sequence[_T] | None:\n    \"\"\"Get a list value from the dictionary and verify it's the expected items type.\"\"\"","sourceCodeStart":94,"sourceCodeEnd":130,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/packaging/pylock.py#L94-L130","documentation":"Raised by _get in pylock.py:108-116 while reading a TOML key for a pylock dataclass. It fires when a present value is not the expected type, and also when expected_type is int but the value is a bool (TOML keeps bool and int distinct, even though bool subclasses int in Python). Surfaces as PylockValidationError with context set to the offending key.","triggerScenarios":"In a pylock TOML: size = \"12345\" (str where int expected), size = true (bool where int expected), index = 5 (int where str expected), upload-time = \"2024-01-01 00:00:00\" (str where datetime expected).","commonSituations":"Hand-editing pylock.toml with quoted numbers; a TOML emitter that quotes everything as strings; using true/false where an integer size is required.","solutions":["Use the correct TOML scalar: unquoted integers, quoted strings, true/false booleans, native datetime.","Never pass a bool where an int is expected (e.g. size)."],"exampleFix":"# before\n[[packages.sdist]]\nsize = \"12345\"\n# after\n[[packages.sdist]]\nsize = 12345","handlingStrategy":"validation","validationCode":"def toml_scalar_ok(key: str, value, expected_type: type) -> bool:\n    if isinstance(value, expected_type) and not (expected_type is int and isinstance(value, bool)):\n        return True\n    return False\n","typeGuard":null,"tryCatchPattern":"from packaging.pylock import Pylock, PylockValidationError\n\ntry:\n    lock = Pylock.from_dict(toml_dict)\nexcept PylockValidationError as e:\n    # e.context names the offending key\n    ...\n","preventionTips":["Keep TOML scalars type-correct: unquoted ints, quoted strings, true/false booleans.","Never use a bool where an int (e.g. size) is expected."],"tags":["pylock","toml","validation","type"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}