{"record":{"id":"04d8eb8118159311","repo":"python/cpython","slug":"invalid-buffering-r","errorCode":null,"errorMessage":"invalid buffering: %r","messagePattern":"invalid buffering: %r","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":201,"sourceCode":"    'r', 'wt', 'rt', etc.), it returns a TextIOWrapper. When used to open\n    a file in a binary mode, the returned class varies: in read binary\n    mode, it returns a BufferedReader; in write binary and append binary\n    modes, it returns a BufferedWriter, and in read/write mode, it returns\n    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\")","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pyio.py#L183-L219","documentation":"Raised by io.open() when the buffering argument is not an int. buffering selects the buffer strategy (-1 default, 0 unbuffered, 1 line-buffered, >1 buffer size), and the validator only accepts ints — bool passes (it is an int subclass), but None, floats, and strings do not.","triggerScenarios":"open('f', 'rb', buffering=None), buffering=8192.0 (float), buffering='8192', or buffering read from JSON/config as a string or float. Note open() defaults buffering=-1, so this only fires when the argument is passed explicitly.","commonSituations":"Config files or JSON supplying buffering as a string number; a size computed with float arithmetic (e.g. 1024*0.5); passing None intending 'default' — the caller should simply omit the argument for the default.","solutions":["Omit the buffering argument entirely when you want the default (-1).","Coerce explicit sizes to int: buffering=int(size).","Validate config-supplied values: accept only ints before calling open()."],"exampleFix":"// before\nbuf = config['buffer_size']        # e.g. \"8192\" from JSON\nopen('f.bin', 'rb', buffering=buf)  # TypeError: invalid buffering: '8192'\n\n// after\nbuf = int(config['buffer_size'])\nopen('f.bin', 'rb', buffering=buf)","handlingStrategy":"validation","validationCode":"if buffering is not None:\n    if not isinstance(buffering, int) or isinstance(buffering, bool) and buffering is False:\n        pass\n    buffering = int(buffering) if not isinstance(buffering, int) else buffering\n    assert buffering >= 0, 'buffering must be >= 0'\nopen(path, buffering=buffering if buffering is not None else -1)","typeGuard":"def valid_buffering(b) -> bool:\n    return b is None or (isinstance(b, int) and not isinstance(b, bool) and b >= -1)","tryCatchPattern":null,"preventionTips":["Omit buffering to get the default instead of passing None.","Coerce config/JSON-sourced sizes with int() at load time.","Keep sizes as ints in arithmetic (avoid float multiplies like 1024 * 0.5)."],"tags":["io","open","buffering","typeerror"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}