Skip to content

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Jun 3, 2024

When downloading files in file_download.py, we use a progress bar. This progress bar can be either passed by the user or we create a classic tqdm bar (see update in #2143).

As @cbensimon noticed, the progress bar is not correctly closed when an error occurs or when hf_transfer is used. This PR fixes this by using a context manager. The progress bar is now always closed if created internally. When passed by the user, the progress bar is not closed as it is a the user's discretion to manage it.

Better to review with "hide whitespace" checked.

@Wauplin Wauplin requested review from cbensimon and LysandreJik June 3, 2024 09:15
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

"Consider upgrading to latest version to enable progress bars "
"using `pip install -U hf_transfer`."
)
with progress:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with progress:
with progress as progress:

Not 100% sure but I think that the value from nullcontext is only returned in __enter__ : https://docs.python.org/3/library/contextlib.html#contextlib.nullcontext

This shouldn't break the case where _tqdm_bar is None because __enter__ method of a tqdm.tqdm instance returns self

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, thanks for the hint. Corrected it in 63dfdaf

@Wauplin
Copy link
Contributor Author

Wauplin commented Jun 3, 2024

Thanks for the review!

@Wauplin Wauplin merged commit 919ce7d into main Jun 3, 2024
@Wauplin Wauplin deleted the fix-progress-bar-not-closed-when-downloading branch June 3, 2024 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants