Python Type Hints 완벽 가이드
기본 타입 힌트
def greet(name: str) -> str:
return f"Hello, {name}"
age: int = 25
price: float = 19.99
is_active: bool = True
컬렉션 타입
from typing import List, Dict, Set, Tuple
names: List[str] = ["Alice", "Bob"]
scores: Dict[str, int] = {"Alice": 95, "Bob": 87}
tags: Set[str] = {"python", "coding"}
point: Tuple[int, int] = (10, 20)
Optional과 Union
from typing import Optional, Union
def find_user(user_id: int) -> Optional[str]:
return None
def process(value: Union[int, str]) -> str:
return str(value)
제네릭 타입
from typing import TypeVar, Generic, List
T = TypeVar('T')
class Stack(Generic[T]):
def __init__(self) -> None:
self.items: List[T] = []
def push(self, item: T) -> None:
self.items.append(item)
def pop(self) -> T:
return self.items.pop()
int_stack: Stack[int] = Stack()
int_stack.push(1)
Callable 타입
from typing import Callable
def apply_operation(
x: int,
y: int,
operation: Callable[[int, int], int]
) -> int:
return operation(x, y)
apply_operation(5, 3, lambda a, b: a + b)
TypedDict
from typing import TypedDict
class User(TypedDict):
name: str
age: int
email: str
user: User = {
"name": "Alice",
"age": 30,
"email": "alice@example.com"
}
Literal 타입
from typing import Literal
def set_status(status: Literal["pending", "approved", "rejected"]) -> None:
print(f"Status: {status}")
set_status("approved")
도구
mypy 사용
pip install mypy
mypy your_file.py
pyright (Microsoft)
pip install pyright
pyright your_file.py
요약
- 기본 타입: str, int, float, bool
- 컬렉션: List, Dict, Set, Tuple
- Optional: None 가능
- Union: 여러 타입 허용
- Generic: 제네릭 클래스
- TypedDict: 구조화된 딕셔너리