UV Python Execution & Type Checking
Activation
When this skill is triggered, ALWAYS display this banner first:
╭─────────────────────────────────────────────────────────────╮ │ 🐍 SKILL ACTIVATED: uv-python-execution │ ├─────────────────────────────────────────────────────────────┤ │ Mode: [Python Execution | Type Checking | Inline Script] │ │ Action: Using uv/ty toolchain instead of direct invocation │ ╰─────────────────────────────────────────────────────────────╯
Replace [Mode] with the detected trigger type.
When to Use
This skill activates automatically whenever:
Python Execution - Pattern matching on:
- •
python script.py - •
python3 script.py - •
python -m module - •
python3 -m module - •Any direct Python interpreter invocation
One-Time Scripts - Pattern matching on:
- •"write a quick script"
- •"make a standalone script"
- •"one-off script"
- •"throwaway script"
- •Scripts needing packages not in the project
Type Checking - Pattern matching on:
- •
pyright - •
mypy - •Any type checking operation
Rules
1. ALWAYS use uv run to execute Python scripts and modules.
2. ALWAYS use ty for type checking (not pyright or mypy directly).
Correct Usage - Python Execution
# Running a script uv run python script.py # Running a module uv run python -m pytest tests/ # Running with arguments uv run python train.py --epochs 100 --lr 1e-4 # Running inline Python uv run python -c "import torch; print(torch.cuda.is_available())"
Incorrect Usage - Python Execution (NEVER DO)
# Direct python invocation - WRONG python script.py python3 script.py python -m pytest ./script.py # if shebang points to python
Correct Usage - Type Checking
# Check entire project ty check # Check specific file ty check src/models/unet.py # Check specific directory ty check src/ # Check with specific options ty check --warn-unreachable src/
Incorrect Usage - Type Checking (NEVER DO)
# Using pyright directly - WRONG pyright src/ uv run pyright src/ # Using mypy directly - WRONG mypy src/ uv run mypy src/
Why
Python Execution with uv run
- •Dependency isolation:
uv runensures the script runs with the project's virtual environment and dependencies - •Reproducibility: Uses the exact versions locked in
uv.lock - •Consistency: All team members run scripts the same way
- •No activation needed: Works without manually activating the venv
Type Checking with ty
- •Speed:
tyis extremely fast (written in Rust by Astral, same team as uv/ruff) - •Unified toolchain: Part of the Astral ecosystem (uv, ruff, ty) for consistent tooling
- •Project-aware: Reads
pyproject.tomlconfiguration automatically - •Better defaults: Sensible defaults for modern Python projects
Exceptions
Python Execution
- •System Python checks:
python3 --versionto check system Python (but preferuv run python --version) - •Inside Docker/CI: When explicitly running inside a container with pre-configured environment
- •User explicitly requests: If user says "use python directly" or "don't use uv"
Type Checking
- •CI/pre-commit already configured: If CI uses pyright/mypy explicitly
- •User explicitly requests: If user says "use pyright" or "use mypy"
- •Compatibility checks: When verifying behavior matches pyright/mypy specifically
Implementation
Python Execution
When writing Bash commands that execute Python:
- •Prefix with
uv run: Always adduv runbeforepythonorpython3 - •Check existing scripts: If referencing a script in
scripts/, verify it usesuv runin justfile tasks - •For new scripts: Add corresponding justfile tasks that use
uv run
Type Checking
When checking types or fixing type errors:
- •Use
ty check: Runty checkinstead of pyright/mypy - •Target specific files: Use
ty check path/to/file.pyfor focused checking - •Iterative fixing: Run
ty check, fix errors, repeat until clean
Examples
Training a Model
# Correct uv run python scripts/train.py --config config.yaml # Also correct - using just task just train
Running Tests
# Correct uv run pytest tests/ -v # Also correct just test
Quick Python Check
# Correct uv run python -c "import torch; print(torch.__version__)"
Type Checking Before Fixing Code
# Check entire project for type errors ty check # Check specific module you're working on ty check src/models/ # Check a specific file after making changes ty check src/trainer/lightning_module.py
One-Time Scripts (Inline Dependencies)
When the user asks for a quick standalone script with external dependencies, use uv's inline script metadata (PEP 723). This runs scripts with their own dependencies without polluting the project environment.
When to Use
- •"write a quick script to fetch data from an API"
- •"make a one-off script to process this CSV"
- •"create a standalone script that uses requests/pandas/etc."
- •Any throwaway or utility script needing packages not in the project
Inline Script Format
Add a special comment block at the top of the script:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "requests",
# "rich",
# ]
# ///
import requests
from rich import print
response = requests.get("https://api.github.com")
print(response.json())
Running Inline Scripts
# Run directly (uv auto-detects inline metadata) uv run script.py # Or make executable and run chmod +x script.py ./script.py
Examples
Fetch and display JSON data:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = ["httpx", "rich"]
# ///
import httpx
from rich.console import Console
from rich.json import JSON
console = Console()
data = httpx.get("https://api.github.com/repos/astral-sh/uv").json()
console.print(JSON.from_data(data))
Quick CSV processing:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = ["pandas", "tabulate"]
# ///
import pandas as pd
df = pd.read_csv("data.csv")
print(df.describe().to_markdown())
Web scraping one-liner:
#!/usr/bin/env -S uv run --script
# /// script
# dependencies = ["beautifulsoup4", "requests"]
# ///
import requests
from bs4 import BeautifulSoup
soup = BeautifulSoup(requests.get("https://example.com").text, "html.parser")
print(soup.title.string)
Key Points
- •No project setup needed: Dependencies are installed in an isolated cache
- •Reproducible: Specify exact versions if needed (
requests>=2.28,<3) - •Self-contained: Script documents its own requirements
- •Fast: uv caches dependencies, subsequent runs are instant
When NOT to Use Inline Scripts
- •Script will be used repeatedly by the team → Add to project dependencies
- •Script is part of the main codebase → Use
uv runwith project deps - •Complex multi-file scripts → Create proper package structure
Red Flags
❌ Using python or python3 directly without uv run
❌ Activating venv manually before running (.venv/bin/activate)
❌ Using pip install instead of uv add or uv pip install
❌ Using pyright or mypy instead of ty check
❌ Running type checking with uv run pyright or uv run mypy
❌ Installing packages globally for one-time scripts (use inline metadata instead)
Success Criteria
✅ All Python script executions use uv run prefix
✅ New scripts have corresponding justfile tasks
✅ Consistent execution environment across all runs
✅ All type checking uses ty check
✅ Type errors are checked before and after code changes
✅ One-time scripts use inline metadata (PEP 723) for dependencies