templates/skills/languages/python/SKILL.md
Execute these commands after EVERY implementation (see AGENT_AUTOMATION module for full workflow).
npx skillsauth add hivellm/rulebook PythonInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
CRITICAL: Execute these commands after EVERY implementation (see AGENT_AUTOMATION module for full workflow).
# Complete quality check sequence:
ruff format --check . # Format check
ruff check . # Linting
mypy . # Type checking
pytest # All tests (100% pass required)
pytest --cov # Coverage check (95%+ required)
# Security audit:
pip-audit # Vulnerability scan
pip list --outdated # Check outdated deps
CRITICAL: Use Python 3.11+ for modern features and performance.
ruff format (fast, modern) or black (traditional)Configuration in pyproject.toml:
[tool.ruff]
line-length = 100
target-version = "py311"
[tool.ruff.format]
quote-style = "double"
indent-style = "space"
ruff check (fast, comprehensive) or ruff + flake8Configuration in pyproject.toml:
[tool.ruff.lint]
select = ["E", "F", "I", "N", "W", "UP", "B", "A", "C4", "SIM"]
ignore = ["E501"] # Line too long (handled by formatter)
[tool.ruff.lint.per-file-ignores]
"tests/*" = ["S101"] # Allow assert in tests
mypy for static type checkingtyping module for complex typesConfiguration in pyproject.toml:
[tool.mypy]
python_version = "3.11"
strict = true
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
Example:
from typing import Optional, List, Dict, Any
def process_data(
input_data: str,
options: Optional[Dict[str, Any]] = None
) -> List[str]:
"""Process input data and return results."""
# Implementation
return []
/tests directory@pytest.mark.parametrize for multiple test casesExample test structure:
import pytest
from mymodule import process_data
@pytest.fixture
def sample_data():
"""Provide sample data for tests."""
return "test input"
def test_process_data_valid_input(sample_data):
"""Test process_data with valid input."""
result = process_data(sample_data)
assert result == ["expected"]
@pytest.mark.parametrize("input_val,expected", [
("hello", ["HELLO"]),
("world", ["WORLD"]),
])
def test_process_data_parametrized(input_val, expected):
"""Test multiple input cases."""
result = process_data(input_val)
assert result == expected
CRITICAL: Tests must be categorized based on execution time and dependencies.
Tests that require active servers, databases, or external services must be isolated using pytest markers.
Implementation:
import pytest
import os
# Regular fast test (always runs)
def test_local_computation():
"""Fast test, no external dependencies."""
result = compute_locally("input")
assert result == "expected"
# S2S test (only runs with -m s2s)
@pytest.mark.s2s
def test_database_connection():
"""Requires active database server."""
db = connect_to_database()
# ... test implementation
@pytest.mark.s2s
def test_api_integration():
"""Requires active API server."""
client = create_api_client()
# ... test implementation
pytest.ini or pyproject.toml:# pytest.ini
[pytest]
markers =
s2s: Server-to-server tests requiring active services
slow: Slow tests taking > 20 seconds
Or in pyproject.toml:
[tool.pytest.ini_options]
markers = [
"s2s: Server-to-server tests requiring active services",
"slow: Slow tests taking > 20 seconds",
]
# Regular tests (excludes S2S)
pytest
# Include S2S tests (requires active servers)
pytest -m s2s
# Run all tests including S2S
pytest -m "not slow" # Fast + S2S, excludes slow
Tests that take > 10-20 seconds must be marked and run separately.
Implementation:
import pytest
# Fast test (always runs)
def test_quick_operation():
"""Completes in < 1 second."""
result = quick_compute("input")
assert result == "expected"
# Slow test (only runs with -m slow)
@pytest.mark.slow
def test_heavy_computation():
"""Takes 30+ seconds."""
# Heavy processing, large dataset, etc.
result = process_large_dataset()
assert result is not None
@pytest.mark.slow
def test_large_file_processing():
"""Processes large files, takes > 20 seconds."""
result = process_file("large_file.dat")
assert result.success
# Regular tests (excludes slow and S2S)
pytest -m "not slow and not s2s"
# Include slow tests
pytest -m slow
# Run all tests
pytest -m "" # Empty marker means all tests
# conftest.py
import pytest
@pytest.fixture(autouse=True)
def configure_timeouts(request):
"""Configure timeouts based on test markers."""
if 'slow' in request.keywords:
request.node.add_marker(pytest.mark.timeout(300)) # 5 minutes
elif 's2s' in request.keywords:
request.node.add_marker(pytest.mark.timeout(60)) # 1 minute
else:
request.node.add_marker(pytest.mark.timeout(20)) # 20 seconds
pyproject.toml or setup.py:[tool.poetry.scripts]
test = "pytest -m 'not slow and not s2s'"
test-s2s = "pytest -m s2s"
test-slow = "pytest -m slow"
test-all = "pytest"
@pytest.mark.timeout(60)@pytest.mark.s2s and @pytest.mark.slow@pytest.mark.skipif(not os.getenv('RUN_S2S_TESTS'), reason='S2S tests disabled')CRITICAL: Use modern dependency management tools.
[tool.poetry]
name = "myproject"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]
[tool.poetry.dependencies]
python = "^3.11"
requests = "^2.31.0"
[tool.poetry.group.dev.dependencies]
pytest = "^7.4.0"
mypy = "^1.5.0"
ruff = "^0.1.0"
Commands:
poetry install # Install dependencies
poetry add requests # Add dependency
poetry add --group dev pytest # Add dev dependency
poetry update # Update dependencies
# requirements.in
requests>=2.31.0
pydantic>=2.0.0
# requirements-dev.in
-r requirements.in
pytest>=7.4.0
mypy>=1.5.0
Commands:
pip-compile requirements.in
pip-compile requirements-dev.in
pip-sync requirements-dev.txt
Check for latest versions:
pip index versions <package>Version pinning:
>=1.0,<2.0)except:Example:
class ValidationError(Exception):
"""Raised when data validation fails."""
def __init__(self, message: str, field: str):
super().__init__(message)
self.field = field
def validate_data(data: dict[str, Any]) -> None:
"""
Validate input data.
Args:
data: The data to validate
Raises:
ValidationError: If validation fails
"""
if not isinstance(data, dict):
raise ValidationError("Data must be a dictionary", "data")
Example (Google style):
def process_data(input_data: str, options: dict[str, Any] | None = None) -> list[str]:
"""
Process input data and return results.
Args:
input_data: The input string to process
options: Optional processing options
Returns:
A list of processed strings
Raises:
ValidationError: If input_data is empty
Examples:
>>> process_data("hello")
['HELLO']
>>> process_data("world", {"lowercase": True})
['world']
"""
# Implementation
return []
project/
├── pyproject.toml # Project metadata and dependencies
├── README.md # Project overview (allowed in root)
├── CHANGELOG.md # Version history (allowed in root)
├── AGENTS.md # AI assistant rules (allowed in root)
├── LICENSE # Project license (allowed in root)
├── CONTRIBUTING.md # Contribution guidelines (allowed in root)
├── CODE_OF_CONDUCT.md # Code of conduct (allowed in root)
├── SECURITY.md # Security policy (allowed in root)
├── src/
│ └── mypackage/
│ ├── __init__.py
│ ├── module.py
│ └── py.typed # PEP 561 marker for type hints
├── tests/ # Test files
│ ├── __init__.py
│ └── test_module.py
└── docs/ # Documentation
asyncio for async codeasync def func() -> Coroutinepytest-asyncioExample:
import asyncio
from typing import List
async def fetch_data(url: str) -> dict[str, Any]:
"""Fetch data asynchronously."""
# Implementation
return {}
async def main() -> None:
"""Main async function."""
results = await asyncio.gather(
fetch_data("url1"),
fetch_data("url2"),
)
print(results)
if __name__ == "__main__":
asyncio.run(main())
Must include GitHub Actions workflows for:
Testing (python-test.yml):
Linting (python-lint.yml):
ruff format --check .ruff check .mypy .Security (python-security.yml):
pip-auditsafety checkPrerequisites:
pyproject.toml Configuration:
[build-system]
requires = ["setuptools>=68.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "your-package-name"
version = "1.0.0"
description = "A short description of your package"
readme = "README.md"
requires-python = ">=3.11"
license = {text = "MIT"}
authors = [
{name = "Your Name", email = "[email protected]"}
]
keywords = ["your", "keywords"]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
dependencies = [
"requests>=2.31.0",
]
[project.optional-dependencies]
dev = [
"pytest>=7.4.0",
"pytest-cov>=4.1.0",
"ruff>=0.1.0",
"mypy>=1.7.0",
"black>=23.12.0",
]
[project.urls]
Homepage = "https://github.com/your-org/your-package"
Documentation = "https://your-package.readthedocs.io"
Repository = "https://github.com/your-org/your-package"
"Bug Tracker" = "https://github.com/your-org/your-package/issues"
[tool.setuptools.packages.find]
where = ["src"]
[tool.setuptools.package-data]
your_package = ["py.typed"]
CRITICAL: Package names must be normalized according to PEP 625.
PyPI requires source distribution filenames to use normalized package names (underscores instead of hyphens).
Naming Rules:
Package name in pyproject.toml: Use underscores (_)
[project]
name = "my_package_name" # Correct
# NOT: name = "my-package-name" # Will cause deprecation warning
Package directory: Must match with underscores
src/
└── my_package_name/ # Correct
├── __init__.py
└── ...
Import statement: Uses underscores
import my_package_name
from my_package_name import something
Distribution filename: Will be my_package_name-1.0.0.tar.gz ✅
Common Issue:
If you use hyphens in the package name, PyPI will reject new uploads:
# ❌ WRONG - Will fail PEP 625 compliance
[project]
name = "my-package-name"
# Result: my-package-name-1.0.0.tar.gz (non-compliant)
# PyPI Error: "Filename does not contain normalized project name"
Correct Approach:
# ✅ CORRECT - PEP 625 compliant
[project]
name = "my_package_name"
# Result: my_package_name-1.0.0.tar.gz (compliant)
# PyPI: Accepts upload without warnings
Migration from Hyphenated Names:
If you previously published with hyphens:
pyproject.toml and setup.py to use underscorespip install my-package-name → works (auto-normalized)pip install my_package_name → works (canonical form)import my_package_nameReference: PEP 625 - File name of a Source Distribution
Publishing Workflow:
ruff check .
ruff format --check .
mypy .
pytest
python -m build
twine check dist/*
twine upload --repository testpypi dist/*
git tag v1.0.0 && git push --tagstwine upload dist/*Publishing Checklist:
pytest)mypy .)ruff check .)ruff format .)py.typed marker for type hintspython -m build)twine check dist/*)check-manifest)my_package-1.0.0.tar.gz (underscores) ✅Trusted Publishing (Recommended):
PyPI trusted publishing eliminates the need for API tokens:
Go to PyPI → Your Account → Publishing
Add a new pending publisher:
your-package-nameyour-github-orgyour-repo-namepython-publish.ymlrelease (optional)GitHub Actions will authenticate automatically using OIDC
Versioning:
Use semantic versioning and consider:
bump2version or setuptools_scmsetuptools_scm in pyproject.toml:[build-system]
requires = ["setuptools>=68.0", "setuptools_scm>=8.0"]
[tool.setuptools_scm]
version_file = "src/your_package/_version.py"
Type Hints:
Include py.typed marker for PEP 561 compliance:
touch src/your_package/py.typed
This tells type checkers your package includes type information.
<!-- PYTHON:END -->research
Author a rulebook task spec interactively — research, draft, ask the user clarifying questions, confirm, then create the tasks in rulebook ready for /rulebook-driver. Use when the user wants to plan/spec a feature before implementing.
development
Behavioral guidelines to reduce common LLM coding mistakes — overcomplication, sloppy refactors, hidden assumptions, weak goals. Use when writing, reviewing, or refactoring code. Auto-applies; invoke explicitly via /karpathy-guidelines or 'follow karpathy discipline'.
data-ai
Autonomous AI agent loop for iterative task implementation (@hivehub/rulebook ralph)
data-ai
Use SQL Server for enterprise relational data storage with advanced features, high availability, and Windows integration.