{"record":{"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":141,"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":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_vendor/packaging/pylock.py#L123-L159","documentation":"Raised by _get_sequence in pylock.py:139-145 when iterating a TOML array: each element is checked against the expected item type (e.g. Mapping for dependencies, str for extras/dependency-groups). A wrong-typed element raises PylockValidationError with context 'key[i]'.","triggerScenarios":"In pylock TOML: dependencies = [{name = \"x\"}, \"y\"] (a string element among table elements), dependency-groups = [\"dev\", 42] (int element where str expected), attestation-identities = [{kind=\"...\"}, \"bad\"].","commonSituations":"A mixed-type array; a stray scalar accidentally placed inside an array of tables; copy-paste leaving a placeholder string in a list.","solutions":["Make every element of the array the spec-required type (tables where Mapping is expected, strings where str is expected)."],"exampleFix":"# before\n[[packages]]\ndependencies = [{ name = \"x\" }, \"y\"]\n# after\n[[packages]]\ndependencies = [{ name = \"x\" }, { name = \"y\" }]","handlingStrategy":"validation","validationCode":"from collections.abc import Mapping, Sequence\n\ndef array_items_typed(value, item_type: type) -> bool:\n    return isinstance(value, Sequence) and all(isinstance(i, item_type) for i in value)\n","typeGuard":null,"tryCatchPattern":"from packaging.pylock import Pylock, PylockValidationError\n\ntry:\n    Pylock.from_dict(d)\nexcept PylockValidationError as e:\n    # e.context is 'key[i]'\n    ...\n","preventionTips":["Keep pylock arrays homogeneous (all tables or all strings as the spec requires).","Validate item types at the TOML-loading boundary."],"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"}