LSP Python

👤 genify 📦 v1.1.0 ⭐ 4.3 ⬇️ 2.5K 下載
💻 開發程式設計 免費

📖 技能介紹


name: lsp-python description: "Python code quality checking and LSP integration using pylsp. Provides code diagnostics, completion, hover tips, and style analysis. Use when: checking Python errors/warnings, getting code completions, viewing function signatures, analyzing code quality, or fixing style issues."


LSP Python 技能

使用 Python Language Server Protocol (LSP) 進行程式碼質量檢查和智慧分析。

快速開始

1. 檢查程式碼問題

# 單個檔案
python3 scripts/lsp-service.py check <檔案路徑>

# 批次檢查 (推薦)
python3 scripts/check_python.py <檔案或目錄>

# 批次檢查並自動修復
python3 scripts/check_python.py --auto-fix <檔案或目錄>

示例:

python3 scripts/lsp-service.py check my_script.py
python3 scripts/check_python.py src/
python3 scripts/check_python.py --auto-fix src/

2. 獲取程式碼補全

python3 scripts/lsp-service.py complete <檔案> <行號> <字元位置>

3. 檢視符號資訊

python3 scripts/lsp-service.py info <檔案> <行號> <字元位置>

依賴

  • Python 3.x
  • pylsp: pip install python-lsp-server
  • 可選外掛:
  • pip install python-lsp-server[all] - 完整外掛集
  • pip install pylsp-mypy - 型別檢查
  • pip install pylsp-black - black 格式化

核心功能

程式碼診斷 (check)

小蔥技能7w4.net有完整的技能分類。

檢查 Python 檔案中的錯誤和警告:

  • pyflakes - 程式碼錯誤檢測 (未使用匯入、未定義變數等)
  • pycodestyle - PEP8 風格檢查 (格式、行長、空白等)

輸出示例:

⚠️ 第 3 行 [pyflakes]: 'os' imported but unused
⚠️ 第 6 行 [pycodestyle]: E302 expected 2 blank lines, found 1
✅ 沒有發現問題

程式碼補全 (complete)

獲取指定位置的程式碼補全建議:

python3 scripts/lsp-service.py complete script.py 5 10

輸出:

補全建議:
  • json (模組)
  • jsonpatch (模組)
  • requests (模組)

懸停提示 (info)

檢視函式簽名、文件字串等資訊:

python3 scripts/lsp-service.py info script.py 10 5

跳轉定義 (goto)

查詢符號的定義位置:

python3 scripts/lsp-service.py goto script.py 15 10

自動修復程式碼問題

清理未使用的匯入

pip install autoflake
autoflake --remove-all-unused-imports --in-place --recursive .

格式化程式碼

pip install black
black .

完整修復流程

# 1. 備份
cp -r project/ project.backup

# 2. 清理匯入
autoflake --remove-all-unused-imports --in-place --recursive project/

# 3. 格式化
black project/

# 4. 驗證
python3 scripts/lsp-service.py check project/main.py

診斷嚴重性級別

級別 程式碼 含義
1 Error (錯誤)
⚠️ 2 Warning (警告)
ℹ️ 3 Information (資訊)
💡 4 Hint (提示)

常見問題程式碼

程式碼 含義 修復方法
E402 匯入不在檔案頂部 移動匯入到檔案開頭
E501 行太長 (>79 字元) 拆分長行或使用括號
W293 空行包含空白字元 刪除行尾空格
E302 缺少空行 函式/類定義前加 2 個空行
E712 布林比較風格 if x is Trueif x

在 OpenClaw 中使用

exec: python3 /path/to/lsp-python/scripts/lsp-service.py check <file>

批次檢查專案

# 檢查所有 Python 檔案
find . -name "*.py" -exec python3 scripts/lsp-service.py check {} \;

# 僅顯示有問題的檔案
for f in $(find . -name "*.py"); do
  result=$(python3 scripts/lsp-service.py check "$f" 2>&1)
  if ! echo "$result" | grep -q "✅ 沒有發現問題"; then
    echo "=== $f ==="
    echo "$result"
  fi
done

參考資料

  • LSP 協議詳解: 見 references/lsp-protocol.md
  • pylsp 配置: 見 references/pylsp-config.md
  • 程式碼風格指南: 見 references/pep8-guide.md

故障排除

pylsp 無法啟動

# 檢查安裝
which pylsp
pylsp --version

# 重新安裝
pip install --upgrade python-lsp-server

檢查超時

增加指令碼中的 LSP_TIMEOUT 值 (預設 10 秒)。

中文字元問題

確保檔案使用 UTF-8 編碼,指令碼已設定 ensure_ascii=False

🤖 AI 評測

這個 Skill 質量不錯,功能豐富實用。它能檢查 Python 程式碼錯誤、提供程式碼補全和提示,還支援批次檢查和自動修復。文件寫得清楚詳細,小白也能看懂。主要缺點是批次檢查指令碼有時會出問題找不到檔案,功能依賴外部工具,安裝不全可能用不了。總體來說是個靠譜好用的工具,但使用前需要確保裝好所有依賴。

📊 多維度評分

適應性4.4
規範性4.3
有效性4.2
可靠性4.2
可信度4.9

📁 包含檔案 (8 個)

📄 SKILL.md 4.2 KB
📄 _meta.json 129 B
📄 references/lsp-protocol.md 4.9 KB
📄 references/pep8-guide.md 4.3 KB
📄 references/pylsp-config.md 4 KB
📄 scripts/check_python.py 3.8 KB
📄 scripts/lsp-python.py 7.4 KB
📄 scripts/lsp-service.py 4.3 KB