What does this skill do?

The Python Skill Patterns provides a set of idiomatic patterns and best practices for writing Python code that is readable, explicit, efficient, and maintainable. It covers native type hints in Python 3.9+, error handling with custom hierarchies, context managers for resources, and generators for large datasets—all in line with PEP 8 standards and modern language conventions.

New code written from scratch
Write functions and modules with complete type hints, dataclasses with validation, and standard package structure right from the start.
Code Review
Identify anti-patterns during the review: overly broad exceptions, lack of type hints, string concatenation in loops, or the absence of context managers.
Legacy Refactoring
Modernize legacy code to meet current standards: add type annotations, replace generic error handling with custom hierarchies, and optimize memory usage with generators.
Reusable Packaging Design
Create modules and packages using pyproject.toml, __slots__ for classes with many instances, and clear export conventions.

Usage examples

✍️ Write new code
Create a User dataclass with email validation in __post_init__ and full type hints using Python 3.9+.
🔍 Review anti-patterns
Review this Python module and identify anti-patterns: overly broad exceptions, a lack of type hints, and string concatenation in loops.
🔧 Refactoring legacy code
Refactor this function to use context managers, specific exceptions chained with `from`, and generators if there is a large amount of data.
⚙️ Configure validation
Generate a pyproject.toml file with Black, Ruff, and MyPy configurations for a modern Python package.

Features

Native type hints 3.9+ Define type signatures using `list[str]`, `dict[str, int]`, and `typing` for earlier versions, ensuring compatibility and clarity.
Specific Error Handling It catches specific exceptions, chains them using `from`, and defines its own hierarchies, such as `AppError → ConfigError`.
Context managers for resources Use `with` for files, connections, and transactions, including custom context managers with `contextlib`.
Memory Optimization Use generators for large datasets, __slots__ for classes with many instances, and str.join instead of string concatenation in loops.
Automatic validation Integrate Black, Ruff, MyPy, and PyTest into your workflow to ensure error-free code before committing changes.

Frequently asked questions

The skill prioritizes native Python 3.9+ type hints (list[str], dict[str, int]). For earlier versions, you can use the `typing` module as an alternative.
The skill recommends running `black` for formatting, `ruff` for linting, `mypy` for type checking, and `pytest` for testing before committing any changes.
Yes. The skill is designed to modernize legacy code by adding type hints, replacing generic error handling with custom hierarchies, and optimizing memory usage with generators.
Yes. All patterns and examples follow PEP 8 standards and modern programming conventions, prioritizing readability, clarity, and the principle of least surprise.
Python Patterns — Idiomatic and Robust Code with Claude AI

¿Prefieres escuchar el contenido? Genera la narración de audio con un clic.