Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Use this skill when reviewing Python code for common anti-patterns to avoid. Use as a checklist when reviewing code, before finalizing implementations, or when debugging issues that might stem from known bad practices.
.claude/skills/wshobson-python-anti-patterns/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-03 | ✗→✓ | ▲ Improved | 57% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 48% | 0% |
| case-04 | ✓→✓ | = Same ✓ | 80% | 0% |
| case-05 | ✓→✓ | = Same ✓ | 79% | 0% |
| case-06 | ✓→✓ | = Same ✓ | 47% | 0% |
A reference checklist of common mistakes and anti-patterns in Python code. Review this before finalizing implementations to catch issues early.
Note: This skill focuses on what to avoid. For guidance on positive patterns and architecture, see the python-design-patterns skill.
python# BAD: Timeout logic duplicated everywhere def fetch_user(user_id): try: return requests.get(url, timeout=30) except Timeout: logger.warning("Timeout fetching user") return None def fetch_orders(user_id): try: return requests.get(url, timeout=30) except Timeout: logger.warning("Timeout fetching orders") return None
Fix: Centralize in decorators or client wrappers.
python# GOOD: Centralized retry logic @retry(stop=stop_after_attempt(3), wait=wait_exponential()) def http_get(url: str) -> Response: return requests.get(url, timeout=30)
python# BAD: Retrying at multiple layers @retry(max_attempts=3) # Application retry def call_service(): return client.request() # Client also has retry configured!
Fix: Retry at one layer only. Know your infrastructure's retry behavior.
python# BAD: Secrets and config in code DB_HOST = "prod-db.example.com" API_KEY = "sk-12345" def connect(): return psycopg.connect(f"host={DB_HOST}...")
Fix: Use environment variables with typed settings.
python# GOOD from pydantic_settings import BaseSettings class Settings(BaseSettings): db_host: str = Field(alias="DB_HOST") api_key: str = Field(alias="API_KEY") settings = Settings()
python# BAD: Leaking ORM model to API @app.get("/users/{id}") def get_user(id: str) -> UserModel: # SQLAlchemy model return db.query(UserModel).get(id)
Fix: Use DTOs/response models.
python# GOOD @app.get("/users/{id}") def get_user(id: str) -> UserResponse: user = db.query(UserModel).get(id) return UserResponse.from_orm(user)
python# BAD: SQL embedded in business logic def calculate_discount(user_id: str) -> float: user = db.query("SELECT * FROM users WHERE id = ?", user_id) orders = db.query("SELECT * FROM orders WHERE user_id = ?", user_id) # Business logic mixed with data access if len(orders) > 10: return 0.15 return 0.0
Fix: Repository pattern. Keep business logic pure.
python# GOOD def calculate_discount(user: User, orders: list[Order]) -> float: # Pure business logic, easily testable if len(orders) > 10: return 0.15 return 0.0
python# BAD: Swallowing all exceptions try: process() except Exception: pass # Silent failure - bugs hidden forever
Fix: Catch specific exceptions. Log or handle appropriately.
python# GOOD try: process() except ConnectionError as e: logger.warning("Connection failed, will retry", error=str(e)) raise except ValueError as e: logger.error("Invalid input", error=str(e)) raise BadRequestError(str(e))
python# BAD: Stops on first error def process_batch(items): results = [] for item in items: result = process(item) # Raises on error - batch aborted results.append(result) return results
Fix: Capture both successes and failures.
python# GOOD def process_batch(items) -> BatchResult: succeeded = {} failed = {} for idx, item in enumerate(items): try: succeeded[idx] = process(item) except Exception as e: failed[idx] = e return BatchResult(succeeded, failed)
python# BAD: No validation def create_user(data: dict): return User(**data) # Crashes deep in code on bad input
Fix: Validate early at API boundaries.
python# GOOD def create_user(data: dict) -> User: validated = CreateUserInput.model_validate(data) return User.from_input(validated)
python# BAD: File never closed def read_file(path): f = open(path) return f.read() # What if this raises?
Fix: Use context managers.
python# GOOD def read_file(path): with open(path) as f: return f.read()
python# BAD: Blocks the entire event loop async def fetch_data(): time.sleep(1) # Blocks everything! response = requests.get(url) # Also blocks!
Fix: Use async-native libraries.
python# GOOD async def fetch_data(): await asyncio.sleep(1) async with httpx.AsyncClient() as client: response = await client.get(url)
python# BAD: No types def process(data): return data["value"] * 2
Fix: Annotate all public functions.
python# GOOD def process(data: dict[str, int]) -> int: return data["value"] * 2
python# BAD: Generic list without type parameter def get_users() -> list: ...
Fix: Use type parameters.
python# GOOD def get_users() -> list[User]: ...
python# BAD: Only tests success case def test_create_user(): user = service.create_user(valid_data) assert user.id is not None
Fix: Test error conditions and edge cases.
python# GOOD def test_create_user_success(): user = service.create_user(valid_data) assert user.id is not None def test_create_user_invalid_email(): with pytest.raises(ValueError, match="Invalid email"): service.create_user(invalid_email_data) def test_create_user_duplicate_email(): service.create_user(valid_data) with pytest.raises(ConflictError): service.create_user(valid_data)
python# BAD: Mocking everything def test_user_service(): mock_repo = Mock() mock_cache = Mock() mock_logger = Mock() mock_metrics = Mock() # Test doesn't verify real behavior
Fix: Use integration tests for critical paths. Mock only external services.
Before finalizing code, verify:
except Exception: pass| Anti-Pattern | Fix | |-------------|-----| | Scattered retry logic | Centralized decorators | | Hard-coded config | Environment variables + pydantic-settings | | Exposed ORM models | DTO/response schemas | | Mixed I/O + logic | Repository pattern | | Bare except | Catch specific exceptions | | Batch stops on error | Return BatchResult with successes/failures | | No validation | Validate at boundaries with Pydantic | | Unclosed resources | Context managers | | Blocking in async | Async-native libraries | | Missing types | Type annotations on all public APIs | | Only happy path tests | Test errors and edge cases |
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→fail | 12,676 | 8,125 | -36% | 1 | 1 | 0% | 2,821 | 4,075 | +44% | 0 | 0 | — |
case-02 | pass→pass | 11,968 | 8,456 | -29% | 1 | 1 | 0% | 2,829 | 4,173 | +48% | 0 | 0 | — |
case-03 | fail→pass | 11,920 | 7,506 | -37% | 1 | 1 | 0% | 2,268 | 3,564 | +57% | 0 | 0 | — |
case-04 | pass→pass | 8,096 | 4,796 | -41% | 1 | 1 | 0% | 1,707 | 3,071 | +80% | 0 | 0 | — |
case-05 | pass→pass | 9,177 | 7,694 | -16% | 1 | 1 | 0% | 2,021 | 3,611 | +79% | 0 | 0 | — |
case-06 | pass→pass | 11,068 | 6,849 | -38% | 1 | 1 | 0% | 2,515 | 3,686 | +47% | 0 | 0 | — |
case-07 | pass→pass | 9,469 | 7,816 | -17% | 1 | 1 | 0% | 1,935 | 3,311 | +71% | 0 | 0 | — |
case-08 | pass→pass | 10,685 | 8,667 | -19% | 1 | 1 | 0% | 2,243 | 4,133 | +84% | 0 | 0 | — |
case-09 | pass→pass | 8,483 | 4,712 | -44% | 1 | 1 | 0% | 1,835 | 3,064 | +67% | 0 | 0 | — |
case-10 | pass→pass | 68,793 | 6,890 | -90% | 1 | 1 | 0% | 1,636 | 3,633 | +122% | 0 | 0 | — |
case-11 | pass→pass | 8,537 | 6,639 | -22% | 1 | 1 | 0% | 1,833 | 3,502 | +91% | 0 | 0 | — |
case-20 | pass→pass | 13,688 | 11,180 | -18% | 1 | 1 | 0% | 2,934 | 4,697 | +60% | 0 | 0 | — |
case-12 | pass→pass | 5,595 | 6,228 | +11% | 1 | 1 | 0% | 1,266 | 3,508 | +177% | 0 | 0 | — |
case-13 | pass→pass | 10,708 | 8,242 | -23% | 1 | 1 | 0% | 2,064 | 3,901 | +89% | 0 | 0 | — |
case-14 | pass→pass | 13,431 | 16,752 | +25% | 1 | 1 | 0% | 2,700 | 5,406 | +100% | 0 | 0 | — |
case-15 | pass→pass | 12,397 | 12,591 | +2% | 1 | 1 | 0% | 2,247 | 4,386 | +95% | 0 | 0 | — |
case-21 | pass→pass | 13,904 | 11,828 | -15% | 1 | 1 | 0% | 3,074 | 4,693 | +53% | 0 | 0 | — |
case-16 | pass→pass | 10,218 | 7,602 | -26% | 1 | 1 | 0% | 2,033 | 3,608 | +77% | 0 | 0 | — |
case-17 | pass→pass | 9,310 | 8,393 | -10% | 1 | 1 | 0% | 1,544 | 3,552 | +130% | 0 | 0 | — |
case-18 | pass→pass | 15,274 | 8,233 | -46% | 1 | 1 | 0% | 2,601 | 3,887 | +49% | 0 | 0 | — |
case-19 | pass→pass | 7,167 | 4,490 | -37% | 1 | 1 | 0% | 1,202 | 3,022 | +151% | 0 | 0 | — |
case-22 | pass→pass | 17,147 | 14,911 | -13% | 1 | 1 | 0% | 3,557 | 5,193 | +46% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +5 percentage points is the difference between those two pass rates over the 22 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.