{"record":{"id":"a9d75cf1ab620b8f","repo":"apache/beam","slug":"cannot-set-position-to-d-since-it-s-larger-than-size-of-data","errorCode":null,"errorMessage":"Cannot set position to %d since it's larger than size of data %d.","messagePattern":"Cannot set position to (.+?) since it's larger than size of data (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/textio.py","lineNumber":98,"sourceCode":"\n    @property\n    def data(self):\n      return self._data\n\n    @data.setter\n    def data(self, value):\n      assert isinstance(value, bytes)\n      self._data = value\n\n    @property\n    def position(self):\n      return self._position\n\n    @position.setter\n    def position(self, value):\n      assert isinstance(value, int)\n      if value > len(self._data):\n        raise ValueError(\n            'Cannot set position to %d since it\\'s larger than '\n            'size of data %d.' % (value, len(self._data)))\n      self._position = value\n\n    def reset(self):\n      self.data = b''\n      self.position = 0\n\n  def __init__(\n      self,\n      file_pattern,\n      min_bundle_size,\n      compression_type,\n      strip_trailing_newlines,\n      coder: coders.Coder,\n      buffer_size=DEFAULT_READ_BUFFER_SIZE,\n      validate=True,\n      skip_header_lines=0,","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/textio.py#L80-L116","documentation":"The in-memory _Position class used by textio sources asserts the position set is within the length of the in-memory data buffer, raising this ValueError otherwise. It protects against readers advancing past the end of the backing byte data.","triggerScenarios":"Setting .position on the position object to a value greater than len(self._data); typically inside custom ReadProgress/skip logic or tests that manipulate the position directly, e.g. after the buffer was replaced with shorter data.","commonSituations":"Custom text source subclass computing offsets from the original (uncompressed/larger) data against a truncated buffer; tests reusing a position object after resetting data to b''; off-by-one when setting position to len(data)+1 to signal EOF.","solutions":["Clamp the new position to len(self._data) before assigning: value = min(value, len(data)).","Use position == len(data) (not larger) to represent end-of-buffer.","Refresh self._data before setting position if the buffer was reset or replaced.","Set position via the source's own advance logic (read/skip) instead of writing .position directly."],"exampleFix":"// before\npos.position = len(new_data) + 1\n// after\npos.position = min(len(new_data), requested_position)","handlingStrategy":"validation","validationCode":"def safe_set_position(pos, value, data):\n    assert isinstance(value, int)\n    pos.position = min(value, len(data))","typeGuard":"def is_valid_position(value, data):\n    return isinstance(value, int) and 0 <= value <= len(data)","tryCatchPattern":"try:\n    pos.position = value\nexcept (ValueError, AssertionError) as e:\n    logging.error('position out of range: %s', e)\n    pos.position = len(data)","preventionTips":["Clamp positions to len(data) before assignment","Represent EOF with position == len(data), never len(data)+1","Re-sync position objects after resetting/replacing the data buffer"],"tags":["python","apache-beam","textio","position","value-out-of-range"],"backgroundTag":"value-out-of-range","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}