code-quality
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePython Code Quality
Python代码质量
High-value Python code-quality anti-patterns to check during review or self-review.
This skill is review-focused: it covers correctness and readability defects that a
reviewer (or a linter) should flag, separate from testing mechanics () and
whole-codebase health scoring ().
pytestcode-quality-scoringSource note: These anti-patterns are derived from CAST Highlight's Python code quality indicators (https://doc.casthighlight.com/), which reference PEP 8 and the Python data model as primary sources. Where a rule mirrors PEP 8, the PEP is the authoritative source. All examples are original.
代码审查或自我审查中需重点关注的高价值Python代码质量反模式。本技能聚焦于审查环节:涵盖审查人员(或代码检查工具)应标记的正确性与可读性缺陷,与测试机制()和全代码库健康评分()相区分。
pytestcode-quality-scoring来源说明: 这些反模式源自CAST Highlight的Python代码质量指标(https://doc.casthighlight.com/),其主要参考**PEP 8**和Python数据模型。若某规则与PEP 8一致,则以PEP 8为权威来源。所有示例均为原创。
When to Use This Skill
适用场景
Use it when the task is "is this Python code clean and correct?" — for example:
- Reviewing a pull request and checking for the defects below.
- Self-reviewing before opening a PR.
- Configuring /
ruff/pylintrules so CI catches these automatically.mypy - Writing or updating a team's Python code-quality guidance.
Do not use it for testing mechanics (use the skill) or for scoring a whole
codebase's health and technical debt (use the skill).
pytestcode-quality-scoring当你需要判断「这段Python代码是否简洁正确?」时使用本技能,例如:
- 审查拉取请求(pull request),检查以下缺陷。
- 提交PR前进行自我审查。
- 配置/
ruff/pylint规则,让CI自动检测这些问题。mypy - 编写或更新团队的Python代码质量指南。
请勿将其用于测试机制(请使用技能),或用于评估全代码库的健康状况与技术债务(请使用技能)。
pytestcode-quality-scoringCore Anti-Patterns (Summary)
核心反模式(摘要)
Six highest-value Python anti-patterns. Each has a non-compliant/compliant example and a
"how to test" note in the reference doc:
- Custom exceptions must derive from — a class meant to be raised that inherits from
Exceptionfails at runtime and breaks everyobjectclause.except - Compare singletons with , not
is— use==/isforis not/None/True(PEP 8); useFalseonly for singletons, never for value comparison.is - Avoid bare / overly broad — catch the narrowest type you can handle; a generic
exceptonly as a last-position fallback that logs or re-raises.except Exception - Avoid wildcard imports () — they hide dependencies, risk silent name collisions, and defeat static analysis.
from x import * - Replace magic numbers with named constants — promote non-obvious literals to documented, named constants.
- Remove unused local variables — a dead assignment misleads readers and can hide a bug where a value was meant to be used.
六大高价值Python反模式。每个反模式在参考文档中均包含非合规/合规示例以及「测试方法」说明:
- 自定义异常必须继承自—— 若一个用于抛出的类继承自
Exception,会在运行时失败,并破坏所有object子句。except - 使用而非
is比较单例 —— 对==/None/True使用False/is(符合PEP 8);仅对单例使用is not,绝不能用于值比较。is - 避免裸或过于宽泛的
except—— 捕获你能处理的最窄类型;仅将通用的except作为最后一步的回退方案,用于日志记录或重新抛出异常。except Exception - 避免通配符导入()—— 它们会隐藏依赖关系,存在静默命名冲突的风险,且会失效静态分析。
from x import * - 用命名常量替换魔术数字 —— 将非直观的字面量提升为带文档说明的命名常量。
- 移除未使用的局部变量 —— 无效的赋值会误导读者,还可能隐藏本应使用该值的bug。
Best Practices
最佳实践
- Gate these in CI. Most are enforceable cheaply with (F403/F405 wildcard, F841 unused locals,
ruff/E711singleton comparison),E712, andpylint. Put the lint step in CI so review effort focuses on judgment, not mechanics.mypy - Prefer specific exception handlers. Order handlers narrowest-first; reserve a
generic for a logging/re-raising last resort.
except Exception - Name intent, not values. A constant's name documents why a threshold exists; a bare literal documents nothing.
- 在CI中设置检查关卡。大多数反模式可通过(F403/F405通配符导入、F841未使用局部变量、
ruff/E711单例比较)、E712和pylint低成本强制执行。将代码检查步骤加入CI,让审查工作聚焦于判断而非机械性检查。mypy - 优先使用特定异常处理器。按范围从窄到宽的顺序排列处理器;仅将通用的作为日志记录/重新抛出异常的最后手段。
except Exception - 命名体现意图,而非值。常量的名称应说明阈值存在的原因;而裸字面量则无法提供任何信息。
Anti-Patterns (What to Avoid)
需避免的反模式
- Inheriting custom exceptions from or directly from
object.BaseException - ,
== None, or== True.is "some literal" - Bare or
except:that swallows control-flow signals.except BaseException: - outside a curated
from module import *with explicit__init__.py.__all__ - Unexplained numeric literals in business logic.
- Assigned-but-never-read locals left behind by a stale refactor.
- 自定义异常继承自或直接继承自
object。BaseException - 使用、
== None或== True。is "some literal" - 使用裸或
except:吞噬控制流信号。except BaseException: - 在未经过精心设计且未显式定义的
__all__中使用__init__.py。from module import * - 业务逻辑中出现未加解释的数值字面量。
- 因过时重构留下的已赋值但从未读取的局部变量。
Navigation
导航
- quality-antipatterns.md: Full non-compliant vs compliant examples and a "how to test" note for each of the six anti-patterns.
- quality-antipatterns.md:包含六大反模式的完整非合规与合规示例,以及每个反模式的「测试方法」说明。
Related Skills
相关技能
- pytest (): testing mechanics — fixtures, parametrization, mocking. Several anti-patterns here (broad
toolchains/python/testing/pytest, malformed exception classes) directly cause flaky tests.except - code-review-standards (): the project-wide, severity-tagged review checklist that incorporates equivalents of these.
universal/process/code-review-standards - code-quality-scoring (): whole-codebase health and technical-debt scoring, rather than individual findings.
universal/quality/code-quality-scoring
- pytest():测试机制 —— 夹具、参数化、模拟。本技能中的部分反模式(宽泛的
toolchains/python/testing/pytest、格式错误的异常类)会直接导致测试不稳定。except - code-review-standards():项目级、带严重程度标记的审查清单,其中包含与本技能等效的内容。
universal/process/code-review-standards - code-quality-scoring():全代码库健康状况与技术债务评分,而非单个问题排查。
universal/quality/code-quality-scoring