๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Python

Ruff (๊ณ ์„ฑ๋Šฅ Python linter) ์†Œ๊ฐœ ๋ฐ ์„ธํŒ…

by KK1 2023. 8. 20.

 

ruff homepage https://beta.ruff.rs/docs/

 

Ruff

Ruff Discord | Docs | Playground An extremely fast Python linter, written in Rust. Linting the CPython codebase from scratch. โšก๏ธ 10-100x faster than existing linters ๐Ÿ Installable via pip ๐Ÿ› ๏ธ pyproject.toml support ๐Ÿค Python 3.11 compatibility

beta.ruff.rs

ruff github https://github.com/astral-sh/ruff

 

GitHub - astral-sh/ruff: An extremely fast Python linter, written in Rust.

An extremely fast Python linter, written in Rust. Contribute to astral-sh/ruff development by creating an account on GitHub.

github.com

Ruff๋Š” Rust๋กœ ์ž‘์„ฑ๋œ ๊ณ ์„ฑ๋Šฅ Python Linter ์ž…๋‹ˆ๋‹ค.

์œ„ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด ๋‹ค๋ฅธ linter ๋Œ€๋น„ ๊ต‰์žฅํžˆ ๊ณ ์„ฑ๋Šฅ์ž„์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

์ž‘์—… ์ค‘ ์ €์žฅํ•  ๋•Œ ๋งˆ๋‹ค ์ฒดํฌํ•ด๋„ ๋ฌด๊ด€ํ•  ์ •๋„์ž…๋‹ˆ๋‹ค.

 

Linter๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•  ์ด์œ ?

Linter๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด 1์ฐจ์ ์œผ๋กœ ์ฝ”๋“œ ์Šคํƒ€์ผ์˜ ๊ธฐ๋ณธ์ ์ธ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ๊ฒ€์ฆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ๋ฒ• ์Šคํƒ€์ผ, ์ฝ”๋“œ ๋ผ์ธ๋‹น ๋ฌธ์ž์ˆ˜, ํ•จ์ˆ˜ ๋‚ด if ๋ฌธ ๊ฐœ์ˆ˜ ๋“ฑ ๊ฒ€์ฆ์„ Linter์—์„œ ํ•ด์ฃผ๋‹ˆ ์ฝ”๋“œ ๋ฆฌ๋ทฐํ•  ๋•Œ ๋ฆฌ๋ทฐ์–ด๋Š” ๋กœ์ง๋ถ€๋ถ„๋งŒ ์‹ ๊ฒฝ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

์ด์™€ ๊ฐ™์ด ๋„๊ตฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ 1์ฐจ์ ์ธ ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด ํŒ€๋‚ด์— ์ƒ์‚ฐ์„ฑ๋„ ์ฆ๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ณ ๋กœ ์•ˆ์จ๋„ ๋˜์ง€๋งŒ ์“ฐ๋ฉด ๋งค์šฐ ์ข‹์€ ๊ฒƒ์ด Linter ์ž…๋‹ˆ๋‹ค.

 

ํ™œ๋ฐœํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ์˜คํ”ˆ์†Œ์Šค

Apache Airflow

FastAPI

Hugging Face

Pandas

SciPy

๋“ฑ..

์œ ๋ช… ์˜คํ”ˆ์†Œ์Šค์—์„œ๋„ ruff๋ฅผ ์ ๊ทน ํ™œ์šฉ์ค‘์ž…๋‹ˆ๋‹ค.

 

์„ค์น˜ ๋ฐ ๊ธฐ๋ณธ ์‚ฌ์šฉ

๊ธฐ๋ณธ์ ์ธ ์„ค์น˜ ๋ฐ ์„ธํŒ… ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ๋ ค๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

pip install ruff

์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•˜์—ฌ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

ruff .
ruff check .
python -m ruff .
python -m ruff check .

์œ„ ๋„ค๊ฐ€์ง€ ๋ฐฉ๋ฒ• ์ค‘ ์•„๋ฌด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ํ˜„์žฌ ๊ฒฝ๋กœ ๋ฐ ํ•˜์œ„ ์ „์ฒด ๊ฒฝ๋กœ์— ๋Œ€ํ•ด ์ฝ”๋“œ ์ปจ๋ฒค์…˜์„ ๊ฒ€์‚ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋Œ€๋žต ์œ„์™€ ๊ฐ™์ด ์–ด๋–ค ํŒŒ์ผ์˜ ์–ด๋Š ๋ผ์ธ ๋ช‡ ๋ฒˆ์งธ ์นธ์˜ ์ฝ”๋“œ์— ์–ด๋–ค ์ œ์•ฝ์กฐ๊ฑด์— ๊ฑธ๋ฆฌ๋Š”์ง€ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

ํ˜„์žฌ๋Š” ํŠน์ • ์ฝ”๋“œ์˜ ๋ผ์ธ์ด ๋„ˆ๋ฌด ๊ธธ๋‹ค๋Š” ์—๋Ÿฌ๋ฅผ ๋‚จ๊ธฐ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

pyproject.toml ์„ค์ • ํ•˜๊ธฐ

pyproject.toml์€ PEP 518 ์—์„œ ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ์‹œ์Šคํ…œ์— ๋Œ€ํ•œ ์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์ •๋œ ํŒŒ์ผ์ด๋ฉฐ Ruff ์—์„œ๋Š” ์„ค์ •ํŒŒ์ผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

[tool.ruff]
select = [
    "E",
    "F",
    "PL",
]

Root ๊ฒฝ๋กœ์— pyproject.toml์„ ์ถ”๊ฐ€ํ›„ ์œ„ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

select๋Š” ์–ด๋–ค ๋ฃฐ๋“ค์„ ์ ์šฉํ• ์ง€ ์„ ํƒํ•˜๋Š” ์˜ต์…˜์ž…๋‹ˆ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด ์„ค์ •ํ•˜์—ฌ ์ฝ”๋“œ๊ฐ€ ์ข€ ๋” ๋งŽ์€ ์ œ์•ฝ์กฐ๊ฑด์— ๊ฑธ๋ฆฌ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

์˜ต์…˜์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ https://beta.ruff.rs/docs/rules/ ์— ์ž์„ธํžˆ ๋‚˜์™€์žˆ์Šต๋‹ˆ๋‹ค.

ํ•„์š”์— ๋”ฐ๋ผ ํŒ€๋‚ด์˜ ๋ฃฐ๋Œ€๋กœ ์ฝ”๋“œ ๋ผ์ธ์ˆ˜, ์ œ์•ฝ์กฐ๊ฑด ๋“ฑ์„ ์„ค์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

Ruff ํ™œ์šฉ ๋ฐฉํ–ฅ

ํŒ€ ๋‹จ์œ„ ์ž‘์—…์ด๋ผ๋ฉด pyproject.toml ํŒŒ์ผ์„ ํ˜•์ƒ์— ์˜ฌ๋ ค ๋ฃฐ์„ ๊ด€๋ฆฌํ•˜๋ฉด ํŒ€ ์ „์ฒด ์ฝ”๋“œ ํ€„๋ฆฌํ‹ฐ๊ฐ€ ์˜ฌ๋ผ๊ฐˆ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

git์„ ์“ด๋‹ค๋ฉด pre-commit hook ์—์„œ ์ปค๋ฐ‹์‹œ Ruff๋ฅผ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜์—ฌ ํ†ต๊ณผ๋˜๋ฉด ๋„˜์–ด๊ฐ€๋„๋ก ์„ธํŒ…์„ ํ•˜๊ฑฐ๋‚˜,

PyCharm์ด๋ผ๋ฉด File Wathers ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ํ™œ์šฉํ•˜์—ฌ ์ €์žฅํ•  ๋•Œ ๋งˆ๋‹ค ์ฒดํฌํ•ด๋„ ์ถฉ๋ถ„ํžˆ ์‚ฌ์šฉํ• ๋งŒ ํ•  ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

Formatter์ธ Black์™€ ํ•จ๊ป˜ Ruff๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ ์ •๋ฆฌ ๋ฐ ํ€„๋ฆฌํ‹ฐ ์—…์„ ์†์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€