Python 重复代码检测
查找重复的 Python 代码 在它继续扩散之前。
Deslop 在整个 Python 代码库中查找重复函数、复制粘贴的类和改名后的副本,把最严重的重复代码排在最前面,并在你输入时于 VS Code 中标出新的副本。
打开 VS Code 并安装扩展。免费、开源,代码不会离开你的电脑。
英文文字稿
Click, open in VS Code
This is Click, the Python library behind thousands of command-line tools, open in VS Code with Deslop installed. Deslop has already read every Python file in the library and ranked the duplicate code it found. The worst copies sit at the top of this list, and the warning under this method says it exists more than once.
Compare two exceptions
Near the top, Deslop pairs two exception classes: No Such Option and No Such Command. Compare puts them side by side. The constructor, the default message and the close-match suggestions are the same code. The only change is that option became command, all the way through.
Three identical methods
This finding is exact. The Bash, Z shell and PowerShell completion classes each define the same get completion args method, line for line. I follow each file link to read the copies in place before I change anything.
Remove the copies
The other two classes can reuse the Bash method directly, so I replace both copies with one line each and save. Deslop scans again straight away and the finding is gone. Then I run the shell completion tests from Click, and all fifty eight still pass.
真实录像,真实仓库
观看 Deslop 清理 Click 中的重复代码
Click 是成千上万个命令行工具背后的 Python 库,也是 GitHub 上维护得最好的代码库之一。这段视频是 Deslop 在 VS Code 中运行于 Click 源码(提交 8b19813)的真实录像,未经摆拍,仅为简短而缩放和加速:它找到真实的克隆,并排比较,然后用两行重构删除其中一处,而 Click 自己的测试依然全部通过。
在 Click 中发现
三种重复代码,全部来自一个维护良好的 Python 项目
每个成熟的 Python 代码库都会积累副本:粘贴到兄弟类里的方法、复制后改名的异常、写了两遍的装饰器重载。这些都不是缺陷,但每一处都意味着下一次修复要做不止一次。
-
完全相同
一个方法,写了三遍
def get_completion_args(self) -> tuple[list[str], str]:src/click/shell_completion.py · 第 432、454、530 行
Bash、Zsh 和 PowerShell 补全类各自逐行定义了同一个 get_completion_args 方法。视频中,Zsh 和 PowerShell 改为复用 Bash 的方法,58 个 shell 补全测试依然通过。
-
改名副本
两个异常,同一个结构
class NoSuchOption(UsageError): class NoSuchCommand(UsageError):src/click/exceptions.py · 第 232 至 301 行
NoSuchOption 和 NoSuchCommand 共用同样的构造函数、默认消息和相近匹配建议。文本 diff 看到几十行改动,Deslop 看到的是一次一致的改名。
-
几乎相同
并排的装饰器重载
@t.overload def command(name: _AnyCallable) -> Command: ...src/click/decorators.py · 第 133 至 165 行与第 258 至 290 行
command 和 group 装饰器的类型重载是同一段代码,只是把 Command 换成了 Group。修改其中一处之前,最好知道另一处的存在。
超越 linter
为什么 Python linter 会漏掉大多数副本
Pylint、Flake8 和 Ruff 是保证 Python 代码质量的必备工具:它们检查风格、导入和潜在缺陷。重复是另一个问题,它关注的是两段代码之间的关系,而不是其中某一段是否有错。
Pylint 的重复代码检查(R0801)按文本行比较,能找到逐字相同的副本,但变量一改名就找不到了。Deslop 用 tree-sitter 解析 Python 并比较语法结构,所以 option_name 和 command_name 依然能对上。保留你的 linter,再加上 Deslop 找出它看不到的副本。
如何融入工作流
从安装到第一次重构,只需一分钟
安装并打开项目
Deslop 会自动扫描工作区中的 Python 文件,无需任何配置。Top Offenders 视图按严重程度列出每一组重复代码。
比较副本
打开一条结果即可看到每个副本的文件和行号,再在 VS Code 的 diff 编辑器中比较任意两处,然后决定如何合并。
边重构边获得反馈
Deslop 会在你编辑时重新扫描。删除一个副本,结果随即消失;粘贴一个副本,警告会在代码审查之前出现。
让 AI 编程助手不再重复
同样的实时分析也以 MCP 服务器运行,Claude Code、Cursor 和 GitHub Copilot 可以在编写新代码前询问是否已有相似的 Python 代码。
Python 开发者常见问题
如何在 Python 项目中查找重复代码?
在 VS Code 中安装 Deslop 并打开项目。它会解析每个 .py 文件,匹配即使改了名字也在重复同一逻辑的函数、方法和代码块,并在 Top Offenders 视图中按严重程度列出。同一引擎也可作为 CLI 和 GitHub Action 在 CI 中运行。
Deslop 会取代 Pylint、Ruff 或 Flake8 吗?
不会。继续用它们检查风格、导入和潜在缺陷。Deslop 做的是它们不做的事:找出跨文件重复编写的同一逻辑,包括名称和字面量已改变的副本。
我的代码会离开我的电脑吗?
不会。Deslop 在 VS Code 中本地运行,无需账号,也不会把源代码发送到任何地方。
它能与 GitHub Copilot、Claude Code 和 Cursor 配合使用吗?
可以。Deslop 内置 MCP 服务器,编程助手可以在编写新代码前查询,从而复用已有函数,而不是生成一个近似副本。
它还支持哪些语言?
除 Python 外,同一个扩展还支持 C、C#、Rust、Dart、JavaScript、TypeScript/TSX、PHP、F#、Go 和 Osprey。
Deslop 免费吗?
免费。Deslop 以 MIT 许可证开源,VS Code 扩展可在 Visual Studio Marketplace 免费获取。
你的 Python 代码库里有副本。一分钟就能看到。
一次点击即可在 VS Code 中安装 Deslop。打开项目,最严重的重复代码已经排在列表顶部。