Skip to main content
skill CX Codex

python · martinholovsky — Claude Code skill: what it does, how to install

A backend skill for Python work: type hints, asyncio, input validation and pytest. The agent writes the failing test before the code.

Unlicense 229 installs ★ 48 updated 2025-12-06

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add martinholovsky/claude-skills-generator --skill python -a claude-code
Open on GitHub

What python does

The skill puts the agent in the seat of a Python backend developer: annotations, async code, input validation, tests. Work follows a test-first loop — write a failing pytest case, make it pass with the smallest change, refactor. Before handing anything over it runs pytest --cov, mypy --strict, bandit -r src/ -ll, pip-audit and safety check.

A decision table covers the usual traps: user input goes through Pydantic, database calls stay parameterized, subprocesses take an argument list instead of shell=True, secrets come from the environment. Performance notes cover asyncio.gather for parallel requests, generators over large files, a SQLAlchemy connection pool and batched inserts.

The skill marks itself HIGH-RISK and splits the material: principles in SKILL.md, CVE write-ups and OWASP material in references/security-examples.md, advanced patterns and a threat model in their own files. Only SKILL.md reaches the agent by default, so for one specific vulnerability you hand over the matching reference file yourself. Examples were run on Python 3.11+, with 3.9 as the floor.

What it helps with

  • write the failing pytest case before the implementation
  • swap a shell=True subprocess call for an argument list
  • run requests concurrently with asyncio.gather
  • stream a large file through a generator instead of loading it
  • wire mypy --strict and bandit into the check step

Example prompts for the agent

  • Add a GET /users/{user_id} endpoint on FastAPI, test first.
  • Audit this module for unvalidated input and SQL built from strings.
  • Rewrite the log loader as a generator, the file is large.

How to install python

1Skills CLI (Vercel)

npx skills add martinholovsky/claude-skills-generator --skill python -a claude-code

2Manually: put the files in place

~/.claude/skills/python/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/python/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about python

How do I install the python skill in Claude Code?

Run npx skills add martinholovsky/claude-skills-generator --skill python -a claude-code. The skill lands in ~/.claude/skills/python/SKILL.md.

What does the python skill do?

It steers the agent through Python backend work: tests before code, type hints, safe handling of input, databases and subprocesses, plus speed-ups with asyncio and generators.

Which Python version does it target?

Examples were run on Python 3.11+, and 3.9 is the minimum it supports.

Source text

SKILL.md · Unlicense
---
name: python
description: Backend services development with Python emphasizing security, performance, and maintainability for JARVIS AI Assistant
model: sonnet
risk_level: HIGH
---

# Python Backend Development Skill

## File Organization

This skill uses a split structure for HIGH-RISK requirements:
- **SKILL.md**: Core principles, patterns, and essential security (this file)
- **references/security-examples.md**: Complete CVE details and OWASP implementations
- **references/advanced-patterns.md**: Advanced Python patterns and optimization
- **references/threat-model.md**: Attack scenarios and STRIDE analysis

## Validation Gates

| Gate | Status | Notes |
|------|--------|-------|
| 0.1 Domain Expertise | PASSED | Type safety, async, security, testing |
| 0.2 Vulnerability Research | PASSED | 5+ CVEs documented (2025-11-20) |
| 0.5 Hallucination Check | PASSED | Examples tested on Python 3.11+ |
| 0.11 File Organization | Split | HIGH-RISK, ~450 lines + references |

---

## 1. Overview

**Risk Level**: HIGH

**Justification**: Python backend services handle authentication, database access, file operations, and external API communication. Vulnerabilities in input validation, deserialization, command execution, and cryptography can lead to data breaches and system compromise.

You are an expert Python backend developer specializing in secure, maintainable, and performant services.

### Core Expertise Areas
- Type annotations and runtime validation
- Async programming with asyncio
- Security: input validation, cryptography, secrets management
- Testing: pytest, property-based testing, security testing
- Database access with SQLAlchemy/asyncpg
- API development with FastAPI/Starlette

---

## 2. Core Responsibilities

### Fundamental Principles

1. **TDD First**: Write tests before implementation, design API through test cases
2. **Performance Aware**: Use async, generators, efficient data structures by default
3. **Type Safety**: Use type hints everywhere, validate at runtime boundaries
4. **Defense in Depth**: Multiple validation layers, fail securely
5. **Secure Defaults**: Use safe libraries, reject unsafe operations
6. **Explicit over Implicit**: Clear error handling, explicit dependencies
7. **Testability**: Design for testing, write security tests

### Decision Framework

| Situation | Approach |
|-----------|----------|
| User input | Validate with Pydantic, sanitize output |
| Database queries | Use ORM or parameterized queries, never format strings |
| File operations | Validate paths, use pathlib, check containment |
| Subprocess | Use list args, never shell=True with user input |
| Secrets | Load from environment or secret manager |
| Cryptography | Use cryptography library, never roll your own |

---

## 2.1 Implementation Workflow (TDD)

### Step 1: Write Failing Test First

```python
import pytest
from my_service import UserService, UserNotFoundError

class TestUserService:
    @pytest.mark.asyncio
    async def test_get_user_returns_user_when_exists(self, db_session):
        service = UserService(db_session)
        user_id = await service.create_user("alice", "[email protected]")
        user = await service.get_user(user_id)
        assert user.username == "alice"

    @pytest.mark.asyncio
    async def test_get_user_raises_when_not_found(self, db_session):
        service = UserService(db_session)
        with pytest.raises(UserNotFoundError):
            await service.get_user(99999)

    @pytest.mark.asyncio
    async def test_create_user_validates_email(self, db_session):
        service = UserService(db_session)
        with pytest.raises(ValueError, match="Invalid email"):
            await service.create_user("bob", "not-an-email")
```

### Step 2: Implement Minimum to Pass

```python
class UserNotFoundError(Exception): pass

class UserService:
    def __init__(self, db: AsyncSession):
        self.db = db

    async def get_user(self, user_id: int) -> User:
        user = await self.db.get(User, user_id)
        if not user:
            raise UserNotFoundError(f"User {user_id} not found")
        return user

    async def create_user(self, username: str, email: str) -> int:
        if "@" not in email:
            raise ValueError("Invalid email format")
        # ... minimal implementation to pass tests
```

### Step 3: Refactor if Needed

- Extract common patterns, add type hints, ensure errors don't leak internals

### Step 4: Run Full Verification

```bash
pytest --cov=src           # All tests pass
mypy src/ --strict         # Type check passes
bandit -r src/ -ll         # Security scan passes
pip-audit && safety check  # Dependencies clean
```

---

## 2.2 Performance Patterns

### Pattern 1: Async I/O with asyncio.gather

```python
# BAD: Sequential requests (slow)
for url in urls:
    response = await client.get(url)  # Waits for each one

# GOOD: Concurrent requests with gather
tasks = [client.get(url) for url in urls]
responses = await asyncio.gather(*tasks)  # All at once
```

### Pattern 2: Generators for Large Data Processing

```python
# BAD: Load all into memory
return [process(line) for line in f.readlines()]  # OOM risk

# GOOD: Generator yields one at a time
def process_large_file(filepath: str) -> Iterator[dict]:
    with open(filepath) as f:
        for line in f:
            yield process(line)  # Memory efficient
```

### Pattern 3: Efficient Data Structures

```python
# BAD: List for membership testing - O(n)
required in user_perms_list  # Slow for large lists

# GOOD: Set for membership testing - O(1)
required in user_perms_set  # Fast lookup

# BAD: Repeated string concatenation
result = ""; for f in fields: result += f + ", "  # Creates new string each time

# GOOD: Join for string building
", ".join(fields)  # Single allocation
```

### Pattern 4: Connection Pooling

```python
# BAD: New connection per request
engine = create_async_engine(DATABASE_URL)  # Connection overhead each time

# GOOD: Reuse pooled connections
engine = create_async_engine(DATABASE_URL, pool_size=20, max_overflow=10)
async_session = sessionmaker(engine, class_=AsyncSession)

async def get_user(user_id: int):
    async with async_session() as session:  # Reuses pooled connection
        return await session.get(User, user_id)
```

### Pattern 5: Batch Database Operations

```python
# BAD: Individual inserts (N round trips)
for user in users:
    db.add(User(**user)); await db.commit()  # N commits = slow

# GOOD: Batch insert (1 round trip)
stmt = insert(User).values(users)
await db.execute(stmt); await db.commit()  # Single commit

# GOOD: Chunked for very large datasets
for i in range(0, len(users), 1000):
    await db.execute(insert(User).values(users[i:i+1000]))
await db.commit()
```

---

## 3. Technical Foundation

### Version Recommendations

| Category | Version | Notes |
|----------|---------|-------|
| **LTS/Recommended** | Python 3.11+ | Performance improvements, better errors |
| **Minimum** | Python 3.9 | Security support until Oct 2025 |
| **Avoid** | Python 3.8- | EOL, no security patches |

### Security Dependencies

```toml
# pyproject.toml
[project]
dependencies = [
    "pydantic>=2.0", "email-validator>=2.0",      # Validation
    "cryptography>=41.0", "argon2-cffi>=21.0",    # Cryptography
    "PyJWT>=2.8", "sqlalchemy>=2.0", "asyncpg>=0.28",
    "httpx>=0.25", "bandit>=1.7",
]

[project.optional-dependencies]
dev = ["pytest>=7.0", "pytest-asyncio>=0.21", "hypothesis>=6.0", "safety>=2.0", "pip-audit>=2.0"]
```

---

## 4. Implementation Patterns

### Pattern 1: Type-Safe Input Validation

```python
from pydantic import BaseModel, Field, field_validator, EmailStr
from typing import Annotated
import re

class UserCreate(BaseModel):
    """Validated user creation request."""
    username: Annotated[str, Field(min_length=3, max_length=50)]
    email: EmailStr
    password: Annotated[str, Field(min_length=12)]

    @field_validator('username')
    @classmethod
    def validate_username(cls, v: str) -> str:
        if not re.match(r'^[a-zA-Z0-9_-]+$', v):
            raise ValueError('Username must be alphanumeric')
        return v

    @field_validator('password')
    @classmethod
    def validate_password_strength(cls, v: str) -> str:
        if not all([re.search(r'[A-Z]', v), re.search(r'[a-z]', v), re.search(r'\d', v)]):
            raise ValueError('Password needs uppercase, lowercase, and digit')
        return v
```

### Pattern 2: Secure Password Hashing

```python
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError

ph = PasswordHasher(time_cost=3, memory_cost=65536, parallelism=4)

def hash_password(password: str) -> str:
    return ph.hash(password)

def verify_password(password: str, hash: str) -> bool:
    try:
        ph.verify(hash, password)
        return True
    except VerifyMismatchError:
        return False
```

### Pattern 3: Safe Database Queries

```python
from sqlalchemy import select, text
from sqlalchemy.ext.asyncio import AsyncSession

# NEVER: f"SELECT * FROM users WHERE username = '{username}'"

async def get_user_safe(db: AsyncSession, username: str) -> User | None:
    stmt = select(User).where(User.username == username)
    result = await db.execute(stmt)
    return result.scalar_one_or_none()

async def search_users(db: AsyncSession, pattern: str) -> list:
    stmt = text("SELECT * FROM users WHERE username LIKE :pattern")
    result = await db.execute(stmt, {"pattern": f"%{pattern}%"})
    return result.fetchall()
```

### Pattern 4: Safe File Operations

```python
from pathlib import Path

def safe_read_file(base_dir: Path, user_filename: str) -> str:
    if '..' in user_filename or user_filename.startswith('/'):
        raise ValueError("Invalid filename")

    file_path = (base_dir / user_filename).resolve()
    if not file_path.is_relative_to(base_dir.resolve()):
        raise ValueError("Path traversal detected")

    return file_path.read_text()
```

### Pattern 5: Safe Subprocess Execution

```python
import subprocess

ALLOWED_PROGRAMS = {'git', 'python', 'pip'}

def run_command_safe(program: str, args: list[str]) -> str:
    if program not in ALLOWED_PROGRAMS:
        raise ValueError(f"Program not allowed: {program}")

    result = subprocess.run(
        [program, *args],
        capture_output=True, text=True, timeout=30, check=True,
    )
    return result.stdout
```

---

## 5. Security Standards

### 5.1 Domain Vulnerability Landscape

| CVE ID | Severity | Description | Mitigation |
|--------|----------|-------------|------------|
| CVE-2024-12718 | CRITICAL | tarfile filter bypass | Python 3.12.3+, filter='data' |
| CVE-2024-12254 | HIGH | asyncio memory exhaustion | Upgrade, monitor memory |
| CVE-2024-5535 | MEDIUM | SSLContext buffer over-read | Upgrade OpenSSL |
| CVE-2023-50782 | HIGH | RSA information disclosure | Upgrade cryptography |
| CVE-2023-27043 | MEDIUM | Email parsing vulnerability | Strict email validation |

> **See `references/security-examples.md` for complete CVE details and mitigation code**

### 5.2 OWASP Top 10 Mapping

| Category | Risk | Key Mitigations |
|----------|------|-----------------|
| A01 Broken Access Control | HIGH | Validate permissions, decorators |
| A02 Cryptographic Failures | HIGH | cryptography lib, Argon2 |
| A03 Injection | CRITICAL | Parameterized queries, no shell=True |
| A04 Insecure Design | MEDIUM | Type safety, validation layers |
| A05 Misconfiguration | HIGH | Safe defaults, audit deps |
| A06 Vulnerable Components | HIGH | pip-audit, safety in CI |

### 5.3 Essential Security Patterns

```python
from pydantic import BaseModel, field_validator
import os, logging

# Secure base model - reject unknown fields, strip whitespace
class SecureInput(BaseModel):
    model_config = {'extra': 'forbid', 'str_strip_whitespace': True}

    @field_validator('*', mode='before')
    @classmethod
    def reject_null_bytes(cls, v):
        if isinstance(v, str) and '\x00' in v:
            raise ValueError('Null bytes not allowed')
        return v

# Secrets from environment (NEVER hardcode)
API_KEY = os.environ["API_KEY"]
DB_URL = os.environ["DATABASE_URL"]

# Safe error handling - log details, return safe message
class AppError(Exception):
    def __init__(self, message: str, internal: str = None):
        self.message = message
        if internal:
            logging.error(f"{message}: {internal}")

    def to_response(self) -> dict:
        return {"error": self.message}
```

> **See `references/advanced-patterns.md` for secrets manager integration**

---

## 6. Testing & Validation

### Security Testing Commands

```bash
bandit -r src/ -ll          # Static analysis
pip-audit && safety check   # Dependency vulnerabilities
mypy src/ --strict          # Type checking
```

### Security Test Examples

```python
import pytest
from pathlib import Path

def test_sql_injection_prevented(db):
    for payload in ["'; DROP TABLE users; --", "' OR '1'='1", "admin'--"]:
        assert get_user_safe(db, payload) is None

def test_path_traversal_blocked():
    base = Path("/app/data")
    for attack in ["../etc/passwd", "..\\windows\\system32", "foo/../../etc/passwd"]:
        with pytest.raises(ValueError, match="traversal|Invalid"):
            safe_read_file(base, attack)

def test_command_injection_blocked():
    with pytest.raises(ValueError, match="not allowed"):
        run_command_safe("rm", ["-rf", "/"])
```

> **See `references/security-examples.md` for comprehensive test patterns**

---

## 7. Common Mistakes & Anti-Patterns

| Anti-Pattern | Bad | Good |
|-------------|-----|------|
| SQL formatting | `f"SELECT * WHERE id={id}"` | `select(User).where(User.id == id)` |
| Pickle untrusted | `pickle.loads(data)` | `json.loads(data)` |
| Shell injection | `subprocess.run(f"echo {x}", shell=True)` | `subprocess.run(["echo", x])` |
| Weak hashing | `hashlib.md5(pw).hexdigest()` | `PasswordHasher().hash(pw)` |
| Hardcoded secrets | `API_KEY = "sk-123..."` | `API_KEY = os.environ["API_KEY"]` |

---

## 8. Pre-Deployment Checklist

### Phase 1: Before Writing Code

- [ ] Requirements understood and documented
- [ ] API design reviewed (inputs, outputs, errors)
- [ ] Security threat model considered
- [ ] Test cases written first (TDD)
- [ ] Edge cases and error scenarios identified

### Phase 2: During Implementation

- [ ] Following TDD workflow (test -> implement -> refactor)
- [ ] Using performance patterns (async, generators, pooling)
- [ ] All inputs validated with Pydantic
- [ ] DB queries parameterized/ORM
- [ ] File ops check path containment
- [ ] Subprocess uses list args
- [ ] Passwords hashed with Argon2id
- [ ] Secrets from environment only

### Phase 3: Before Committing

- [ ] All tests pass: `pytest --cov=src`
- [ ] Type check passes: `mypy src/ --strict`
- [ ] Security scan passes: `bandit -r src/ -ll`
- [ ] Dependency audit passes: `pip-audit && safety check`
- [ ] No hardcoded secrets in code
- [ ] Errors don't leak internal details
- [ ] Debug mode disabled
- [ ] Logging configured (no PII/secrets)

---

## 9. Summary

Create Python code that is **type safe**, **secure**, **testable**, and **maintainable**.

**Security Essentials**:
1. Validate and sanitize all user input
2. Use parameterized queries for database ops
3. Never use shell=True with user input
4. Hash passwords with Argon2id
5. Load secrets from environment
6. Keep dependencies updated and audited

> **For attack scenarios and threat modeling, see `references/threat-model.md`**

© martinholovsky · Unlicense · martinholovsky/claude-skills-generator

Similar skills

All skills for Backend and APIs →

python by other authors comes first.

python Mindrally This skill tells your coding agent how to write Python: PEP 8, type hints, early returns, async for I/O. Useful when the agent edits or generates code in a Python repo. CX Codex +1 1,100installs python alinaqi Guides the agent through Python development using ruff, strict mypy types, and pytest coverage thresholds. CX Codex +1 282installs python pproenca Skill for Claude Code, Codex, Cursor and other agents with Python 3.11+ performance optimization rules. Triggers on asyncio, data structures, memory management, loops and strings. CX Codex +1 329installs python siviter-xyz Gives the agent one set of Python conventions: typing, dependency injection, module and test layout. Useful when edits land in an existing codebase. CX Codex +1 353installs python finom Turns Vovk.ts schemas into a typed Python package: RPC functions, TypedDict shapes, JSON Lines streaming. Useful when the API exists but the Python client does not. CX Codex +1 ★ 57repo stars python-pro VoltAgent Community python-pro — a subagent for type-safe Python code across web APIs, system utilities, and complex applications. Use it when async patterns and broad type coverage matter. CX Codex 11,689installs async-python-patterns Seth Hobson The async-python-patterns skill guides async Python work with asyncio, async/await, and concurrent tasks. Use it for APIs and applications with many I/O-bound operations. CX Codex +1 18,055installs python-patterns davila7 python-patterns is a Claude Code skill that guides Python system design, including framework choices, async versus sync execution, project structure, and Pydantic validation. CX Codex +1 1,135installs python-backend OrchestKit python-backend — a skill for Claude Code and Codex providing async Python patterns when editing *.py or package files. Enforces conventions for FastAPI, SQLAlchemy 2.0, and asyncio. CX Codex +1 250installs python-backend-expert platform-team python-backend-expert is a Claude Code skill for implementing FastAPI backends with SQLAlchemy 2.0 and Pydantic v2, including async routes, service layers, and Alembic migrations. CX Codex +1 138installs python-expert 0xfurai python-expert guides Claude Code to write idiomatic PEP 8 Python, build thorough pytest test suites, and enforce static typing with mypy. CX Codex ★ 1,029repo stars

From the same set50

The whole set →
llm-integration The llm-integration skill guides local LLM work with llama.cpp and Ollama, covering inference setup and prompt protection. Use it when model execution stays on your machine. CX Codex +1 239installs text-to-speech text-to-speech — a skill that walks the agent through Kokoro TTS: building a synthesis engine, streaming audio as it is generated, and clearing the files it writes. CX Codex +1 237installs browser-automation Directs agents to build browser automation with Playwright, CDP, and WebDriver using TDD and domain restrictions. CX Codex +1 235installs vue-nuxt vue-nuxt is a Claude Code skill for Vue 3 and Nuxt 3: it guides agents in building HUD components with sanitized input and server routes that keep secrets off the client. CX Codex +1 229installs prompt-engineering prompt-engineering is a Claude Code skill with rules and patterns for secure prompt building, injection defense, and LLM response validation. CX Codex +1 228installs graphql-expert graphql-expert is a Claude Code skill for designing GraphQL schemas, building resolvers, and working with federation. It activates for API development, Apollo Server tasks, and query performance work. CX Codex +1 220installs SQLCipher Encrypted Database Expert The SQLCipher Encrypted Database Expert skill guides SQLCipher database work, from key handling and rotation to SQLite migrations. Use it when building applications that store sensitive data locally. CX Codex +1 216installs typescript Enforces strict TypeScript conventions, Zod schema validation, and test-driven development in your coding agent. CX Codex +1 213installs

Project sponsors

Companies that keep this analytics open