Python 冗余导入修复
coding.fix_bloated_imports
修复 Python 模块中仅需用到大型包的少数函数却整体导入的问题
- License 许可
- Apache-2.0
- 最近更新
- 2026-11-02
- Stars
- ★ 155
- Forks
- 170
01 前言 Front Matter
- model
- haiku
- description
- Fix Python imports of large packages needed only for few functions in a module
02 资源文件 3
- 参考文档README.md2.7 KB
- 参考文档references/anti-patterns.md2.8 KB
- 参考文档references/se-principles.md5.7 KB
03 SKILL.md 正文
-
I will pass you one or more files
<FILES>and one or more packages<PACKAGES>that are usually large to import (e.g.,ipython,pandas) and are needed only in few functions in the files -
In the files
<FILES>you will use a conditional import for type checking and lazy imports for<PACKAGES>- E.g., transform
intodef make_slider() -> "ipywidgets.IntSlider": import ipywidgets ...from typing import TYPE_CHECKING if TYPE_CHECKING: import ipywidgets def make_slider() -> "ipywidgets.IntSlider": import ipywidgets ...
- E.g., transform
-
Use forward references only for the types in the package to remove and not for the including
- Bad (quotes the whole expression as one string)
"Tuple[Union[ipywidgets.FloatSlider, ipywidgets.IntSlider], ipywidgets.HBox]" - Good (quotes only the package types)
Tuple[Union["ipywidgets.FloatSlider", "ipywidgets.IntSlider"], "ipywidgets.HBox"]
- Bad (quotes the whole expression as one string)
-
If you see that most of the functions in
<FILES>require the passed package<PACKAGES>, you might suggest to not do this transform
Verification
- [ ] Confirm
<PACKAGES>is imported only underTYPE_CHECKINGor inside the functions that use it - [ ] Confirm forward references quote only the package types, not full expressions
04 同仓库更多 Skills
Docker 文件同步与模板对齐
tutorial_in_60_mins.propagate_docker_changes
同步教程和课程项目的 Docker 文件,使其与 project_template 保持一致。
Markdown 合并至 Jupyter Notebook
tutorial_in_60_mins.merge_markdown
将 Markdown 文件的内容合并到 Jupyter Notebook 中。
60 分钟教程格式规范化
tutorial_in_60_mins.format
格式化目录,使其遵循「Learn X in 60 Minutes」(60 分钟学会 X)教程约定。
60 分钟教程目录创建
tutorial_in_60_mins.create
按照「Learn X in 60 Minutes」(60 分钟学会 X)约定创建教程目录。
todo.reorg_in_progress
Reorganize ai_task_queue.md so in-progress tasks, not-started tasks, and mislabeled topic headers sit under the right H1 stage
todo.reorg_headers
Reorganize ai_task_queue.md .md so every H2 topic matches ALL TOPICS and every H3 task sits under the right topic, keeping its H1 stage