.agents/skills/dignified-python/dignified-python-core.md
This document contains the core Python coding standards that apply to 80%+ of Python code. These principles are loaded with every skill invocation.
For conditional loading of specialized patterns:
cli-patterns.mdsubprocess.mdFor detailed reference material, see the "When to Read Each Reference" section in SKILL.md.
This skill leans LBYL when a cheap, precise precondition keeps intent clearer than a try/except.
# CORRECT: Check first
if key in mapping:
value = mapping[key]
process(value)
# WRONG: Exception as control flow
try:
value = mapping[key]
process(value)
except KeyError:
pass
Prefer LBYL for routine branching when the precondition is cheap and precise.
LBYL means checking conditions before acting. EAFP (Easier to Ask for Forgiveness than Permission) means trying operations and catching exceptions. In this skill, default to LBYL for ordinary branching, but use EAFP when the operation itself is the authoritative test or when you're translating failures at a boundary.
# CORRECT: Membership testing
if key in mapping:
value = mapping[key]
process(value)
else:
handle_missing()
# ALSO CORRECT: .get() with default
value = mapping.get(key, default_value)
process(value)
# CORRECT: Check before nested access
if "config" in data and "timeout" in data["config"]:
timeout = data["config"]["timeout"]
# WRONG: KeyError as control flow
try:
value = mapping[key]
except KeyError:
handle_missing()
Exceptions are a good fit at:
Default: Let exceptions bubble up
For detailed exception handling patterns including B904 chaining, third-party API examples, and
anti-patterns, see references/advanced/exception-handling.md.
Use .exists() when filesystem presence is part of your requirement, not as a blanket precondition
for .resolve() or .is_relative_to().
Path.resolve() on Python 3.11 resolves non-existent paths unless you pass strict=TruePath.is_relative_to() returns bool; it does not raise ValueError when a path is not under
another pathfrom pathlib import Path
# CORRECT: Check existence only when you need a real filesystem entry
for wt_path in worktree_paths:
wt_path_resolved = wt_path.resolve()
if not wt_path_resolved.exists():
continue
if current_dir.is_relative_to(wt_path_resolved):
current_worktree = wt_path_resolved
break
# ALSO CORRECT: Ask resolve() to fail when absence is an error
config_dir = config_path.resolve(strict=True)
# WRONG: Broad exception handling around APIs that already communicate the result directly
for wt_path in worktree_paths:
try:
wt_path_resolved = wt_path.resolve()
if current_dir.is_relative_to(wt_path_resolved):
current_worktree = wt_path_resolved
break
except OSError:
continue
Always Use Pathlib (Never os.path)
# CORRECT: Use pathlib.Path
from pathlib import Path
config_file = Path.home() / ".config" / "app.yml"
if config_file.exists():
content = config_file.read_text(encoding="utf-8")
# WRONG: Use os.path
import os.path
config_file = os.path.join(os.path.expanduser("~"), ".config", "app.yml")
Always Specify Encoding
# CORRECT: Always specify encoding
content = path.read_text(encoding="utf-8")
path.write_text(data, encoding="utf-8")
# WRONG: Default encoding
content = path.read_text() # Platform-dependent!
# CORRECT: Module-level imports
import json
import click
from pathlib import Path
from myapp.config import load_config
def my_function() -> None:
data = json.loads(content)
# CORRECT: Absolute import
from myapp.config import load_config
# WRONG: Relative import
from .config import load_config
# WRONG: Inline imports without justification
def my_function() -> None:
import json # NEVER do this
For detailed inline import patterns and when they're legitimate, see references/module-design.md.
# WRONG: Property doing I/O
@property
def size(self) -> int:
return self._fetch_from_db()
# CORRECT: Explicit method name
def fetch_size_from_db(self) -> int:
return self._fetch_from_db()
# CORRECT: O(1) property
@property
def size(self) -> int:
return self._cached_size
# WRONG: __len__ doing iteration
def __len__(self) -> int:
return sum(1 for _ in self._items)
# CORRECT: O(1) __len__
def __len__(self) -> int:
return self._count
# WRONG: Keeping old API unnecessarily
def process_data(data: dict, legacy_format: bool = False) -> Result:
if legacy_format:
return legacy_process(data)
return new_process(data)
# CORRECT: Break and migrate immediately
def process_data(data: dict) -> Result:
return new_process(data)
Core Principle: Every symbol has exactly one import path. Never re-export.
# WRONG: __all__ exports create duplicate import paths
# myapp/__init__.py
from myapp.core import Process
__all__ = ["Process"]
# CORRECT: Empty __init__.py, import from canonical location
# from myapp.core import Process
When re-exports ARE required (plugin entry points): Use explicit import X as X syntax:
# CORRECT: Explicit re-export syntax for required entry points
from myapp.core.feature import my_function as my_function
# WRONG: Variable declared far from use
def process_data(ctx, items):
result_path = compute_result_path(ctx) # Declared here...
# 20+ lines of other logic...
save_to_path(transformed, result_path) # ...used here
# CORRECT: Inline at use site
def process_data(ctx, items):
validate_items(items)
transformed = transform_items(items)
save_to_path(transformed, compute_result_path(ctx))
# WRONG: Unnecessary field extraction
result = fetch_user(user_id)
name = result.name # only used once below
email = result.email # only used once below
send_notification(name, email, role)
# CORRECT: Access fields directly
user = fetch_user(user_id)
send_notification(user.name, user.email, user.role)
Maximum indentation: 4 levels
# WRONG: Too deeply nested (5 levels)
def process_items(items):
for item in items:
if item.valid:
for child in item.children:
if child.enabled:
for grandchild in child.descendants:
pass # 5 levels deep!
# CORRECT: Extract helper functions
def process_items(items):
for item in items:
if item.valid:
process_children(item.children)
def process_children(children):
for child in children:
if child.enabled:
process_descendants(child.descendants)
with Statements# CORRECT: Context manager stays in with statement
with (cm_a if condition else nullcontext()):
do_work()
# CORRECT: Multiple conditional context managers
with (lock if thread_safe else nullcontext()):
process(data)
# WRONG: Extracting to intermediate variable obscures lifecycle
cm = cm_a if condition else nullcontext()
with cm:
do_work()
Context managers belong in with statements where the __enter__/__exit__ lifecycle is explicit.
Do not extract them to intermediate variables. If the inline expression is genuinely overwhelming,
extract the logic into a helper function that returns the context manager.
Default stance: NO backwards compatibility preservation
Only preserve backwards compatibility when:
Benefits:
For detailed guidance on specialized topics:
references/advanced/exception-handling.mdreferences/advanced/interfaces.mdreferences/advanced/typing-advanced.mdreferences/module-design.mdreferences/advanced/api-design.mdreferences/checklists.md