{"record":{"id":"e97f787ce7c207cc","repo":"Textualize/textual","slug":"can-t-encode-datum-r","errorCode":null,"errorMessage":"Can't encode {datum!r}","messagePattern":"Can't encode (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/textual/_binary_encode.py","lineNumber":163,"sourceCode":"        dict: encode_dict,\n    }\n\n    def encode(datum: object) -> bytes:\n        \"\"\"Recursively encode data.\n\n        Args:\n            datum: Data suitable for encoding.\n\n        Raises:\n            TypeError: If `datum` is not one of the supported types.\n\n        Returns:\n            Encoded data bytes.\n        \"\"\"\n        try:\n            decoder = ENCODERS[type(datum)]\n        except KeyError:\n            raise TypeError(\"Can't encode {datum!r}\") from None\n        return decoder(datum)\n\n    return encode(data)\n\n\ndef load(encoded: bytes) -> object:\n    \"\"\"Load an encoded data structure from bytes.\n\n    Args:\n        encoded: Encoded data in bytes.\n\n    Raises:\n        DecodeError: If an error was encountered decoding the string.\n\n    Returns:\n        Decoded data.\n    \"\"\"\n    if not isinstance(encoded, bytes):","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/_binary_encode.py#L145-L181","documentation":"Textual's internal binary encoder (used for the devtools/serving protocol) only supports a fixed set of types (int, float, str, bytes, bool, None, list, tuple, dict). encode() looks up the type in the ENCODERS registry and raises TypeError when the datum's type is not registered. Note: the message string lacks an f-prefix in this version, so it prints literally '{datum!r}'.","triggerScenarios":"Calling textual._binary_encode.dump/encode with an unsupported value such as a set, datetime, custom class, complex, or numpy scalar nested inside the payload.","commonSituations":"Serializing app state or snapshot payloads for the devtools connection that contain enums, sets, datetimes, or dataclasses; private-API users dumping arbitrary Python objects.","solutions":["Convert unsupported values to supported primitives before encoding (str(), list(), dict(...))","Replace sets with lists or tuples, datetimes with ISO strings, dataclasses with dicts","Restrict payloads to JSON-like types (int, float, str, bytes, bool, None, list, tuple, dict)","Avoid the private _binary_encode module in user code; use JSON or pickle explicitly"],"exampleFix":"# before\nencode({\"tags\": {\"a\", \"b\"}})  # TypeError: Can't encode\n\n# after\nencode({\"tags\": [\"a\", \"b\"]})","handlingStrategy":"validation","validationCode":"ALLOWED = (int, float, str, bytes, bool, type(None), list, tuple, dict)\ndef sanitize(data):\n    if isinstance(data, dict):\n        return {k: sanitize(v) for k, v in data.items()}\n    if isinstance(data, (list, tuple)):\n        return [sanitize(v) for v in data]\n    if isinstance(data, ALLOWED):\n        return data\n    return str(data)","typeGuard":"def is_encodable(datum: object) -> bool:\n    return isinstance(datum, (int, float, str, bytes, bool, type(None), list, tuple, dict))","tryCatchPattern":"try:\n    encode(data)\nexcept TypeError:\n    encode(sanitize(data))","preventionTips":["Keep payloads JSON-like; convert sets/datetimes/dataclasses first","Don't rely on the private _binary_encode module for arbitrary objects","Unit-test serialization of every payload shape you add"],"tags":["python","textual","serialization","type-mismatch","internal-api"],"backgroundTag":"unsupported-serialization-type","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}