Inefficient regex in `extract_full_summary_from_signature`

未关闭
#281 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@Shubham-Padkonde 已经在做这个了。

开始于 2026年9月21日。

  • #308 来自 @Shubham-Padkonde —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
cli, performance

调研方向

从 knack/introspection.py 中第 18 行附近的 extract_full_summary_from_signature regex 开始。重现 issue 中所示的长空格输入和格式错误输入,然后验证参数文档仍能正确提取,同时病态输入不再导致过度回溯。所提出的 regex 和示例定义了预期行为。

由索引模型根据 Issue 内容生成。

描述

As pointed out by https://gist.github.com/prodigysml/d07cd482214c80bfb6d3240454d2f679, this regex (introduced by https://github.com/microsoft/knack/commit/430c39e657d8a424ef9b631782fe0e62a6bed203, #8) is inefficient:

https://github.com/microsoft/knack/blob/e0c14114aea5e4416c70a77623e403773aba73a8/knack/introspection.py#L18

It tries to match a string such as:

        :param command_loader: The command loader that commands will be registered into

As shown in https://regex101.com/, a simple :param r requires 1214 steps to fail.

image

:param r causes catastrophic backtracking:

Image

This is because \s+, .+? and \s* all match consecutive spaces, thus can trigger many backtrackings.

A better solution is to replace .+? with \w+ to match the parameter name so that backtrackings can be greatly reduced:

\s*(:param)\s+(\w+)\s*:(.*)

image

主要语言
Python
星标
359
派生
104
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/knack 的其他 Issue

查看 microsoft/knack 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。