3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonのdictに型付けする

Posted at

きっかけ

本題

from typing import TypedDict

class ObjectType(TypedDict):
    id: int
    name: str

class RequestType(TypedDict):
    foo: str
    bar: ObjectType

def get(self, args: RequestType) -> None:
    foo = args['foo'] # type: str
    bar = args['bar'] # type: ObjectType
    name = args['bar']['name'] # type: str

まとめ

  • TSと似た感覚で型付けができます!
  • typingにはこれ以外にも多くの型ヒントが定義されていますので一度目を通すと参考になるかと思います
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?