linter

권도현·2025년 10월 12일

lint는 영어로 '보풀'을 의미하는 단어이다. 벨 연구소에서 1978년에 개발된 C언어 소스코드 분석 도구인 'lint'에서 linter 혹은 lint라는 용어가 탄생하였다. 이후 모든 정적 코드 분석기를 linter(lint)라고 부른다.
python에서 대표적인 linter로는 Ruff, Flake8, Pylint, Mypy 등이 있다.

Ruff

내가 python 프로젝트에서 주로 사용하는 linter는 Ruff이다.
Ruff는 Rust로 개발된 formatter + linter로 Ruff를 개발한 Astral Software사의 사이트를 들어가보면 FastAPI프로젝트에 사용하는 것을 추천하고 있으며 lint 기본 설정은 Flake8을 지키도록 되어있다.

#pyproject.toml
[tool.ruff]
target-version="py313"

https://docs.astral.sh/ruff/configuration/
이처럼 pyproject.toml에서 설정을 바꿔 사용할 수 있다.
위의 내용은 python버전을 3.13버전을 대상으로 하겠다는 의미이며 이외에도 여러 개의 옵션이 존재한다.

#tmp.py
import os,sys

print("hello world")

Flake8 Rules에 의해 위의 코드는 E401,F401 규칙을 위반한 코드이다.

tmp.py:1:1: E401 [*] Multiple imports on one line
  |
1 | import os,sys
  | ^^^^^^^^^^^^^ E401
2 | print("aaaa")
  |
  = help: Split imports

tmp.py:1:8: F401 [*] `os` imported but unused
  |
1 | import os,sys
  |        ^^ F401
2 | print("aaaa")
  |
  = help: Remove unused import

tmp.py:1:11: F401 [*] `sys` imported but unused
  |
1 | import os,sys
  |           ^^^ F401
2 | print("aaaa")
  |
  = help: Remove unused import

Found 3 errors.
[*] 3 fixable with the `--fix` option.

ruff check --fix로 위반사항들을 수정할 수 있다.

만약 ruff 검사에서 제외해야 하는 문장이 있는 경우

import os,sys # noqa

처럼 # noqa 주석으로 선언해주면 된다.

profile
weeeeeeeee!

0개의 댓글