{"id":"66d3a2973583dc81","repo":"pypa/pip","slug":"unexpected-type-type-item-name-expected-e","errorCode":null,"errorMessage":"Unexpected type {type(item).__name__} (expected {expected_item_type.__name__})","messagePattern":"Unexpected type (.+?) \\(expected (.+?)\\)","errorType":"validation","errorClass":"PylockValidationError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/packaging/pylock.py","lineNumber":132,"sourceCode":"        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.\"\"\"\n    if (value := _get(d, Sequence, key)) is None:  # type: ignore[type-abstract]\n        return None\n    if isinstance(value, (str, bytes)):\n        # special case: str and bytes are Sequences, but we want to reject it\n        raise PylockValidationError(\n            f\"Unexpected type {type(value).__name__} (expected Sequence)\",\n            context=key,\n        )\n    for i, item in enumerate(value):\n        if not isinstance(item, expected_item_type):\n            raise PylockValidationError(\n                f\"Unexpected type {type(item).__name__} \"\n                f\"(expected {expected_item_type.__name__})\",\n                context=f\"{key}[{i}]\",\n            )\n    return value\n\n\ndef _get_as(\n    d: Mapping[str, Any],\n    expected_type: type[_T],\n    target_type: Callable[[_T], _T2],\n    key: str,\n) -> _T2 | None:\n    \"\"\"Get a value from the dictionary, verify it's the expected type,\n    and convert to the target type.\n\n    This assumes the target_type constructor accepts the value.\n    \"\"\"","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/packaging/pylock.py#L114-L150","documentation":"Raised as PylockValidationError by _get_sequence in packaging.pylock when a sequence field's elements are not all of the expected item type. The context is set to '{key}[{i}]' so the exact offending index is reported. For example dependencies must be a list of Mapping, wheels a list of Mapping; a scalar element triggers this.","triggerScenarios":"dependencies = [\\\"foo\\\"] where the expected item type is Mapping (it should be a list of inline tables); wheels = [\\\"a.whl\\\", 42]; attestation-identities containing a string instead of a table.","commonSituations":"Authoring a pylock by hand and putting scalars where tables belong; converting from requirements.txt where each line is a string rather than a dependency object.","solutions":["Make each list element an inline table of the right shape, e.g. dependencies = [{ name = \\\"foo\\\", ... }].","Regenerate the lockfile with a compliant resolver so element shapes are correct.","Validate element schemas before calling from_dict.","Inspect e.context to find the failing key[index]."],"exampleFix":"# before\ndependencies = [\\\"requests\\\"]\n# after\ndependencies = [{ name = \\\"requests\\\", version = \\\"2.31.0\\\" }]","handlingStrategy":"type-guard","validationCode":"def all_items_match(seq, item_type) -> bool:\n    return all(isinstance(x, item_type) for x in seq)","typeGuard":"from typing import Any, Sequence as Seq\n\ndef is_homogeneous_sequence(value: Seq[Any], item_type) -> bool:\n    return all(isinstance(x, item_type) for x in value)","tryCatchPattern":"try:\n    PylockFile.from_dict(data)\nexcept PylockValidationError as e:\n    # e.context like 'wheels[2]' or 'dependencies[0]'\n    fix_element(e.context)","preventionTips":["Make each list element an inline table of the correct shape.","Validate element types before calling from_dict.","Use a resolver to generate well-formed entries.","Read e.context to find the failing index."],"tags":["pylock","pep771","toml","type-checking"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}