PEP 8 style guide enforcement for Python code. Covers formatting, naming, imports, whitespace, comments, docstrings, type hints, and tooling configuration. Use this skill whenever writing, reviewing, or refactoring Python code to ensure consistent style across the project.
These instructions define the code style for all Python code in this project. Every Python file must follow these rules. They are based on PEP 8, extended with project-specific conventions where PEP 8 is silent or ambiguous.
Tooling enforces most of this automatically:
ruff check . # lint
ruff format . # format
mypy . # type check
Do not rely on memory — configure the tools, run them, and let them catch violations.
# Aligned with opening delimiter
result = some_function(first_argument, second_argument,
third_argument, fourth_argument)
# Hanging indent — preferred for long signatures
result = some_function(
first_argument,
second_argument,
third_argument,
fourth_argument,
)
# Good — trailing comma
config = {
"host": "localhost",
"port": 5432,
"database": "myapp",
}
# Bad — no trailing comma
config = {
"host": "localhost",
"port": 5432,
"database": "myapp"
}
if, for, while, with, try headers.Spaces around operators:
# Good
x = 1
y = x + 2
result = value * (base + offset)
is_valid = name != "" and age >= 18
# Bad
x=1
y = x +2
result = value *( base + offset)
No spaces inside brackets:
# Good
items[0]
data["key"]
func(arg1, arg2)
# Bad
items[ 0 ]
data[ "key" ]
func( arg1, arg2 )
No spaces around = in keyword arguments or default values:
# Good
def connect(host="localhost", port=5432, timeout=30):
client.send(data=payload, retry=True)
# Bad
def connect(host = "localhost", port = 5432, timeout = 30):
client.send(data = payload, retry = True)
Spaces around = in annotated assignments:
# Good — space around = when annotation is present