Skip to content

fix: close the json file handle in download_file_from_hf - #2058

Open
faisalhacks wants to merge 1 commit into
TransformerLensOrg:devfrom
faisalhacks:fix-hf-download-file-handle
Open

faisalhacks wants to merge 1 commit into
TransformerLensOrg:devfrom
faisalhacks:fix-hf-download-file-handle

Conversation

@faisalhacks

Copy link
Copy Markdown

Description

download_file_from_hf loaded .json files with json.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 (a ResourceWarning under -W error::ResourceWarning, and held open until GC on interpreters without refcounting). This changes it to with open(file_path, "r") as f: return json.load(f).

This is the only unclosed open( in transformer_lens/utilities/hf_utils.py; the other open( there (line ~380) already uses a with block. No other changes.

Adds TestDownloadFileFromHf::test_closes_json_file_after_loading. It needs no network: it stubs hf_hub_download to return a temp file, wraps builtins.open to keep a reference to the handle opened for that path, and asserts the handle is closed after the call. The test fails on dev without 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_hf and found no overlap)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • I have commented my code, particularly in hard-to-understand areas (two-line change, no comment needed)
  • I have made corresponding changes to the documentation (not applicable)
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (not run locally; run on Modal instead: uv sync --locked --group dev, then pytest tests/unit/utilities, 410 passed. Only this directory was run, not the full suite.)
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

Verification detail: before the fix (new test only, applied to dev) 1 failed; after the fix pytest tests/unit/utilities gave 410 passed. black --check and isort --check-only pass on both changed files.

🤖 Generated with Claude Code

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

No deployments
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.

2 participants