{"record":{"id":"fc3411a4014f02e6","repo":"python/cpython","slug":"invalid-errors-r","errorCode":null,"errorMessage":"invalid errors: %r","messagePattern":"invalid errors: %r","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":205,"sourceCode":"    a BufferedRandom.\n\n    It is also possible to use a string or bytearray as a file for both\n    reading and writing. For strings StringIO can be used like a file\n    opened in a text mode, and for bytes a BytesIO can be used like a file\n    opened in a binary mode.\n    \"\"\"\n    if not isinstance(file, int):\n        file = os.fspath(file)\n    if not isinstance(file, (str, bytes, int)):\n        raise TypeError(\"invalid file: %r\" % file)\n    if not isinstance(mode, str):\n        raise TypeError(\"invalid mode: %r\" % mode)\n    if not isinstance(buffering, int):\n        raise TypeError(\"invalid buffering: %r\" % buffering)\n    if encoding is not None and not isinstance(encoding, str):\n        raise TypeError(\"invalid encoding: %r\" % encoding)\n    if errors is not None and not isinstance(errors, str):\n        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\")","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pyio.py#L187-L223","documentation":"Raised by io.open() when the errors argument is supplied and is neither None nor a str. errors names the error handler for text decoding/encoding ('strict', 'replace', 'ignore', 'backslashreplace', ...); the validator rejects any non-string value with this TypeError before the stream is created.","triggerScenarios":"open('f', 'r', errors=None) is fine, but errors=0, errors=['ignore'], or a typo'd constant (e.g. errors=IGNORE vs the string 'ignore') raises it. Also errors=sys.stderr (confusing the stream with its error attribute) or errors='ignore '.strip miswired.","commonSituations":"Passing module constants instead of their string names; wiring the argument from a settings object whose attribute is an enum or bool instead of str; conditional expressions like errors=strict_mode and 'strict' that evaluate to False.","solutions":["Pass one of the documented handler names as a str ('strict', 'ignore', 'replace', 'backslashreplace', 'surrogateescape', 'xmlcharrefreplace', 'namereplace') or None.","If the value is an enum/constant object, pass its string value (e.g. member.value).","Fix conditional expressions: errors='strict' if strict_mode else 'replace'."],"exampleFix":"// before\nopen('f', encoding='utf-8', errors=ErrorHandlers.IGNORE)  # enum object -> TypeError\n\n// after\nopen('f', encoding='utf-8', errors='ignore')            # str handler name","handlingStrategy":"type-guard","validationCode":"HANDLERS = {'strict','ignore','replace','backslashreplace','surrogateescape','xmlcharrefreplace','namereplace'}\nif errors is not None:\n    assert isinstance(errors, str) and errors in HANDLERS, f'bad errors handler: {errors!r}'\nopen(path, errors=errors)","typeGuard":"def valid_errors_handler(e) -> bool:\n    return e is None or (isinstance(e, str) and e in {\n        'strict','ignore','replace','backslashreplace',\n        'surrogateescape','xmlcharrefreplace','namereplace'})","tryCatchPattern":null,"preventionTips":["Store error-handler names as strings in settings, not enum objects or booleans.","If using an enum, pass member.value to open().","Write conditionals as 'strict' if strict else 'replace', not strict and 'strict'."],"tags":["io","open","errors","encoding","typeerror","text-io"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}