| name | python-code-style |
| description | Python 코드 스타일 검사 및 품질 관리 가이드 |
| version | 1.0.0 |
| tags | ["python","linting","code-quality","ruff","mypy"] |
| author | deep-research-MAF |
Python Code Style Check Skill
Overview
Python 코드 스타일 검사 및 품질 관리를 위한 가이드
Tools & Configuration
1. Ruff (권장)
현재 프로젝트에서 사용 중인 fast Python linter & formatter
설치
uv add --dev ruff
uvx ruff
기본 사용법
ruff check .
ruff check . --fix
ruff format .
ruff check backend/src
Configuration (pyproject.toml)
[tool.ruff]
line-length = 88
target-version = "py312"
[tool.ruff.lint]
select = [
"E",
"W",
"F",
"I",
"B",
"C4",
"UP",
]
ignore = [
"E501",
]
[tool.ruff.lint.isort]
known-first-party = ["src"]
2. Black (Alternative Formatter)
코드 포맷터로 사용 가능
uv add --dev black
black backend/src
black --check backend/src
uvx black backend/src
3. Mypy (Type Checking)
타입 체킹
uv add --dev mypy
mypy backend/src
uvx mypy backend/src
Configuration (pyproject.toml)
[tool.mypy]
python_version = "3.12"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
4. Pylint
상세한 코드 분석
uv add --dev pylint
pylint backend/src
uvx pylint backend/src
Best Practices
코드 스타일 가이드라인
-
PEP 8 준수
- 들여쓰기: 스페이스 4개
- 최대 라인 길이: 88자 (Black 기본값) 또는 79자 (PEP 8)
- 함수/클래스 간 공백: 2줄
-
Import 정렬
import os
import sys
import fastapi
import pydantic
from src.models import Query
from src.services import AzureOpenAIService
-
타입 힌트 사용
def process_query(query: str, max_results: int = 10) -> list[str]:
"""Process search query and return results."""
results: list[str] = []
return results
-
Docstring 작성
def complex_function(param1: str, param2: int) -> dict:
"""
Brief description of the function.
Args:
param1: Description of param1
param2: Description of param2
Returns:
Description of return value
Raises:
ValueError: When invalid input is provided
"""
pass
Pre-commit Integration
Setup
uv add --dev pre-commit
.pre-commit-config.yaml
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
Install hooks
pre-commit install
CI/CD Integration
GitHub Actions Example
name: Code Quality
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install dependencies
run: |
pip install uv
uv sync --dev
- name: Run Ruff
run: ruff check .
- name: Run MyPy
run: mypy backend/src
Quick Commands
프로젝트 전체 체크
ruff check .
mypy backend/src
ruff format .
특정 파일 체크
ruff check backend/src/main.py
mypy backend/src/main.py
변경된 파일만 체크
git diff --name-only --cached | grep '\.py$' | xargs ruff check
Common Issues & Solutions
Issue 1: Import 순서 문제
ruff check --select I --fix .
Issue 2: 사용하지 않는 import
ruff check --select F401 --fix .
Issue 3: 라인 길이 초과
ruff format .
VS Code Integration
settings.json
{
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
}
},
"ruff.lint.args": ["--config=backend/pyproject.toml"]
}
References