Skip to content

Fix missing tool export requirements for commented imports#2212

Open
luojiyin1987 wants to merge 4 commits intohuggingface:mainfrom
luojiyin1987:fix/tool-requirements-imports
Open

Fix missing tool export requirements for commented imports#2212
luojiyin1987 wants to merge 4 commits intohuggingface:mainfrom
luojiyin1987:fix/tool-requirements-imports

Conversation

@luojiyin1987
Copy link
Copy Markdown

Summary

Fix tool export requirement detection when imports include inline comments.

Closes #2211.

Changes

  • parse imports via AST in get_imports() instead of regex
  • keep excluding imports inside optional try/except blocks
  • keep excluding flash-attn guarded imports
  • include generated forward source when collecting requirements for SimpleTool
  • add regression coverage for commented imports and function-tool save requirements

Verification

  • 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'

Copy link
Copy Markdown

@VANDRANKI VANDRANKI left a comment

Choose a reason for hiding this comment

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

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.

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.

Tool save can miss requirements for commented imports

2 participants