fix: capture commented imports in Tool requirements#2536
Open
nolanchic wants to merge 1 commit into
Open
Conversation
get_imports() used a regex anchored on end-of-line, so any import with a trailing comment (e.g. 'import IPython # noqa: F401' or 'import numpy as np # type: ignore') was silently dropped, and Tool.save emitted an incomplete requirements.txt. Rewrite get_imports to walk the AST, which ignores comments by construction and also handles multi-name imports. The previous behaviour of skipping imports inside try/except blocks and is_flash_attn_*_available() guards is preserved, as is the filtering of relative imports. Closes huggingface#2211
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.
What does this PR do?
Closes #2211.
get_imports()extracted imported modules with a regex anchored at end-of-line (^\s*import\s+(\S+?)(?:\s+as\s+\S+)?\s*$). Any import carrying a trailing comment failed to match, so it was silently dropped — e.g.import IPython # noqa: F401orimport numpy as np # type: ignore. As a resultTool.save/Tool.to_dictproduced an incompleterequirements.txtand saved tools could be missing dependencies.How
Rewrite
get_importsto parse the snippet withastand walk the statements. Comments are ignored by construction, and multi-name imports (import a, b) are handled correctly.textwrap.dedentis applied first so indented snippets (e.g. a tool body) parse without anIndentationError.The previous filtering behaviour is preserved:
try/exceptblocks (optional dependencies) are skipped,if is_flash_attn_*_available()guards (cpu-only environments) are skipped,from . import x) are excluded.Test
test_get_importscoveringimport/import ... as/from ... importeach followed by an inline comment. Fails onmain, passes with this change.import os.path) still pass.@toolwhoseforwardcontains a commented import now lists it into_dict()["requirements"].