type-safety-mastery
Master type safety patterns - fix type errors at source, never use type: ignore or any, prefer Pydantic models, use type stubs for external libraries
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Master type safety patterns - fix type errors at source, never use type: ignore or any, prefer Pydantic models, use type stubs for external libraries
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Master code organization - encapsulation with wrapper methods, file modularity (split >150 lines), one function one responsibility, descriptive naming, minimal comments
Use when starting work - guidelines for asking questions and commit policies
Use when adding error handling - exception patterns, HTTP codes, domain exceptions
Use when adding logging to features - structured logging with LoggerService categories
Use when exiting plan mode - where to save implementation plans
Apply Martin Fowler's refactoring patterns - extract variables to eliminate repetition, split temporary variables, replace temp with query for cleaner, more maintainable code
| name | type-safety-mastery |
| description | Master type safety patterns - fix type errors at source, never use type: ignore or any, prefer Pydantic models, use type stubs for external libraries |
Fix type errors at their source—never use # type: ignore to bypass warnings. When ty reports an error:
Never use # type: ignore[return-value] or any specific type ignore comments. If a function's return type doesn't match:
Never use any type. It defeats the purpose of type checking. Instead:
Prefer Optional[T] over T | None:
Optional[str] instead of str | NoneMinimize use of cast and runtime workarounds:
cast(Type, val) to silence type errors when possiblegetattr(obj, 'attr') or setattr(obj, 'attr', val) to bypass missing type definitionstypings/ insteadcast is unavoidable (for example, dynamically resolved methods), add a short comment explaining whyUse Pydantic models instead of TypedDict for data structures:
Always prefer BaseModel over Dict/TypedDict. If you catch yourself annotating a variable as dict[str, X] (or using TypedDict) inside application code, extract a dedicated Pydantic model instead and expose helper methods (get_size(), set_size(), etc.) so callers never need .get() accessors.
Bad example:
from typing import TypedDict
class LoRAData(TypedDict):
id: int
name: str
weight: float
Good example:
from pydantic import BaseModel, Field
class LoRAData(BaseModel):
id: int = Field(..., description='Database ID')
name: str = Field(..., description='Display name')
weight: float = Field(..., ge=0.0, le=2.0, description='Weight/strength')
Type aliases for complex types: Create type aliases for frequently used complex types to improve readability.
Bad example:
def list_files(
self, id: str,
repo_info: Optional[Union[ModelInfo, DatasetInfo, SpaceInfo]] = None
) -> List[str]:
pass
Good example:
# Define type alias once at module level
RepoInfo = Union[ModelInfo, DatasetInfo, SpaceInfo]
def list_files(self, id: str, repo_info: Optional[RepoInfo] = None) -> List[str]:
pass
Use type stubs (.pyi files) for external library types:
typings/{package_name}/ instead of runtime wrapper classes.pyi extension)pyproject.toml under [tool.ty.src]assert isinstance(...)) to force typesTYPE_CHECKING. If you have to use it, then you did it wrong. Go back and find the root cause, then fix itUse public interfaces by default (lock, set_state()) and reserve underscores for truly private implementation details.