Fix missing tool export requirements for commented imports#2212
Fix missing tool export requirements for commented imports#2212luojiyin1987 wants to merge 4 commits intohuggingface:mainfrom
Conversation
VANDRANKI
left a comment
There was a problem hiding this comment.
Replacing the regex-based import detection with ast.parse() is the right fix. Regexes on Python source cannot correctly handle all comment forms (# noqa: F401, # type: ignore[import-not-found], etc.) without becoming arbitrarily complex. The AST approach is robust by construction.
visit() design is correct: the in_try_block and in_flash_attn_block flags propagate downward through child nodes, so any import nested inside a guarded block is correctly excluded. The ast.If detection for flash_attn guards uses re.fullmatch on the function name, which is more precise than the previous regex that operated on raw text.
requirements_sources.extend([tool_code, forward_source_code]) fixes a real bug: previously only tool_code was passed to get_imports(), but the forward function source could introduce additional imports not present in the class body. Now both are scanned.
# FIXME tests updated: the two tests that previously asserted {'smolagents'} with a FIXME comment now correctly assert {'IPython', 'smolagents'}. These are the best kind of regression tests - they came from a known bug.
Edge case: ast.parse(code) raises SyntaxError for invalid Python. The old regex approach would return [] (no imports found) for unparseable code. If get_imports() is ever called on code that could be syntactically invalid (e.g. templates with placeholder syntax), a try/except SyntaxError: return [] guard would maintain backward compatibility. Not a blocker since tool code is expected to be valid Python.
LGTM.
Summary
Fix tool export requirement detection when imports include inline comments.
Closes #2211.
Changes
get_imports()instead of regextry/exceptblocksSimpleToolVerification
python -m pytest tests/test_function_type_hints_utils.py -k 'test_get_imports'python -m pytest tests/test_utils.py -k 'function_tool_save'