Skill v1.0.0
currentLLM-judged scan90/100version: "1.0.0" name: python-style description: Apply personal Python code style — stdlib-first, single quotes, low nesting, named ANSI constants, performance-driven, vertical kwarg alignment. Use when writing or editing Python (.py files), setting up pyproject.toml, choosing between implementations, or naming tests.
Python Style
Rules
Dependencies
- Prefer the standard library. Reach for third-party packages only when stdlib genuinely lacks the capability — say so explicitly in the commit/PR.
- Avoid
typing.cast. Fix the types or change the design first. Only reach forcastwhen there is literally no other way, or when the alternative is dramatically more complex — and say why in a comment. - Avoid
Anyat all costs. Use a precise type, aProtocol, aTypeVar, orobject+isinstancenarrowing before falling back toAny. - For self-referential type hints (a class referring to itself, or forward references), use
from __future__ import annotationsat the top of the file rather than quoting the type name as a string.
```python from __future__ import annotations
class Node: def add_child(self, child: Node) -> None: ... # not 'Node' ```
Structure
- Keep nesting shallow: 2 levels is the usual ceiling, 3 the hard maximum.
- Use early return /
continueto flatten. - If early return won't work, extract a helper method.
- Never write banner comments (
# ===== Section =====etc.).
Quoting
- Single quotes for strings:
'hello', not"hello". - Single-line docstrings use single quotes too:
``python def yolo() -> None: 'A test function to demonstrate docstring style' ... ``
Performance
- Performance is a first-class concern. When multiple implementations are plausible, benchmark them.
- Benchmark options, in order of preference:
bench.pyfrom laser-prynter — for comparing multiple Python implementations on the same input set.ipythonwith%timeit— for ad-hoc one-liners.hyperfine— for whole-process / CLI comparisons.
- Prefer
'sep'.join([...])over repeated+=string append in loops. - Prefer
yield(generator) over building a list withfor+appendthen returning it.
Constants
- Always use named constants for ANSI colour/style codes. Never inline
'\033[31m'etc.
``python RED = '\033[31m' RESET = '\033[0m' print(f'{RED}error{RESET}') ``
Tests
- Name tests after what they test, not after spec numbers, ticket IDs, or section labels.
- Good:
test_parses_iso_date_with_offset - Bad:
test_REQ_042_3,test_spec_section_2_1
Vertical alignment of kwargs
- I prefer aligned
=in long kwarg lists. Ruff's formatter strips these spaces. - Workaround: keep the file in
tool.ruff.exclude(see template). The file is still linted only if removed from exclude — but format won't touch alignment. - Example shape:
``python return cls( session_id = d.get('session_id', ''), transcript_path = d.get('transcript_path', ''), cwd = d.get('cwd', ''), ) ``
Project setup
Drop pyproject-template.toml (in this skill dir) into new repos as the starting point. It encodes:
mypystrict mode withdisallow_untyped_defsruff checkwith single-quote enforcement (flake8-quotes)ruff formatwithquote-style = "preserve"so existing single quotes survive- Excludes for
.claude/,.venv/, and any dir holding vertically-aligned code
Checklist before declaring done
- [ ] No new
cast(...)calls (or one with a justifying comment) - [ ] No
Anyintroduced - [ ] Self-referential hints use
from __future__ import annotations, not stringified types - [ ] No banner comments
- [ ] Single quotes throughout (including docstrings where single-line)
- [ ] Nesting ≤ 2 levels (3 only if justified)
- [ ] ANSI codes pulled out into named constants
- [ ] If multiple implementations considered: benchmark numbers in the commit/PR
- [ ] Test names describe behaviour, not spec IDs
- [ ]
ruff checkclean,mypy --strictclean (outside exclude list)