{"record":{"id":"92dbed3db2956bfe","repo":"TheAlgorithms/Python","slug":"a-bytes-like-object-is-required-not-data-clas","errorCode":null,"errorMessage":"a bytes-like object is required, not '{data.__class__.__name__}'","messagePattern":"a bytes-like object is required, not '(.+?)'","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"ciphers/base64_cipher.py","lineNumber":38,"sourceCode":"    >>> from base64 import b64encode\n    >>> a = b\"This pull request is part of Hacktoberfest20!\"\n    >>> b = b\"https://tools.ietf.org/html/rfc4648\"\n    >>> c = b\"A\"\n    >>> base64_encode(a) == b64encode(a)\n    True\n    >>> base64_encode(b) == b64encode(b)\n    True\n    >>> base64_encode(c) == b64encode(c)\n    True\n    >>> base64_encode(\"abc\")\n    Traceback (most recent call last):\n      ...\n    TypeError: a bytes-like object is required, not 'str'\n    \"\"\"\n    # Make sure the supplied data is a bytes-like object\n    if not isinstance(data, bytes):\n        msg = f\"a bytes-like object is required, not '{data.__class__.__name__}'\"\n        raise TypeError(msg)\n\n    binary_stream = \"\".join(bin(byte)[2:].zfill(8) for byte in data)\n\n    padding_needed = len(binary_stream) % 6 != 0\n\n    if padding_needed:\n        # The padding that will be added later\n        padding = b\"=\" * ((6 - len(binary_stream) % 6) // 2)\n\n        # Append binary_stream with arbitrary binary digits (0's by default) to make its\n        # length a multiple of 6.\n        binary_stream += \"0\" * (6 - len(binary_stream) % 6)\n    else:\n        padding = b\"\"\n\n    # Encode every 6 binary digits to their corresponding Base64 character\n    return (\n        \"\".join(","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/ciphers/base64_cipher.py#L20-L56","documentation":"Raised by base64_encode() in ciphers/base64_cipher.py when the data argument is not a bytes object. The function implements Base64 from scratch and only accepts raw bytes, mirroring the stdlib error text. Any str, int, or other type is rejected before encoding begins.","triggerScenarios":"Calling base64_encode(\"abc\") with a str; passing an int, bytearray is rejected too (isinstance(data, bytes) is strict, so even bytearray fails); passing output of a text API (e.g. json.dumps result) without encoding.","commonSituations":"Porting code from stdlib base64.b64encode which accepts bytes-like objects (bytearray, memoryview) and hitting this stricter bytes-only check; reading a file in text mode and passing its contents directly; interactive testing with string literals.","solutions":["Encode strings first: base64_encode(my_str.encode('utf-8'))","Convert bytes-like inputs: base64_encode(bytes(my_bytearray))","Wrap the call with a type check and raise a clearer error from your own code"],"exampleFix":"# before\nbase64_encode(\"hello\")\n\n# after\nbase64_encode(\"hello\".encode(\"utf-8\"))","handlingStrategy":"type-guard","validationCode":"data = data.encode(\"utf-8\") if isinstance(data, str) else data","typeGuard":"def as_bytes(data) -> bytes:\n    if isinstance(data, str):\n        return data.encode(\"utf-8\")\n    if isinstance(data, bytes):\n        return data\n    raise TypeError(f\"expected str or bytes, got {type(data).__name__}\")","tryCatchPattern":"try:\n    encoded = base64_encode(data)\nexcept TypeError as exc:\n    if \"bytes-like object\" in str(exc):\n        encoded = base64_encode(str(data).encode(\"utf-8\"))\n    else:\n        raise","preventionTips":["Always call .encode('utf-8') on strings before base64_encode","Note this function is stricter than stdlib: bytearray/memoryview are rejected too","Centralize bytes conversion in one helper at your API boundary"],"tags":["base64","type-error","encoding","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}