{"record":{"id":"89265803390e46ce","repo":"python/cpython","slug":"an-integer-is-required","errorCode":null,"errorMessage":"an integer is required","messagePattern":"an integer is required","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pyio.py","lineNumber":1796,"sourceCode":"        try:\n            return os.write(self._fd, b)\n        except BlockingIOError:\n            return None\n\n    def seek(self, pos, whence=SEEK_SET):\n        \"\"\"Move to new file position.\n\n        Argument offset is a byte count.  Optional argument whence defaults\n        to SEEK_SET or 0 (offset from start of file, offset should be >= 0);\n        other values are SEEK_CUR or 1 (move relative to current position,\n        positive or negative), and SEEK_END or 2 (move relative to end of\n        file, usually negative, although many platforms allow seeking beyond\n        the end of a file).\n\n        Note that not all file objects are seekable.\n        \"\"\"\n        if isinstance(pos, float):\n            raise TypeError('an integer is required')\n        self._checkClosed()\n        return os.lseek(self._fd, pos, whence)\n\n    def tell(self):\n        \"\"\"tell() -> int.  Current file position.\n\n        Can raise OSError for non seekable files.\"\"\"\n        self._checkClosed()\n        return os.lseek(self._fd, 0, SEEK_CUR)\n\n    def truncate(self, size=None):\n        \"\"\"Truncate the file to at most size bytes.\n\n        Size defaults to the current file position, as returned by tell().\n        The current file position is changed to the value of size.\n        \"\"\"\n        self._checkClosed()\n        self._checkWritable()","sourceCodeStart":1778,"sourceCodeEnd":1814,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pyio.py#L1778-L1814","documentation":"Raised by FileIO.seek when the pos argument is a float ('an integer is required'). File positions must be integers; a float offset is rejected with TypeError before the closed check and the os.lseek call, mirroring the integer-fd rule in the constructor.","triggerScenarios":"f.seek(1.5), f.seek(n / 2), or f.seek(numpy.float64(x)) on a FileIO (e.g. buffering=0); offsets computed with true division or read from JSON floats.","commonSituations":"Offsets produced by division, statistics, or pandas/numpy arithmetic fed into seek; JSON config where the byte offset arrived as 1024.0; porting code where another API accepted float offsets.","solutions":["Convert with int() after ensuring the value is integral: f.seek(int(pos))","Use floor division // where the offset math may produce floats","Round deliberately if truncation is intended and document it: f.seek(int(round(pos)))"],"exampleFix":"# before\nhalf = total_len / 2\nf.seek(half)          # TypeError: an integer is required (FileIO.seek)\n\n# after\nhalf = total_len // 2\nf.seek(half)","handlingStrategy":"validation","validationCode":"import math\nif not isinstance(pos, int):\n    if isinstance(pos, float) and pos.is_integer():\n        pos = int(pos)\n    else:\n        raise TypeError('seek offset must be an integer')\nf.seek(pos, whence)","typeGuard":"def is_int_offset(p):\n    return isinstance(p, int) and not isinstance(p, bool)","tryCatchPattern":"try:\n    f.seek(pos)\nexcept TypeError as e:\n    if 'integer' in str(e) and isinstance(pos, float) and pos.is_integer():\n        f.seek(int(pos))\n    else:\n        raise","preventionTips":["Use // instead of / when computing byte offsets","Validate JSON-sourced offsets with isinstance(x, int)","Convert numpy scalars with int() before seeking"],"tags":["python","io","file-io","seek","typeerror"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}