NewType(typing)
#Python_typing
https://docs.python.org/ja/3/library/typing.html#newtype
Use the NewType helper to create distinct types:
静的型検査器は新しい型を元々の型のサブクラスのように扱います。
UserId 型の変数も int の全ての演算が行えますが、その結果は常に int 型になります。 この振る舞いにより、 int が期待されるところに UserId を渡せますが、不正な方法で UserId を作ってしまうことを防ぎます。
https://docs.python.org/ja/3/library/typing.html#typing.NewType
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)で知った
https://github.com/ftnext/PyConTalkSummary/issues/40
dataclassの型の表現が分かりやすくなる印象
code:python
Comment = NewType("Comment", str)
@dataclass
class Review:
comment: Comment
疑問:型エイリアス(typing)との使い分けは?