{"record":{"id":"b3019b914b23ad83","repo":"cocoindex-io/cocoindex","slug":"expected-tp-name-loc-got-type-value-na","errorCode":null,"errorMessage":"expected {tp.__name__}{loc}, got {type(value).__name__}: {value!r}","messagePattern":"expected (.+?)(.+?), got (.+?): (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/_internal/datatype.py","lineNumber":387,"sourceCode":"                    fn(elem, f\"{path}[{i}]\")\n\n            return check_fixed_tuple\n\n        # Bare tuple (no args) — just check isinstance\n        def check_bare_tuple(value: Any, path: str) -> None:\n            if not isinstance(value, tuple):\n                loc = f\" at {path}\" if path else \"\"\n                raise TypeError(f\"expected tuple{loc}, got {type(value).__name__}\")\n\n        return check_bare_tuple\n\n    # Simple concrete type: str, int, bytes, uuid.UUID, etc.\n    if isinstance(tp, type):\n\n        def check_isinstance(value: Any, path: str) -> None:\n            if not isinstance(value, tp):\n                loc = f\" at {path}\" if path else \"\"\n                raise TypeError(\n                    f\"expected {tp.__name__}{loc}, got {type(value).__name__}: {value!r}\"\n                )\n\n        return check_isinstance\n\n    raise ValueError(f\"Unsupported type for TypeChecker: {tp}\")\n\n\nT = TypeVar(\"T\")\n\n\nclass TypeChecker(Generic[T]):\n    \"\"\"\n    Pre-built runtime type checker.\n\n    Analyzes a type annotation once at construction time and builds optimized\n    validation closures.  At check time, validation is a fast series of\n    ``isinstance`` calls and tuple-length comparisons — no reflection.","sourceCodeStart":369,"sourceCodeEnd":405,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/_internal/datatype.py#L369-L405","documentation":"For simple concrete types (str, int, bytes, uuid.UUID, etc.), the TypeChecker builds check_isinstance, which raises this TypeError when the value is not an instance of the declared class. Unlike the tuple errors, the message includes the offending value itself (repr) to ease debugging. This is the library's leaf-level type enforcement for scalar fields.","triggerScenarios":"Passing a value of the wrong concrete type where a scalar type is declared — e.g. a str where int is declared, bytes where str is declared, or an int where uuid.UUID is declared — in @coco.fn arguments, target-state fields, or TypeChecker.check().","commonSituations":"Passing numeric strings from config/CLI/env into int fields; passing raw UUID strings where uuid.UUID is declared; NumPy scalar types (np.int64) where Python int is declared and isinstance fails; bool where int is expected or vice versa.","solutions":["Convert the value before passing: int(s), uuid.UUID(s), str(b, 'utf-8')","If it is a NumPy/torch scalar, convert with .item() so it becomes a builtin type","Correct the declared type annotation if the wrong type was declared"],"exampleFix":"// before\ncheck(doc_id)  # doc_id = \"550e8400-e29b-41d4-...\", declared uuid.UUID\n// after\nimport uuid\ndoc_id = uuid.UUID(\"550e8400-e29b-41d4-a716-446655440000\")\ncheck(doc_id)","handlingStrategy":"validation","validationCode":"if not isinstance(value, tp):\n    raise TypeError(f\"{name!r} must be {tp.__name__}, got {type(value).__name__}: {value!r}\")","typeGuard":"def is_instance_of(value: object, tp: type) -> TypeGuard[Any]:\n    return isinstance(value, tp)","tryCatchPattern":"try:\n    check(value)\nexcept TypeError as e:\n    log.error(\"field type mismatch: %s\", e)\n    raise","preventionTips":["Coerce weak inputs (strings from config/CLI/env) to strong types at the earliest boundary","Call .item() on NumPy/torch scalars before passing to builtin-typed fields","Parse UUIDs and datetimes into uuid.UUID / datetime objects instead of raw strings","Run mypy over pipeline code so scalar type mismatches are caught statically"],"tags":["python","type-checking","isinstance","validation"],"backgroundTag":"type-mismatch","analyzedSha":"e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b","analyzedAt":"2026-09-08T15:59:19.997Z","contentChangedAt":"2026-09-08T15:59:19.997Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}