-
Notifications
You must be signed in to change notification settings - Fork 982
Fix custom tqdm_class silently broken in non-TTY environments
#4056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hanouticelina
wants to merge
4
commits into
main
Choose a base branch
from
fix/tqdm-class-non-tty
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+88
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -279,6 +279,32 @@ def _inner_read(size: int | None = -1) -> bytes: | |
| pbar.close() | ||
|
|
||
|
|
||
| def _create_progress_bar(*, cls: type[old_tqdm], log_level: int, name: str | None = None, **kwargs) -> old_tqdm: | ||
| """Create a progress bar. | ||
|
|
||
| For our `tqdm` subclass (or subclasses of it): respects all disable signals | ||
| (`HF_HUB_DISABLE_PROGRESS_BARS`, `disable_progress_bars()`, log level) and uses | ||
| `disable=None` for TTY auto-detection (see https://github.com/huggingface/huggingface_hub/pull/2000), | ||
| unless `TQDM_POSITION=-1` forces bars on (https://github.com/huggingface/huggingface_hub/pull/2698). | ||
|
|
||
| For other classes: does not inject `disable` or `name`. the custom class is fully | ||
| responsible for its own behavior. Vanilla tqdm defaults to `disable=False` (bar shows). | ||
| Omits `name` which vanilla tqdm rejects with `TqdmKeyError`. See https://github.com/huggingface/huggingface_hub/issues/4050. | ||
| """ | ||
| # issubclass() crashes on non-class callables (e.g. functools.partial), guard with isinstance. | ||
| if not (isinstance(cls, type) and issubclass(cls, tqdm)): | ||
| return cls(**kwargs) # type: ignore[return-value] | ||
|
|
||
| # HF subclass: apply all disable signals + TTY auto-detection. | ||
| if are_progress_bars_disabled(name) or log_level == logging.NOTSET: | ||
| disable: bool | None = True | ||
| elif os.getenv("TQDM_POSITION") == "-1": | ||
| disable = False | ||
| else: | ||
| disable = None | ||
| return cls(disable=disable, name=name, **kwargs) # type: ignore[return-value] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _create_progress_bar should wrap instantiation in try/except (OSError, ValueError), log a warning, and fall back to a no-op. A progress bar failing to init shouldn't crash the download. See #4056 (comment) |
||
|
|
||
|
|
||
| def _get_progress_bar_context( | ||
| *, | ||
| desc: str, | ||
|
|
@@ -297,12 +323,13 @@ def _get_progress_bar_context( | |
| # Makes it easier to use the same code path for both cases but in the later | ||
| # case, the progress bar is not closed when exiting the context manager. | ||
|
|
||
| return (tqdm_class or tqdm)( # type: ignore | ||
| return _create_progress_bar( # type: ignore | ||
| cls=tqdm_class or tqdm, | ||
| log_level=log_level, | ||
| name=name, | ||
| unit=unit, | ||
| unit_scale=unit_scale, | ||
| total=total, | ||
| initial=initial, | ||
| desc=desc, | ||
| disable=is_tqdm_disabled(log_level=log_level), | ||
| name=name, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added this private helper to centralize how we initialize the tqdm progress bar.
is_tqdm_disabledis no longer needed internally but kept it since it's exposed inhuggingface_hub.utilsmodule