LLMSkillHub
mirroredCopied from Taoidle/plan-cascade under MIT. Taoidle has not claimed this page, and is not affiliated with LLM SkillHub.

taoidle / python-best-practices

Python coding best practices. Use when writing or reviewing Python code. Covers type hints, error handling, and common patterns.

0
SKILL.md
1.8 KB

Python Best Practices

Code Style

RuleGuideline
Formatterruff format or black
Linterruff check with strict rules
Type hintsAlways for public APIs
DocstringsGoogle or NumPy style

Type Hints

def process(items: list[str], max_count: int = 10) -> dict[str, int]: ...
def find_user(user_id: int) -> User | None: ...

Error Handling

RuleGuideline
Specific exceptionsNever bare except:
Context managersUse with for resources
Early returnValidate inputs, fail fast
# Good
try:
    result = process(data)
except ValidationError as e:
    logger.warning(f"Validation failed: {e}")
    return default

Project Structure

src/package_name/
├── __init__.py
├── core/
└── py.typed
tests/
├── conftest.py
└── test_*.py
pyproject.toml

Common Patterns

PatternUsage
@dataclassData containers
@propertyComputed attributes
GeneratorsMemory-efficient iteration
functools.cacheMemoization

Anti-Patterns

AvoidUse Instead
Mutable default argsNone default, create inside
import *Explicit imports
Global stateDependency injection
# Bad: def add(item, items=[]): ...
# Good:
def add(item, items=None):
    items = items or []
    items.append(item)
    return items

Tools

ToolPurpose
pytestTesting
ruffLinting + formatting
mypyType checking
uvDependencies