C.W.K.
Stream
Lesson 04 of 05 · published

ruff 와 mypy — Lint 와 타입 체크

~18 min · ruff, mypy, lint, type-check

Level 0호기심
0 XP0/93 lessons0/23 achievements
0/100 XP to next level100 XP to go0% complete

ruff — 현대 Python linter

오랫동안 Python lint 엔 pylint, flake8, pycodestyle, isort, 플러그인 stack 필요, 각자 별 config. ruff (Rust 작성) 가 다 단일 도구로 대체, 10-100x 빠름. 스타일 체크, import 정렬, 데드 코드 검출, 보안 경고, 자동 fix 까지. 현대 Python 프로젝트 (Pippa 포함) 가 ruff 사용 + 끝.

두 명령

ruff check — 이슈 보여. ruff check --fix — 안전히 fix 가능한 거 자동 fix. ruff format — 코드 포맷 (많은 setup 에서 black 대체). pyproject.toml[tool.ruff] 아래 한 번 설정. 디폴트 룰 합리적, 특정 이유 있을 때만 조정.

mypy — 정적 타입 체크

코드에 타입 힌트 존재, mypy 가 그거 읽고 사용 검증. mypy module.py 또는 전체 프로젝트엔 mypy .. 에러가 file/line 으로 보고. CI 에서 실행, 가면서 모든 에러 fix. 엄격 모드 (--strict 또는 config 에 strict = true) 가 완전 타입 코드 요구 — 새 프로젝트에 맞는 설정, legacy 엔 야망.

pyright — 대안

pyright (Microsoft) 가 mypy 의 주 경쟁자. 빠름, 약간 다른 추론. VS Code 의 Pylance extension 이 사용. 많은 팀이 IDE 엔 속도용 pyright, CI 엔 "공식" 체크용 mypy. 둘 다 같은 타입 힌트에 작동.

pre-commit — 각 commit 전 체크 실행

pre-commit 이 Git hook 프레임워크. .pre-commit-config.yaml 에 원하는 도구 (ruff, mypy, pytest) 한 번 설정. git commit 시점 변경된 파일에 실행. push 전 이슈 잡음.

Code

ruff — 설치 + 실행·bash
pip install ruff

# 코드 체크
ruff check .

# 자동 fix
ruff check . --fix

# 포맷 (많이 black 대체)
ruff format .

# 흔한 출력:
# my_module.py:42:5: E711 Comparison to None should be 'x is None'
# Found 1 error.
# [*] 1 fixable with the --fix option.
pyproject.toml 의 ruff config·toml
[tool.ruff]
line-length = 100
target-version = "py310"

[tool.ruff.lint]
select = [
    "E",    # pycodestyle 에러
    "F",    # Pyflakes
    "W",    # pycodestyle 경고
    "I",    # isort
    "UP",   # pyupgrade
    "B",    # flake8-bugbear
    "C4",   # flake8-comprehensions
]
ignore = ["E501"]    # 줄 너무 김 (formatter 가 처리)

[tool.ruff.format]
quote-style = "double"
# preview 기능 활성화
preview = false
mypy — 기본 사용·bash
pip install mypy

# 파일 체크
mypy my_module.py

# 전체 프로젝트 체크
mypy .

# 엄격 모드 — 새 코드에 권장
mypy --strict .

# 출력:
# my_module.py:42: error: Incompatible return type (got 'int', expected 'str')
# Found 1 error in 1 file (checked 5 files)
pyproject.toml 의 mypy config·toml
[tool.mypy]
python_version = "3.10"
strict = true

# 모듈별 override
[[tool.mypy.overrides]]
module = "third_party_lib.*"
ignore_missing_imports = true

[[tool.mypy.overrides]]
module = "my_legacy_module.*"
disallow_untyped_defs = false      # gradual typing 동안 untyped 허용
pre-commit — commit gate·yaml
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.4.0
    hooks:
      - id: ruff
        args: [--fix]
      - id: ruff-format

  - repo: https://github.com/pre-commit/mirrors-mypy
    rev: v1.10.0
    hooks:
      - id: mypy

# hook 한 번 설치:
# pip install pre-commit
# pre-commit install
# 이제 모든 git commit 이 변경된 파일에 ruff + mypy 실행

External links

Exercise

작은 프로젝트에서 — (a) ruff 와 mypy 설치. (b) 둘 다 pyproject.toml 에 strict 설정으로 config. (c) 일부러 타입 에러 가진 작은 모듈 (예 — -> int annotated 함수에서 "abc" 반환). (d) mypy 실행 + 에러 관찰. fix. (e) ruff check 실행 — 일부러 unused import 가진 코드 짜고 ruff 가 표시 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.