Loading...
Loading...
Generates production-ready Python code following PEP standards, type hints, and clean code principles. Use when user requests Python implementation of classes, functions, services, or modules. Triggers on "implement", "write", "Python code", "create class", "create function", "code for".
npx skill4agent add josemartinrodriguezmortaloni/opencode-config generating-python-code1. CHECK PREREQUISITES → Verify design exists if Medium+
2. APPLY STANDARDS → Type hints, docstrings, naming
3. IMPLEMENT → Write code following patterns
4. VALIDATE → Run validate_structure.py
5. SECURE → Use securing-code skill before deliverydesigning-systems# RIGHT - Imports at the top, organized
"""Module docstring."""
from __future__ import annotations
import os
import sys
from typing import Protocol
from third_party import SomeClass
from myproject.domain import Entity
def my_function():
...# WRONG - Imports scattered in code
def process_data():
import json # NEVER import inside functions
...
def another_function():
try:
from module import thing # NEVER import inside try/except
except ImportError:
thing = None__future__# WRONG
def process(data):
return data.upper()
# RIGHT
def process(data: str) -> str:
return data.upper()def calculate_total(items: list[LineItem], tax_rate: Decimal) -> Money:
"""Calculate order total including tax.
Args:
items: Line items with price and quantity
tax_rate: Tax rate as decimal (0.21 for 21%)
Returns:
Total amount as Money value object
Raises:
InvalidTaxRateError: If tax_rate is negative
"""| Element | Convention | Example |
|---|---|---|
| Classes | PascalCase, noun | |
| Functions | snake_case, verb+noun | |
| Constants | UPPER_SNAKE | |
| Private | _prefix | |
datainfomanagerhandlerutilhelpermiscprocess# WRONG - Silent failure
def get_user(user_id: str) -> User | None:
user = db.find(user_id)
return user # Caller might forget to check None
# RIGHT - Explicit failure
def get_user(user_id: str) -> User:
user = db.find(user_id)
if not user:
raise UserNotFoundError(f"User {user_id} not found")
return user| Pattern | Why Bad | Fix |
|---|---|---|
| Swallows all errors | Catch specific exceptions |
| Caller forgets to check | Raise domain exception |
| Type-based branching | Use polymorphism |
| Global mutable state | Untestable, race conditions | Inject dependencies |
| String concatenation for SQL | SQL injection | Use parameterized queries |
| Hard-coded config values | Inflexible | Use environment/config |
python scripts/validate_structure.py generated_file.py"""
Module docstring explaining purpose.
"""
from __future__ import annotations
from typing import Protocol
from dataclasses import dataclass
# Interfaces first
class IRepository(Protocol):
...
# Value Objects
@dataclass(frozen=True)
class Money:
...
# Domain classes
class Service:
...validate_structure.pysecuring-code