{"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":103,"sourceCode":"    if isinstance(value, Sequence) and key == \"environments\":\n        return [str(v) for v in value]\n    return value\n\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):\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":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/packaging/pylock.py#L85-L121","documentation":"Raised as PylockValidationError by _get in packaging.pylock when a TOML value for a key exists but is not the expected scalar type. pylock (PEP 771 lockfile) strictly type-checks every field; for example 'size' must be int, 'name' must be str. The context field records the offending key path.","triggerScenarios":"Loading a pylock.toml where size = \\\"12345\\\" (string instead of int), or upload-time = 12345 (int instead of ISO str), or name = 42. Calling PylockFile.from_dict on a hand-built dict with wrong types.","commonSituations":"Hand-edited lockfiles; tooling that emits numbers as strings; schema drift between pylock versions; YAML->TOML converters that lose typing.","solutions":["Open the pylock.toml and correct the value to the expected type shown in the error.","Regenerate the lockfile with a compliant resolver rather than editing by hand.","Pre-validate the TOML against the pylock JSON schema before loading.","Catch PylockValidationError and report the offending key (e.context) to the user."],"exampleFix":"# before\nsize = \\\"1024\\\"\n# after\nsize = 1024","handlingStrategy":"type-guard","validationCode":"def check_types(d, spec):\n    # spec: {key: expected_type}\n    for k, t in spec.items():\n        v = d.get(k)\n        if v is not None and not isinstance(v, t):\n            raise TypeError(f'{k}: expected {t.__name__}, got {type(v).__name__}')","typeGuard":"def is_correct_type(value, expected_type) -> bool:\n    return value is None or isinstance(value, expected_type)","tryCatchPattern":"from packaging.pylock import PylockValidationError\ntry:\n    PylockFile.from_dict(data)\nexcept PylockValidationError as e:\n    log.error('pylock field %s: %s', e.context, e.message)","preventionTips":["Generate lockfiles with a compliant resolver.","Validate TOML field types before loading.","Catch PylockValidationError and report e.context.","Avoid hand-editing scalar fields."],"tags":["pylock","pep771","toml","type-checking"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}