| name | python-patterns |
| description | Use when writing, reviewing, or refactoring Python code, designing data classes, implementing decorators, or choosing concurrency patterns. Do NOT use for linter/formatter config (use python-code-style) or testing (use python-testing). |
| paths | **/*.py, **/pyproject.toml |
Python Development Patterns
๋ฒ์ ๊ฒฝ๊ณ(3.10~3.14)์ ์ค์ ์ฆ์ ์ง์ ๋ง ๋ด๋๋ค. Python ์ผ๋ฐ ๋ฌธ๋ฒยทํจํด ์ง์์ ๋ชจ๋ธ์ ์ด๋ฏธ ์์.
CRITICAL Rules
- NEVER mutable default arguments โ
def f(items=[]) ๊ธ์ง, items=None ์ฌ์ฉ
- NEVER bare
except: โ ํญ์ specific exception ๋ช
์
- ALWAYS chain exceptions โ
raise NewError(...) from e
- PREFER EAFP over LBYL โ
try/except > if exists (race condition ๋ฐฉ์ง)
- PREFER modern type hints (3.10+) โ
str | None > Optional[str]
- ALWAYS
is None / is not None โ == None ๊ธ์ง
Version Boundaries (3.10+)
| Feature | Version | Note |
|---|
X | None union syntax | 3.10 | Optional/Union์ <3.10 ์ง์ ์์๋ง |
type statement (PEP 695), @override | 3.12 | |
TypeIs (์๋ฐฉํฅ type narrowing) | 3.13 | |
| Free-threaded mode | 3.13 experimental โ 3.14 supported | NumPy/pandas๋ ์ด๋ฏธ GIL ํด์ โ threading์ผ๋ก ์ถฉ๋ถํ ๊ฒฝ์ฐ ๋ง์ |
| Deferred annotations ๊ธฐ๋ณธํ (PEP 649) | 3.14 | from __future__ import annotations ๋ถํ์ |
Gotchas
- โ mutable default argument (
def f(x=[])) โ None ๊ธฐ๋ณธ๊ฐ + ๋ด๋ถ ์์ฑ
- โ
type() ๋น๊ต โ isinstance() ์ฌ์ฉ
- โ
== None ๋น๊ต โ is None ์ฌ์ฉ
- โ asyncio์์
CancelledError ์ผํค๊ธฐ โ ๋ฐ๋์ re-raise
- โ asyncio์
gather() ์ต๊ด์ ์ฌ์ฉ โ 3.11+๋ TaskGroup (structured concurrency, ์๋ฌ ์ ํ ์์ )
- โ CPU-bound์ threading โ
ProcessPoolExecutor (๋จ, NumPy/pandas๋ GIL ํด์ ํ๋ฏ๋ก threading OK)
Verification
ruff check . && ruff format --check .
pytest -x -q
mypy --strict src/
Cross-References
| Topic | Skill |
|---|
| Ruff, mypy, formatting, naming | python-code-style |
| pytest, TDD, fixtures, mocking | python-testing |