{"record":{"id":"7efa96d5a2289f1c","repo":"RustPython/RustPython","slug":"binary-mode-doesn-t-take-an-encoding-argument","errorCode":null,"errorMessage":"binary mode doesn't take an encoding argument","messagePattern":"binary mode doesn't take an encoding argument","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":223,"sourceCode":"        raise TypeError(\"invalid errors: %r\" % errors)\n    modes = set(mode)\n    if modes - set(\"axrwb+t\") or len(mode) > len(modes):\n        raise ValueError(\"invalid mode: %r\" % mode)\n    creating = \"x\" in modes\n    reading = \"r\" in modes\n    writing = \"w\" in modes\n    appending = \"a\" in modes\n    updating = \"+\" in modes\n    text = \"t\" in modes\n    binary = \"b\" in modes\n    if text and binary:\n        raise ValueError(\"can't have text and binary mode at once\")\n    if creating + reading + writing + appending > 1:\n        raise ValueError(\"can't have read/write/append mode at once\")\n    if not (creating or reading or writing or appending):\n        raise ValueError(\"must have exactly one of read/write/append mode\")\n    if binary and encoding is not None:\n        raise ValueError(\"binary mode doesn't take an encoding argument\")\n    if binary and errors is not None:\n        raise ValueError(\"binary mode doesn't take an errors argument\")\n    if binary and newline is not None:\n        raise ValueError(\"binary mode doesn't take a newline argument\")\n    if binary and buffering == 1:\n        import warnings\n        warnings.warn(\"line buffering (buffering=1) isn't supported in binary \"\n                      \"mode, the default buffer size will be used\",\n                      RuntimeWarning, 2)\n    raw = FileIO(file,\n                 (creating and \"x\" or \"\") +\n                 (reading and \"r\" or \"\") +\n                 (writing and \"w\" or \"\") +\n                 (appending and \"a\" or \"\") +\n                 (updating and \"+\" or \"\"),\n                 closefd, opener=opener)\n    result = raw\n    try:","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_pyio.py#L205-L241","documentation":"ValueError raised when encoding is passed together with a binary mode (a 'b' in the mode string). Binary streams move raw bytes and never decode, so an encoding is meaningless there; encoding, errors, and newline are text-only arguments. The check fires before the file is opened, so nothing is created or truncated as a side effect.","triggerScenarios":"open(path, 'rb', encoding='utf-8') - usually copy-paste from a text-mode open; a generic wrapper that always forwards **kwargs (including encoding) into open(); one config dict shared by both text and binary opens.","commonSituations":"Refactoring text I/O to binary formats (e.g. switching to pickle/struct) while leaving encoding in the call; helper functions like read_bytes(path, encoding=...); a global encoding setting from YAML applied to every open() call.","solutions":["Drop encoding (and errors/newline) from binary opens: open(path, 'rb')","If decoded text is what you want, use text mode: open(path, 'r', encoding='utf-8')","In wrappers, gate kwargs on the flavor: kwargs = {} if 'b' in mode else {'encoding': enc}"],"exampleFix":"# before\nwith open(path, 'rb', encoding='utf-8') as f:\n    data = f.read()\n\n# after\nwith open(path, 'rb') as f:\n    data = f.read()                     # bytes\n# or, if text was intended:\nwith open(path, 'r', encoding='utf-8') as f:\n    text = f.read()","handlingStrategy":"validation","validationCode":"def open_any(path, mode='r', encoding=None):\n    kwargs = {} if 'b' in mode else ({'encoding': encoding} if encoding else {})\n    return open(path, mode, **kwargs)","typeGuard":"def takes_text_kwargs(mode) -> bool:\n    return 'b' not in mode","tryCatchPattern":null,"preventionTips":["Keep binary opens bare: open(path, 'rb')","Decide once whether a code path is text or binary; do not share kwargs","When migrating text code to binary, grep the call site for encoding/errors/newline"],"tags":["io","open","valueerror","encoding","binary-io","argument-conflict"],"backgroundTag":"incompatible-open-arguments","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}