Amdsmi python style guide

From ROCm/rocm-systems · View interactive version

Amdsmi python style guide is an AI agent skill from the GitHub repository ROCm/rocm-systems. ROCm Python style guide based on TheRock. Use when: writing Python code, reviewing Python PRs, checking Python style, creating Python scripts, type hints, error handling patterns, pathlib usage, argparse CLI design.

How to use this skill

Skill file

Python Style Guide (ROCm / TheRock)

Follows PEP 8, enforced by Ruff (amd-smi) or Black (TheRock) via pre-commit.

Source: TheRock python_style_guide.md

Core Principles


Type Hints

Extract Complex Types

# Good
class KpackInfo(NamedTuple):
    kpack_path: Path
    size: int
    kernel_count: int

# Bad
def get_info() -> tuple[Path, int, int]: ...

Dataclasses Over Tuples

Structured data with multiple fields → @dataclass or NamedTuple, not raw tuples.

Tuples OK for: simple pairs (x, y), stdlib unpacking, single-use immediate unpacking.


Function Calls & Arguments

# Good
result = build(amdgpu_family="gfx942", enable_testing=True, use_ccache=False)

# Bad
result = build("gfx942", True, False)

Code Organization


Error Handling

| Rule | Detail |

|------|--------|

| Fail fast | Never silently continue or produce incomplete results |

| Specific exceptions | FileNotFoundError, subprocess.CalledProcessError — not bare except Exception |

| Preserve chains | Always use from e when re-raising |

| Validate results | After critical ops: check file exists, non-empty, expected size |

| No binutils timeouts | Never timeout= on readelf, objcopy, etc. |

# Good — distinguish error conditions
try:
    output = subprocess.check_output([readelf, "-S", str(file_path)])
except subprocess.CalledProcessError as e:
    if e.returncode == 1:
        return False  # legitimate "not found"
    raise RuntimeError(f"readelf failed: {e.output}") from e
except FileNotFoundError as e:
    raise RuntimeError(f"readelf not found: {readelf}") from e

# Bad — swallows everything
try:
    output = subprocess.check_output([readelf, "-S", str(file_path)])
except Exception:
    return False

Filesystem & Paths

# Good
THIS_DIR = Path(__file__).resolve().parent
config = THIS_DIR / "config.json"

# Bad
config = Path("build_tools/config.json")  # assumes CWD
config = Path("/home/user/project/config.json")  # hard-coded

Performance


Testing


amd-smi Python Conventions

Naming

Project Layout

| Directory | Purpose |

|-----------|---------|

| py-interface/ | Python bindings — amdsmi_interface.py, amdsmi_wrapper.py, amdsmi_exception.py |

| amdsmi_cli/ | CLI tool — amdsmi_commands.py, amdsmi_parser.py, amdsmi_helpers.py |

Critical Python Files (high churn — review carefully)

Library Loader Rules (`amdsmi_wrapper.py`)

Changes to _detect_install_context, _build_candidate_paths, or _load_library are ❌ BLOCKING if they break system or pip install context. Verify:

Python Testing


Review Checklist

When reviewing Python code, verify:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.