{"record":{"id":"d00f4acb13754087","repo":"Textualize/rich","slug":"write","errorCode":null,"errorMessage":"write","messagePattern":"write","errorType":"exception","errorClass":"UnsupportedOperation","httpStatus":null,"severity":"error","filePath":"rich/progress.py","lineNumber":279,"sourceCode":"        lines = self.handle.readlines(hint)\n        self.progress.advance(self.task, advance=sum(map(len, lines)))\n        return lines\n\n    def close(self) -> None:\n        if self.close_handle:\n            self.handle.close()\n        self._closed = True\n\n    def seek(self, offset: int, whence: int = 0) -> int:\n        pos = self.handle.seek(offset, whence)\n        self.progress.update(self.task, completed=pos)\n        return pos\n\n    def tell(self) -> int:\n        return self.handle.tell()\n\n    def write(self, s: Any) -> int:\n        raise UnsupportedOperation(\"write\")\n\n    def writelines(self, lines: Iterable[Any]) -> None:\n        raise UnsupportedOperation(\"writelines\")\n\n\nclass _ReadContext(ContextManager[_I], Generic[_I]):\n    \"\"\"A utility class to handle a context for both a reader and a progress.\"\"\"\n\n    def __init__(self, progress: \"Progress\", reader: _I) -> None:\n        self.progress = progress\n        self.reader: _I = reader\n\n    def __enter__(self) -> _I:\n        self.progress.start()\n        return self.reader.__enter__()\n\n    def __exit__(\n        self,","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/Textualize/rich/blob/9d8f9a372cc5916fd4781fec207ced7ddac2f08f/rich/progress.py#L261-L297","documentation":"rich's Progress.wrap_file() returns a _Reader: a read-only progress-tracking wrapper around the underlying file handle. Calling .write() on it raises io.UnsupportedOperation('write') because the wrapper only implements the read side of the IO interface (read/tell/seek) to update task progress; write is explicitly stubbed out.","triggerScenarios":"Calling progress.write(data) on the object returned by Progress.wrap_file(file) or Progress.open(file) (which delegates to wrap_file). E.g. with progress.wrap_file(f) as p: p.write(b'x'). Also triggered by any library that type-checks against IO and then writes, e.g. shutil.copyfileobj(dst=p) or requests upload plumbing that writes to the passed file object.","commonSituations":"Developer opens a file with Progress.open(..., mode='rb') for an upload and then accidentally writes to the wrapper instead of the raw handle; passing the wrapper as a destination to a copy function; treating wrap_file's result as a general-purpose file object.","solutions":["Write to the original raw handle, not the wrapper: keep a reference to the file you passed into wrap_file and write there.","If you need write progress, track it manually: call progress.update(task_id, completed=f.tell()) after writes instead of relying on wrap_file.","Check mode/usage: wrap_file is designed for downloads/reads (e.g. httpx response.iter_bytes into the reader); restructure so only reads go through the wrapper."],"exampleFix":"# before\nwith progress.wrap_file(f, total=size) as wrapped:\n    wrapped.write(b'data')  # UnsupportedOperation\n\n# after\nwith progress.wrap_file(f, total=size) as wrapped:\n    data = wrapped.read()  # read-only wrapper\n# write to the raw handle if needed\nf.write(b'data')\nprogress.update(task, completed=f.tell())","handlingStrategy":"type-guard","validationCode":"from rich.progress import Progress\n# _Reader is read-only: verify before writing\nis_readable = hasattr(obj, 'read') and not getattr(obj, 'closed', False)","typeGuard":"def is_readonly_progress_reader(obj: object) -> bool:\n    \"\"\"True for rich _Reader wrappers that reject write.\"\"\"\n    return hasattr(obj, 'read') and callable(getattr(obj, 'write', None)) and getattr(type(obj), '__name__', '') == '_Reader'","tryCatchPattern":null,"preventionTips":["Keep a reference to the raw handle when calling Progress.wrap_file; never write through the wrapper.","Only use wrap_file/Progress.open for read (download) progress; track write progress manually with progress.update().","Read the docstring: _Reader implements the io read side only."],"tags":["rich","io","progress","read-only","unsupported-operation"],"backgroundTag":null,"analyzedSha":"9d8f9a372cc5916fd4781fec207ced7ddac2f08f","analyzedAt":"2026-08-15T03:30:11.781Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}