You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python项目git pre-commit hook的isort与black校验失败求助

问题

我有一个使用Dash的Python项目,提交代码时pre-commit hook校验失败,代码如下:

@app_dash.callback(
    Output("output_data_upload", "figure"),
    [Input("radioitems_x_axis", "value"), Input("checklist_y_axis", "value")],
    [State("data_store", "data")],
)
def update_figure(
    selected_signal_of_x_axis: str, selected_signal_of_y_axis: list, data: list
) -> go.Figure:
    """
    Plot the figure based on the selections of users.

    :param selected_signal_of_x_axis: a selected signal for x-ordination
    :type selected_signal_of_x_axis: str
    :param selected_signal_of_y_axis: a/several selected signal(s) for y-ordination
    :type selected_signal_of_y_axis: list
    :param data: the filtered data of the uploaded documents
    :type data: list
    :return fig: the figure which is showed on the website based on the selection of users.
    :rtype: go.Figure
    """
    fig = fig_plotting.data_plot(
        selected_signal_of_x_axis, selected_signal_of_y_axis, data
    )
    return fig

TortoiseGit返回的校验结果:

black....................................................................Failed
- hook id: black
- files were modified by this hook

reformatted ReportGeneration\subpages\measurement_chart_plot.py

All done! ✨ 🍰 ✨
1 file reformatted.

nbstripout...........................................(no files to check)Skipped
isort (python)...........................................................Failed
- hook id: isort
- files were modified by this hook

Fixing C:\Users\OneDrive - User\ReportGeneration\subpages\measurement_chart_plot.py

nbqa-black...........................................(no files to check)Skipped
nbqa-isort...........................................(no files to check)Skipped
nbqa-pylint..........................................(no files to check)Skipped
nbqa-ruff............................................(no files to check)Skipped
ruff.....................................................................Passed
[INFO] Restored changes from C:\Users\.cache\pre-commit\patch17047452450-72632.

解决方法

  • 手动执行black格式化目标文件:
    black ReportGeneration/subpages/measurement_chart_plot.py
    
  • 手动执行isort整理导入语句:
    isort C:\Users\OneDrive - User\ReportGeneration\subpages\measurement_chart_plot.py
    
  • 完成上述操作后,重新提交代码,pre-commit hook即可通过校验。

注:pre-commit虽尝试自动修复,但因缓存或文件路径问题恢复了修改,手动执行格式化更可靠。

内容的提问来源于stack exchange,提问作者H KKK L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 18:09:57