ducdev
Python / Bài viết

Python Type Hints — Viết Code Rõ Ràng Hơn, Bắt Lỗi Sớm Hơn

Type hints không phải chỉ để hài lòng IDE — chúng thay đổi cách bạn nghĩ về code. Từ cú pháp cơ bản đến Generic, Protocol và TypedDict.

a
admin
27/06/2025 · 3 phút đọc · 0 lượt xem
Chia sẻ

Python vẫn là ngôn ngữ động, nhưng kể từ Python 3.5, type hints cho phép bạn annotate code mà không mất đi tính linh hoạt. Kết quả: editor tốt hơn, bugs ít hơn, và code dễ đọc hơn đáng kể.

Cú Pháp Cơ Bản

# Biến
name: str = "Duc"
age: int = 25
active: bool = True
score: float = 9.5

# Hàm
def chao(ten: str) -> str:
    return f"Xin chào, {ten}!"

def tinh_bmi(can_nang: float, chieu_cao: float) -> float:
    return can_nang / (chieu_cao ** 2)

Collection Types

from typing import List, Dict, Tuple, Set

def xu_ly_tags(tags: list[str]) -> dict[str, int]:
    return {tag: len(tag) for tag in tags}

# Python 3.9+ có thể dùng built-in types
def tinh_trung_binh(so: list[float]) -> float:
    return sum(so) / len(so)

Optional Và Union

from typing import Optional, Union

# Optional[X] = Union[X, None]
def lay_user(user_id: int) -> Optional[dict]:
    # Có thể trả về dict hoặc None
    return db.find(user_id)

# Union — nhiều kiểu
def xu_ly(value: Union[str, int, float]) -> str:
    return str(value)

# Python 3.10+ dùng | thay Union
def xu_ly_moi(value: str | int | float) -> str:
    return str(value)

TypedDict — Dict Với Schema Rõ Ràng

from typing import TypedDict

class PostData(TypedDict):
    title: str
    content: str
    published: bool
    views: int

def hien_thi_bai(post: PostData) -> str:
    return f"{post['title']} ({post['views']} lượt xem)"

# Editor sẽ cảnh báo nếu thiếu key hoặc sai kiểu
post: PostData = {
    "title": "Hello",
    "content": "...",
    "published": True,
    "views": 100,
}

Generic Types

from typing import TypeVar, Generic

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()

stack: Stack[int] = Stack()
stack.push(1)   # OK
stack.push("x") # mypy sẽ báo lỗi

Protocol — Duck Typing Có Type

from typing import Protocol

class Serializable(Protocol):
    def to_dict(self) -> dict: ...

class Post:
    def to_dict(self) -> dict:
        return {"title": self.title}

class User:
    def to_dict(self) -> dict:
        return {"name": self.name}

def serialize(obj: Serializable) -> str:
    import json
    return json.dumps(obj.to_dict())

# Cả Post và User đều thỏa Protocol — không cần kế thừa

Chạy Mypy

pip install mypy
mypy your_module.py

# Strict mode — kiểm tra tất cả
mypy --strict your_module.py
Type hints không làm Python chậm hơn — chúng chỉ tồn tại lúc phát triển. Runtime Python hoàn toàn bỏ qua annotations, trừ khi bạn dùng thư viện như Pydantic.

Kết Luận

Bắt đầu bằng cách annotate hàm public — return type và params. Đừng cố annotate tất cả cùng lúc. Khi đã quen, bật mypy vào CI pipeline để bắt type errors trước khi deploy.

#Python #Type hints #Mypy #Best practices
a
Tác giả
admin

Lập trình viên, yêu thích chia sẻ kiến thức về công nghệ và phát triển phần mềm.

Bình luận

Chưa có bình luận. Hãy là người đầu tiên!

Để lại bình luận

Bình luận sẽ được duyệt trước khi hiển thị.