From 7cf2fb8feadbdaea63f25b0e4551a6c9693383e7 Mon Sep 17 00:00:00 2001 From: Jacob Ogden Date: Thu, 24 Jul 2025 12:00:20 -0400 Subject: [PATCH] Adjusted TaskID to not need the use of functools.partial --- CHANGELOG.md | 1 + CONTRIBUTORS.md | 1 + rich/progress.py | 5 ++--- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f24a4ea..04cbe9ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added `TTY_INTERACTIVE` environment variable to force interactive mode off or on https://github.com/Textualize/rich/pull/3777 +- Added Context Manager support for TaskID objects returned by Progress.add_task. Allowing for `with progress.add_task(...) as taskid: ...` which automatically removes the progress bar for that task upon exiting the current context. ## [14.0.0] - 2025-03-30 diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4b04786b..3b5cfb10 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -94,3 +94,4 @@ The following people have contributed to the development of Rich: - [Jonathan Helmus](https://github.com/jjhelmus) - [Brandon Capener](https://github.com/bcapener) - [Alex Zheng](https://github.com/alexzheng111) +- [Jacob Ogden](https://github.com/AetherBreaker) diff --git a/rich/progress.py b/rich/progress.py index d2e9e613..e30d6f93 100644 --- a/rich/progress.py +++ b/rich/progress.py @@ -7,7 +7,6 @@ from abc import ABC, abstractmethod from collections import deque from dataclasses import dataclass, field from datetime import timedelta -from functools import partial from io import RawIOBase, UnsupportedOperation from math import ceil from mmap import mmap @@ -64,7 +63,7 @@ class TaskID(int): return super().__new__(cls, task_id) def __init__(self, task_id: int, prog_instance: Progress | type[Progress]): - self.remove = partial(prog_instance.remove_task, self) + self.prog = prog_instance def __enter__(self) -> Self: return self @@ -75,7 +74,7 @@ class TaskID(int): exc_value: BaseException | None, traceback: TracebackType | None, ): - self.remove() + self.prog.remove_task(self) class _TrackThread(Thread):