python-simplify
Simplify existing Python code
- License 许可
- MIT
- 所属仓库
- Shozye/skills
- 最近更新
- 2026-09-11
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- python-simplify
- license
- MIT
- description
- Simplify existing Python code
03 SKILL.md 正文
Python Simplification
Governing goal
Make one small, evidence-based simplification at a time. Code upgrades are also 'evidence-based' simplifications, so this skill describes good and bad behaviors.
Scope
- Reusing existing project helpers
- Inlining trivial indirection, removing unnecessary duplication, and simplifying control flow or data handling.
- Improving names, local structure, and readability where behavior remains equivalent.
- others
Decision rules
-
Treat fewer lines as a possible result, not the objective; clarity and risk reduction decide.
-
When Pydantic model validates something, there is no need to validate it later itself.
-
Do not create variables that are unnecessary. For example
total = page.totalis unnecessary. Usepage.total
AntiPatterns
Those are in general examples and rules of what to not follow with explanations what is better.
1. Pydantic Antipattern - Everything as None
class Detail(BaseModel):
location: str | None = None
description: str | None = None
This is an antipattern, much better would be
class Detail(BaseModel):
location: str
description: str
It is good to make validation as fast as possible. Only use the None when it really is possible that the API will give you None.
2. Pydantic Antipattern - Field validator for strings
class Offer(BaseModel):
url: str
@field_validator("url")
@classmethod
def require_nonblank_text(cls, value: str) -> str:
if not value.strip():
raise ValueError("value must not be blank")
return value
It is much better to use StrictStr and StringConstraints