跳到正文
Hacker News (official RSS)(github.com)·· 6 天前品質評分71

Unref 用 Rust 找出 Python 專案裡沒用到的程式碼

Show HN: Unref – A Python dead code finder built on Ruff's parser

重點導讀

Python 專案變大後,要找出沒用到的程式碼,可能得花不少時間,也容易把不同模組裡同名的程式混在一起。

  • Unref 是以 Rust 撰寫的檢查工具,使用 Ruff 的語法分析器來找未使用的類別和函式,並按模組分開辨認名稱。作者表示,Unref 檢查約 2,300 個檔案的 CPython 程式碼不到 1 秒,速度是 vulture 的 20 倍。
  • 它也內建 pytest、Pydantic 和 SQLAlchemy 等工具的規則。

AI 伴隨問答

自備 Key 深度分析

針對這篇報導有疑問?可直接點選快捷問題或向 AI 追問背景脈絡與技術細節:

正文

image image image image

An extremely fast Python dead code finder, written in Rust.

Time to check CPython with unref and other dead code finders

Checking the whole CPython codebase (v3.15.0, about 2,300 files), measured with scripts/benchmark.py.

  • Fast: 20x faster than vulture, it checks all of CPython in under a second.
  • Just works: built-in rules for pytest, Pydantic, SQLAlchemy and more, so most projects need little or no configuration.
  • Accurate: names are matched by module, so a used parse in one module does not hide an unused parse in another.
  • Ruff-style: same conventions, options and output style as Ruff, and it finds the unused code Ruff leaves out.
  • Modern: ty-style rule codes, configuration in pyproject.toml, built on Ruff's parser.
  • Fits your workflow: runs with uvx, as a pre-commit or prek hook, and in GitHub Actions.

Documentation: unref.readthedocs.io

Getting Started

Run it with uvx:

$ uvx unref
src/project/models.py:12:7: unused-class 'Account'
src/project/utils.py:40:5: unused-function 'parse_date'

Or as a pre-commit or prek hook, alongside Ruff:

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.17.4
    hooks:
      - id: ruff-check
      - id: ruff-format
  - repo: https://github.com/Apakottur/unref
    rev: v0.2.0 # Or run `pre-commit autoupdate`.
    hooks:
      - id: unref

Contributing

False positives and other bugs are welcome as issues. To contribute code, see CONTRIBUTING.md.

讀者討論

理性交流 · 即時討論