Skip to content

Instantly share code, notes, and snippets.

@tsuyukimakoto
Last active June 17, 2019 14:18
Show Gist options
  • Select an option

  • Save tsuyukimakoto/f7117552fe11c8e6bbd1c86bd567012a to your computer and use it in GitHub Desktop.

Select an option

Save tsuyukimakoto/f7117552fe11c8e6bbd1c86bd567012a to your computer and use it in GitHub Desktop.

2019.06.17 22:00+09:00

python3.8にmypyをインストール。まだmasterじゃないとインストールできない。

$ git clone --recurse-submodules https://github.com/python/mypy.git
$ cd mypy
$ python3.8 setup.py install

だがしかし、python3.8は from typing import TypedDict で、 mypyはまだ from typing_extensions import TypedDict でないとimportできない。

python実行

typeddicts1.py

from typing import TypedDict

class Book(TypedDict):
    name: str
    author: str
    price: int

if __name__ == '__main__':
    book1:Book = {'name': 'Spam', 'author': 'soseki', 'price': 300}
    book2:Book = {'name': 'Spam', 'writer': 'ogai', 'price': 450}
    print(book1)
    print(book2)
$ python3.8 typeddicts1.py
{'name': 'Spam', 'author': 'soseki', 'price': 300}
{'name': 'Spam', 'writer': 'ogai', 'price': 450}

mypyでチェック

typeddicts2.py

from typing_extensions import TypedDict

class Book(TypedDict):
    name: str
    author: str
    price: int

if __name__ == '__main__':
    book1:Book = {'name': 'Spam', 'author': 'soseki', 'price': 300}
    book2:Book = {'name': 'Spam', 'writer': 'ogai', 'price': 450}
    print(book1)
    print(book2)
$ mypy typeddicts2.py
typeddicts2.py:10: error: Extra key 'writer' for TypedDict "Book"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment