Skip to content
配图

Python typing

python
from typing import Protocol

class Repository(Protocol):
    def get(self, id: str) -> Post: ...

def render(repo: Repository, id: str) -> str:
    return repo.get(id).title

Protocol 实现结构化子类型;Generic[T] 表达容器不变性。

mypy --strict 在 CI 里渐进启用,比事后补类型便宜。

Visitors · Page views