lint
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLinting and Code Quality Skill
代码检查与代码质量技能
This skill helps you efficiently validate and format code using the project's comprehensive linting infrastructure.
本技能可借助项目全面的代码检查基础设施,帮助你高效验证和格式化代码。
When to Use This Skill
何时使用本技能
Use this skill when you:
- Edit a file and want to format it before committing
- Need to validate code style, types, or security
- Want to check for spelling errors or documentation issues
- Need to validate test infrastructure (suitespec, log messages)
- Want to run comprehensive quality checks before pushing
当你遇到以下场景时,可使用本技能:
- 编辑文件后,希望在提交前进行格式化
- 需要验证代码风格、类型或安全性
- 想要检查拼写错误或文档问题
- 需要验证测试基础设施(suitespec、日志消息)
- 希望在推送代码前运行全面的质量检查
Key Principles
核心原则
- Always format after editing - Use immediately after code changes
hatch run lint:fmt -- <file> - Run comprehensive checks before committing - Use before pushing
hatch run lint:checks - Target specific files - Use syntax to validate only what you changed, not the entire codebase
-- <file> - Fix auto-fixable issues - Use instead of manually fixing style issues
fmt - Type check after adding types - Use after adding type annotations
hatch run lint:typing -- <file>
- 编辑后立即格式化 - 代码修改完成后,立即使用 命令
hatch run lint:fmt -- <file> - 提交前运行全面检查 - 推送代码前使用 命令
hatch run lint:checks - 针对特定文件 - 使用 语法仅验证你修改的内容,而非整个代码库
-- <file> - 修复可自动修复的问题 - 使用 命令而非手动修复风格问题
fmt - 添加类型后进行类型检查 - 添加类型注解后,使用 命令
hatch run lint:typing -- <file>
Quick Start
快速开始
Run all checks (broad validation):
bash
hatch run lint:checksFormat and validate a specific file:
bash
hatch run lint:fmt -- path/to/file.pyCheck types on a specific file:
bash
hatch run lint:typing -- path/to/file.py运行全部检查(全面验证):
bash
hatch run lint:checks格式化并验证特定文件:
bash
hatch run lint:fmt -- path/to/file.py检查特定文件的类型:
bash
hatch run lint:typing -- path/to/file.pyAvailable Lint Scripts
可用的Lint脚本
Code Formatting
代码格式化
fmt
- Format code (recommended for most edits)
fmtfmt
- 格式化代码(大多数编辑场景推荐使用)
fmtFormats and validates code style using Ruff.
Usage:
bash
undefined使用Ruff格式化并验证代码风格。
使用方式:
bash
undefinedFormat entire codebase
格式化整个代码库
hatch run lint:fmt
hatch run lint:fmt
Format specific files
格式化特定文件
hatch run lint:fmt -- ddtrace/tracer.py tests/test_tracer.py
hatch run lint:fmt -- ddtrace/tracer.py tests/test_tracer.py
Format specific directory
格式化特定目录
hatch run lint:fmt -- ddtrace/contrib/flask/
**What it does:**
1. Runs the Ruff formatter
2. Runs Ruff with --fix to auto-fix issues
3. Re-validates with style checks
**When to use:** After making code changes to automatically format and fix style issues.hatch run lint:fmt -- ddtrace/contrib/flask/
**功能说明:**
1. 运行Ruff格式化工具
2. 使用Ruff的--fix参数自动修复问题
3. 重新执行风格检查进行验证
**适用场景:** 代码修改完成后,自动格式化并修复风格问题。fmt-snapshots
- Format snapshot files
fmt-snapshotsfmt-snapshots
- 格式化快照文件
fmt-snapshotsFormats snapshot test files used in snapshot-based testing.
Usage:
bash
hatch run lint:fmt-snapshots -- tests/snapshots/When to use: After snapshot test updates or when snapshot files need reformatting.
格式化基于快照测试的快照测试文件。
使用方式:
bash
hatch run lint:fmt-snapshots -- tests/snapshots/适用场景: 更新快照测试后,或快照文件需要重新格式化时。
Style Checking
风格检查
style
- Check all style issues (no auto-fix)
stylestyle
- 检查所有风格问题(无自动修复)
styleValidates code style without automatically fixing issues.
Usage:
bash
undefined验证代码风格,但不自动修复问题。
使用方式:
bash
undefinedCheck entire codebase
检查整个代码库
hatch run lint:style
hatch run lint:style
Check specific files
检查特定文件
hatch run lint:style -- ddtrace/
**What it validates:**
- Ruff formatting
- Ruff linting rules
- Cython linting
- C code formatting
- CMake formatting
**When to use:** To verify style compliance before committing without auto-fixes.hatch run lint:style -- ddtrace/
**验证内容:**
- Ruff格式化
- Ruff检查规则
- Cython代码检查
- C代码格式化
- CMake格式化
**适用场景:** 提交前验证代码风格合规性,且不需要自动修复。format_check
- Check formatting
format_checkformat_check
- 检查格式化情况
format_checkValidates Python code formatting with (no auto-fix).
ruff formatUsage:
bash
hatch run lint:format_check -- ddtrace/tracer.pyWhen to use: Quick check of Python formatting before committing.
使用验证Python代码格式化(无自动修复)。
ruff format使用方式:
bash
hatch run lint:format_check -- ddtrace/tracer.py适用场景: 提交前快速检查Python代码格式化情况。
Type Checking
类型检查
typing
- Type check with mypy
typingtyping
- 使用mypy进行类型检查
typingValidates Python type hints and catches type-related errors.
Usage:
bash
undefined验证Python类型提示,捕获类型相关错误。
使用方式:
bash
undefinedCheck all types
检查所有类型
hatch run lint:typing
hatch run lint:typing
Check specific files (mypy path format)
检查特定文件(使用mypy路径格式)
hatch run lint:typing -- ddtrace/tracer.py
**When to use:** After adding type hints or modifying functions with type annotations.hatch run lint:typing -- ddtrace/tracer.py
**适用场景:** 添加类型提示或修改带类型注解的函数后。Security Checks
安全检查
security
- Security audit with Bandit
securitysecurity
- 使用Bandit进行安全审计
securityScans code for common security issues and vulnerabilities.
Usage:
bash
undefined扫描代码中的常见安全问题与漏洞。
使用方式:
bash
undefinedScan entire codebase
扫描整个代码库
hatch run lint:security
hatch run lint:security
Scan specific directory
扫描特定目录
hatch run lint:security -- -r ddtrace/contrib/
**When to use:** Before committing code that handles user input, credentials, or sensitive operations.hatch run lint:security -- -r ddtrace/contrib/
**适用场景:** 提交处理用户输入、凭证或敏感操作的代码前。Spelling and Documentation
拼写与文档检查
spelling
- Check spelling
spellingspelling
- 检查拼写
spellingValidates spelling in documentation, comments, and docstrings.
Usage:
bash
undefined验证文档、注释和文档字符串中的拼写。
使用方式:
bash
undefinedCheck all spelling
检查所有拼写
hatch run lint:spelling
hatch run lint:spelling
Check specific files
检查特定文件
hatch run lint:spelling -- docs/ releasenotes/
**When to use:** Before committing documentation or user-facing text.hatch run lint:spelling -- docs/ releasenotes/
**适用场景:** 提交文档或面向用户的文本前。Test Infrastructure
测试基础设施检查
riot
- Validate riotfile
riotriot
- 验证riotfile
riotDoctests the riotfile to ensure test venv definitions are valid.
Usage:
bash
hatch run lint:riotWhen to use: After modifying to validate syntax and doctest examples.
riotfile.py对riotfile进行文档测试,确保测试虚拟环境定义有效。
使用方式:
bash
hatch run lint:riot适用场景: 修改后,验证语法和文档测试示例。
riotfile.pysuitespec-check
- Validate test suite specifications
suitespec-checksuitespec-check
- 验证测试套件规范
suitespec-checkChecks that test suite patterns in cover all test files.
tests/suitespec.ymlUsage:
bash
hatch run lint:suitespec-checkWhen to use: After adding new test files or modifying suite specifications.
检查中的测试套件模式是否覆盖所有测试文件。
tests/suitespec.yml使用方式:
bash
hatch run lint:suitespec-check适用场景: 添加新测试文件或修改套件规范后。
error-log-check
- Validate error log messages
error-log-checkerror-log-check
- 验证错误日志消息
error-log-checkEnsures error log messages follow project conventions.
Usage:
bash
hatch run lint:error-log-checkWhen to use: After adding new error logging statements.
确保错误日志消息符合项目约定。
使用方式:
bash
hatch run lint:error-log-check适用场景: 添加新的错误日志语句后。
Code Analysis
代码分析
sg
- Static analysis with ast-grep
sgsg
- 使用ast-grep进行静态分析
sgPerforms static code analysis using ast-grep patterns.
Usage:
bash
undefined使用ast-grep模式执行静态代码分析。
使用方式:
bash
undefinedScan all files
扫描所有文件
hatch run lint:sg
hatch run lint:sg
Scan specific directory
扫描特定目录
hatch run lint:sg -- ddtrace/
**When to use:** To find code patterns that may need refactoring or optimization.hatch run lint:sg -- ddtrace/
**适用场景:** 查找可能需要重构或优化的代码模式。sg-test
- Test ast-grep rules
sg-testsg-test
- 测试ast-grep规则
sg-testValidates ast-grep rule definitions.
Usage:
bash
hatch run lint:sg-testWhen to use: After modifying ast-grep rules or patterns.
验证ast-grep规则定义。
使用方式:
bash
hatch run lint:sg-test适用场景: 修改ast-grep规则或模式后。
C/CMake Formatting
C/CMake格式化检查
cformat_check
- Check C code formatting
cformat_checkcformat_check
- 检查C代码格式化
cformat_checkValidates C code formatting.
Usage:
bash
hatch run lint:cformat_checkWhen to use: After modifying C extension code.
验证C代码格式化情况。
使用方式:
bash
hatch run lint:cformat_check适用场景: 修改C扩展代码后。
cmakeformat_check
- Check CMake formatting
cmakeformat_checkcmakeformat_check
- 检查CMake格式化
cmakeformat_checkValidates CMake file formatting.
Usage:
bash
hatch run lint:cmakeformat_checkWhen to use: After modifying CMakeLists.txt or other CMake files.
验证CMake文件格式化情况。
使用方式:
bash
hatch run lint:cmakeformat_check适用场景: 修改CMakeLists.txt或其他CMake文件后。
Common Workflows
常见工作流
Workflow 1: Quick File Format and Check
工作流1:快速格式化与检查文件
After editing a Python file, format and validate it:
bash
undefined编辑Python文件后,格式化并验证:
bash
undefinedEdit the file...
编辑文件...
Then run:
然后运行:
hatch run lint:fmt -- path/to/edited/file.py
undefinedhatch run lint:fmt -- path/to/edited/file.py
undefinedWorkflow 2: Type Check After Adding Types
工作流2:添加类型后的类型检查
After adding type hints:
bash
hatch run lint:typing -- ddtrace/contrib/flask/patch.py添加类型提示后:
bash
hatch run lint:typing -- ddtrace/contrib/flask/patch.pyWorkflow 3: Full Validation Before Commit
工作流3:提交前的全面验证
Run all checks before creating a commit:
bash
hatch run lint:checksThis runs:
- style checks
- typing checks
- spelling checks
- riot validation
- security checks
- suitespec validation
- error log validation
- ast-grep analysis
创建提交前运行全部检查:
bash
hatch run lint:checks此命令会运行:
- 风格检查
- 类型检查
- 拼写检查
- riot验证
- 安全检查
- suitespec验证
- 错误日志验证
- ast-grep分析
Workflow 4: Security Review
工作流4:安全审查
Before committing code handling sensitive operations:
bash
hatch run lint:security -- -r ddtrace/contrib/提交处理敏感操作的代码前:
bash
hatch run lint:security -- -r ddtrace/contrib/Workflow 5: Documentation Review
工作流5:文档审查
After writing documentation or docstrings:
bash
hatch run lint:spelling -- docs/ ddtrace/编写文档或文档字符串后:
bash
hatch run lint:spelling -- docs/ ddtrace/Best Practices
最佳实践
DO ✅
建议✅
- Format files immediately after editing: Use to auto-fix style issues
hatch run lint:fmt -- <file> - Run before pushing: Ensures all quality gates pass
lint:checks - Target specific files: Use syntax to validate only what you changed
-- <file> - Check types early: Run after adding type annotations
lint:typing - Read error messages: Understand what lint failures mean before fixing
- 编辑后立即格式化文件:使用 自动修复风格问题
hatch run lint:fmt -- <file> - 推送前运行:确保所有质量关卡都能通过
lint:checks - 针对特定文件:使用语法仅验证你修改的内容
-- <file> - 尽早检查类型:添加类型注解后运行
lint:typing - 阅读错误消息:在修复前理解Lint失败的原因
DON'T ❌
不建议❌
- Ignore lint failures: They indicate potential bugs or style issues
- Manually fix issues that auto-fix can handle: Use instead
fmt - Commit without running lint:checks: Let automation catch issues before push
- Run lint:checks every time for small changes: Use targeted commands during development
- 忽略Lint失败:失败提示可能意味着潜在的bug或风格问题
- 手动修复可自动修复的问题:使用命令代替手动操作
fmt - 未运行就提交:让自动化工具在推送前发现问题
lint:checks - 小改动时每次都运行:开发过程中使用定向命令
lint:checks
Passing Arguments
传递参数
All lint commands support passing arguments with syntax:
--bash
undefined所有Lint命令都支持使用语法传递参数:
--bash
undefinedBasic format
基本格式
hatch run lint:<script> -- <args>
hatch run lint:<script> -- <args>
Examples:
示例:
hatch run lint:fmt -- ddtrace/tracer.py # Format specific file
hatch run lint:typing -- ddtrace/ # Type check directory
hatch run lint:security -- -r ddtrace/contrib/ # Security scan with args
hatch run lint:spelling -- docs/ releasenotes/ # Spelling check specific paths
undefinedhatch run lint:fmt -- ddtrace/tracer.py # 格式化特定文件
hatch run lint:typing -- ddtrace/ # 检查目录类型
hatch run lint:security -- -r ddtrace/contrib/ # 带参数的安全扫描
hatch run lint:spelling -- docs/ releasenotes/ # 检查特定路径的拼写
undefinedTroubleshooting
故障排除
Formatting keeps failing
格式化持续失败
Ensure you've run to auto-fix style issues first:
hatch run lint:fmtbash
hatch run lint:fmt -- <file>
hatch run lint:style -- <file> # Should now pass确保先运行自动修复风格问题:
hatch run lint:fmtbash
hatch run lint:fmt -- <file>
hatch run lint:style -- <file> # 现在应该可以通过Type errors after editing
编辑后出现类型错误
Make sure type hints are correct and all imports are available:
bash
hatch run lint:typing -- <file>确保类型提示正确,且所有导入都可用:
bash
hatch run lint:typing -- <file>Lint command not found
找不到Lint命令
Ensure you're running from the project root:
bash
cd /path/to/dd-trace-py
hatch run lint:checks确保你在项目根目录运行命令:
bash
cd /path/to/dd-trace-py
hatch run lint:checksToo many errors to fix manually
错误太多无法手动修复
Use to auto-fix most issues:
fmtbash
hatch run lint:fmt -- .使用命令自动修复大多数问题:
fmtbash
hatch run lint:fmt -- .Related
相关资源
- run-tests skill: For validating that changes don't break tests
- hatch.toml: Source of truth for all lint configurations
- riotfile.py: Defines test venvs and combinations
- run-tests技能:用于验证修改是否会破坏测试
- hatch.toml:所有Lint配置的权威来源
- riotfile.py:定义测试虚拟环境及组合