Repository navigation
fix: close the json file handle in download_file_from_hf - #2058
Open
faisalhacks wants to merge 1 commit into
Open
faisalhacks wants to merge 1 commit into
faisalhacks wants to merge 1 commit into
Conversation
download_file_from_hf did json.load(open(file_path, "r")), leaving the file handle for the garbage collector to close. Use a with block so it is closed deterministically (no ResourceWarning on non-refcounting interpreters). Adds a regression test that fails without the fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
download_file_from_hfloaded.jsonfiles withjson.load(open(file_path, "r")), which never closes the file handle explicitly. On CPython the refcount usually closes it immediately, but it is still an unclosed handle (aResourceWarningunder-W error::ResourceWarning, and held open until GC on interpreters without refcounting). This changes it towith open(file_path, "r") as f: return json.load(f).This is the only unclosed
open(intransformer_lens/utilities/hf_utils.py; the otheropen(there (line ~380) already uses awithblock. No other changes.Adds
TestDownloadFileFromHf::test_closes_json_file_after_loading. It needs no network: it stubshf_hub_downloadto return a temp file, wrapsbuiltins.opento keep a reference to the handle opened for that path, and asserts the handle is closed after the call. The test fails ondevwithout the fix (assert opened[0].closed->AssertionError: assert False) and passes with it.Fixes # (no existing issue; I searched open PRs and issues for
download_file_from_hfand found no overlap)Type of change
Checklist:
uv sync --locked --group dev, thenpytest tests/unit/utilities, 410 passed. Only this directory was run, not the full suite.)Verification detail: before the fix (new test only, applied to
dev) 1 failed; after the fixpytest tests/unit/utilitiesgave 410 passed.black --checkandisort --check-onlypass on both changed files.🤖 Generated with Claude Code