python-best-practices

Applies Python design patterns for classes, typing, error handling, logging, and configuration.

Updated Apr 15, 2026
One-click install
npx skills add https://github.com/shoshoavi/agentic_worflows --skill python-best-practices-shoshoavi
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: python-best-practices
Source: https://github.com/shoshoavi/agentic_worflows/tree/main/cursor/skills/python-best-practices
Command: npx skills add https://github.com/shoshoavi/agentic_worflows --skill python-best-practices-shoshoavi

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Python codebases often accumulate inconsistent class designs, missing type hints, broad exception handling, and print-based debugging that make code hard to maintain and test. This Skill provides a concrete set of patterns to write clean, typed, and testable Python code. ## Core Features & Use Cases - Class Design Patterns: Guides use of dataclasses, frozen and slotted dataclasses, and Pydantic models for validation instead of boilerplate-heavy classes. - SOLID and Dependency Injection: Demonstrates single-responsibility class separation and constructor injection against abstract interfaces for testability. - Typing, Errors, and Logging: Covers modern type hints (generics, TypedDict, union syntax), custom domain exceptions, context managers for resources, and structured logging. - Use Case: When refactoring a service module, apply the dependency injection pattern with an abstract EmailSender so the UserService can be unit tested with a mock sender. ## Quick Start Review my Python module and refactor it to use dataclasses, type hints, custom exceptions, and proper logging following these best practices.

Frequently Asked Questions about python-best-practices

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write clean Python classes with less boilerplate?▼

Use the @dataclass decorator to auto-generate __init__, __repr__, and equality methods from type-annotated fields. Add frozen=True for immutability or slots=True to reduce memory usage by 40-50 percent.

When should I use Pydantic vs dataclasses in Python?▼

Use dataclasses for internal data containers and DTOs where validation is unnecessary. Use Pydantic BaseModel for API input/output where you need runtime validation, field constraints, and custom validators.

How do I make Python services testable with dependency injection?▼

Define an abstract interface with ABC, inject the dependency through the constructor, and depend on the abstraction. In tests, pass a mock implementation that records calls instead of performing real side effects.

Why should I use logging instead of print in Python?▼

The logging module supports severity levels, per-module loggers, structured extra fields, and centralized configuration at startup. Print statements cannot be filtered, redirected, or enriched with contextual metadata.

What is the right way to handle exceptions in Python?▼

Define custom exception classes carrying context like user_id or field names, keep try blocks narrow, and catch specific exceptions rather than broad Exception. Use context managers to guarantee resource cleanup on failure.