NewType(typing)
Use the NewType helper to create distinct types:
静的型検査器は新しい型を元々の型のサブクラスのように扱います。
UserId 型の変数も int の全ての演算が行えますが、その結果は常に int 型になります。 この振る舞いにより、 int が期待されるところに UserId を渡せますが、不正な方法で UserId を作ってしまうことを防ぎます。
A helper class to indicate a distinct type to a typechecker
At runtime it returns an object that returns its argument when called
「Python 3.9 時代の型安全な Pythonの極め方」(PyCon JP 2020)で知った
dataclassの型の表現が分かりやすくなる印象
code:python
Comment = NewType("Comment", str)
@dataclass
class Review:
comment: Comment