<< All versions
Skill v1.0.1
currentAutomated scan100/100majiayu000/claude-skill-registry/python-standards-akaszubski-anyclaude-local
3 files
──Details
PublishedMay 19, 2026 at 10:09 AM
Content Hashsha256:180c53b04f726d09...
Git SHAfa3abd30d36e
Bump Typepatch
──Files
Files (1 file, 9.7 KB)
SKILL.md9.7 KBactive
SKILL.md · 477 lines · 9.7 KB
version: "1.0.1" name: python-standards type: knowledge description: Python code quality standards (PEP 8, type hints, docstrings). Use when writing Python code. keywords: python, pep8, type hints, docstrings, black, isort, formatting auto_activate: true
Python Standards Skill
Python code quality standards for [PROJECT_NAME] project.
When This Activates
- Writing Python code
- Code formatting
- Type hints
- Docstrings
- Keywords: "python", "format", "type", "docstring"
Code Style (PEP 8 + Black)
Formatting
- Line length: 100 characters (black --line-length=100)
- Indentation: 4 spaces (no tabs)
- Quotes: Double quotes preferred
- Imports: Sorted with isort
Running Formatters
bash
# Blackblack --line-length=100 src/ tests/# isortisort --profile=black --line-length=100 src/ tests/# Combined (automatic via hooks)black src/ && isort src/
Type Hints (Required)
Function Signatures
python
from pathlib import Pathfrom typing import Optional, List, Dict, Union, Tupledef process_file(input_path: Path,output_path: Optional[Path] = None,*,max_lines: int = 1000,validate: bool = True) -> Dict[str, any]:"""Process file with type hints on all parameters and return."""pass
Generic Types
python
from typing import List, Dict, Set, Tuple, Optional, Union# Collectionsitems: List[str] = ["a", "b", "c"]mapping: Dict[str, int] = {"a": 1, "b": 2}unique: Set[int] = {1, 2, 3}pair: Tuple[str, int] = ("key", 42)# Optional (can be None)maybe_value: Optional[str] = None# Union (multiple types)flexible: Union[str, int] = "text"
Class Type Hints
python
from dataclasses import dataclassfrom typing import ClassVar@dataclassclass APIConfig:"""API configuration with type hints."""base_url: strapi_key: strtimeout: int = 30max_retries: int = 3enable_cache: bool = True# Class variableDEFAULT_TIMEOUT: ClassVar[int] = 30
Docstrings (Google Style)
Function Docstrings
python
def process_data(data: List[Dict],*,batch_size: int = 32,validate: bool = True) -> ProcessResult:"""Process data with validation and batching.This function processes input data in batches with optionalvalidation. It handles errors gracefully and provides detailed results.Args:data: Input data as list of dicts with 'id' and 'content' keysbatch_size: Number of items to process per batch (default: 32)validate: Whether to validate input data (default: True)Returns:ProcessResult containing processed items, errors, and metricsRaises:ValueError: If data is empty or invalid formatValidationError: If validation failsExample:>>> data = [{"id": 1, "content": "text"}]>>> result = process_data(data, batch_size=10)>>> print(result.success_count)1"""pass
Class Docstrings
python
class DataProcessor:"""Data processing orchestrator for batch operations.This class handles the complete data processing workflow includingvalidation, transformation, batching, and error handling.Args:config: Processing configurationbatch_size: Number of items per batchvalidate: Whether to validate input dataAttributes:config: Processing configurationbatch_size: Configured batch sizemetrics: Processing metrics trackerExample:>>> processor = DataProcessor(config, batch_size=100)>>> result = processor.process(input_data)>>> processor.save("results.json")"""def __init__(self,config: APIConfig,device: str = "gpu"):self.model_name = model_nameself.config = configself.device = device
Error Handling
Helpful Error Messages
python
# ✅ GOOD: Context + Expected + Docsdef load_config(path: Path) -> Dict:"""Load configuration file."""if not path.exists():raise FileNotFoundError(f"Config file not found: {path}\n"f"Expected YAML file with keys: model, data, training\n"f"See example: docs/examples/config.yaml\n"f"See guide: docs/guides/configuration.md")try:with open(path) as f:return yaml.safe_load(f)except yaml.YAMLError as e:raise ValueError(f"Invalid YAML in config file: {path}\n"f"Error: {e}\n"f"See guide: docs/guides/configuration.md")# ❌ BAD: Generic errordef load_config(path):if not path.exists():raise FileNotFoundError("File not found")
Custom Exceptions
python
class AppError(Exception):"""Base exception for application."""passclass ConfigError(AppError):"""Configuration error."""passclass ValidationError(AppError):"""Validation error."""pass# Usagedef validate_config(config: Dict) -> None:"""Validate configuration."""required = ["database", "api_key", "settings"]missing = [k for k in required if k not in config]if missing:raise ConfigError(f"Missing required config keys: {missing}\n"f"Required: {required}\n"f"See: docs/guides/configuration.md")
Code Organization
Imports Order (isort)
python
# 1. Standard libraryimport osimport sysfrom pathlib import Path# 2. Third-partyimport [framework].core as mximport numpy as npfrom anthropic import Anthropic# 3. Localfrom [project_name].core.trainer import Trainerfrom [project_name].utils.config import load_config
Function/Class Organization
python
class Model:"""Model class."""# 1. Class variablesDEFAULT_LR = 1e-4# 2. __init__def __init__(self, name: str):self.name = name# 3. Public methodsdef train(self, data: List) -> None:"""Public training method."""pass# 4. Private methodsdef _prepare_data(self, data: List) -> List:"""Private helper method."""pass# 5. Properties@propertydef num_parameters(self) -> int:"""Number of trainable parameters."""return sum(p.size for p in self.parameters())
Naming Conventions
python
# Classes: PascalCaseclass ModelTrainer:pass# Functions/variables: snake_casedef train_model():training_data = []# Constants: UPPER_SNAKE_CASEMAX_SEQUENCE_LENGTH = 2048DEFAULT_LEARNING_RATE = 1e-4# Private: _leading_underscoredef _internal_helper():pass_internal_cache = {}
Best Practices
Use Keyword-Only Arguments
python
# ✅ GOOD: Clear, prevents positional errorsdef train(data: List,*,learning_rate: float = 1e-4,batch_size: int = 32):pass# Must use: train(data, learning_rate=1e-3)# ❌ BAD: Easy to mix up argumentsdef train(data, learning_rate=1e-4, batch_size=32):pass
Use Pathlib
python
from pathlib import Path# ✅ GOOD: Pathlibconfig_path = Path("config.yaml")if config_path.exists():content = config_path.read_text()# ❌ BAD: String pathsimport osif os.path.exists("config.yaml"):with open("config.yaml") as f:content = f.read()
Use Context Managers
python
# ✅ GOOD: Automatic cleanupwith open(path) as f:data = f.read()# ✅ GOOD: Custom context managerfrom contextlib import contextmanager@contextmanagerdef training_context():"""Setup/teardown for training."""setup_training()try:yieldfinally:cleanup_training()
Use Dataclasses
python
from dataclasses import dataclass, fieldfrom typing import List@dataclassclass Config:"""Configuration with dataclass."""model_name: strlearning_rate: float = 1e-4epochs: int = 3tags: List[str] = field(default_factory=list)def __post_init__(self):"""Validate after initialization."""if self.learning_rate <= 0:raise ValueError("Learning rate must be positive")# Usageconfig = Config(model_name="model", tags=["test"])
Code Quality Checks
Flake8 (Linting)
bash
flake8 src/ --max-line-length=100
MyPy (Type Checking)
bash
mypy src/[project_name]/
Coverage
bash
pytest --cov=src/[project_name] --cov-fail-under=80
File Organization
src/[project_name]/├── __init__.py # Package init├── core/ # Core functionality│ ├── __init__.py│ ├── trainer.py│ └── model.py├── backends/ # Backend implementations│ ├── __init__.py│ ├── mlx_backend.py│ └── pytorch_backend.py├── cli/ # CLI tools│ ├── __init__.py│ └── main.py└── utils/ # Utilities├── __init__.py├── config.py└── logging.py
Anti-Patterns to Avoid
python
# ❌ BAD: No type hintsdef process(data, config):pass# ❌ BAD: No docstringdef train_model(data, lr=1e-4):pass# ❌ BAD: Unclear namesdef proc(d, c):x = d['k']return x# ❌ BAD: Mutable default argumentdef add_item(items=[]):items.append("new")return items# ❌ BAD: Generic exceptiontry:process()except:pass
Key Takeaways
- Type hints - Required on all public functions
- Docstrings - Google style, with examples
- Black formatting - 100 char line length
- isort imports - Sorted and organized
- Helpful errors - Context + expected + docs link
- Pathlib - Use Path not string paths
- Keyword args - Use \* for clarity
- Dataclasses - For configuration objects