Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
pydanticの紹介
Search
mizzsugar
February 13, 2020
Programming
2
640
pydanticの紹介
mizzsugar
February 13, 2020
Tweet
Share
More Decks by mizzsugar
See All by mizzsugar
厳しさとゆるさの間で迷う人に捧げる個人開発記
mizzsugar
0
19
SQLModel入門〜クエリと型〜
mizzsugar
1
1k
フルリモート向いてないと思っていた私が、なんだかんだ健やかに 1年半フルリモート出来ている話
mizzsugar
0
140
Djangoでのプロジェクトだって型ヒントを運用出来る!
mizzsugar
4
8.6k
「動くものは作れる」の一歩先へ 〜「自走プログラマー」の紹介〜
mizzsugar
0
570
pytestの第一歩 〜「テスト駆動Python」の紹介〜
mizzsugar
3
350
データ分析ツール開発でpoetryを使う選択肢
mizzsugar
1
1.1k
unittest.mockを使ってテストを書こう
mizzsugar
5
6.4k
変数に変数を代入したら?
mizzsugar
0
2.6k
Other Decks in Programming
See All in Programming
あなたとJIT, 今すぐアセンブ ル
sisshiki1969
1
690
Reactの歴史を振り返る
tutinoko
1
180
それ CLI フレームワークがなくてもできるよ / Building CLI Tools Without Frameworks
orgachem
PRO
17
3.9k
Claude Code と OpenAI o3 で メタデータ情報を作る
laket
0
130
[DevinMeetupTokyo2025] コード書かせないDevinの使い方
takumiyoshikawa
2
280
AIレビュアーをスケールさせるには / Scaling AI Reviewers
technuma
2
200
The State of Fluid (2025)
s2b
0
170
A Gopher's Guide to Vibe Coding
danicat
0
150
Google I/O Extended Incheon 2025 ~ What's new in Android development tools
pluu
1
290
なぜ今、Terraformの本を書いたのか? - 著者陣に聞く!『Terraformではじめる実践IaC』登壇資料
fufuhu
4
620
画像コンペでのベースラインモデルの育て方
tattaka
3
1.7k
Nuances on Kubernetes - RubyConf Taiwan 2025
envek
0
170
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
223
9.9k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Building Adaptive Systems
keathley
43
2.7k
Thoughts on Productivity
jonyablonski
69
4.8k
The Cost Of JavaScript in 2023
addyosmani
53
8.8k
Bash Introduction
62gerente
614
210k
Visualization
eitanlees
146
16k
Statistics for Hackers
jakevdp
799
220k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
139
34k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
283
13k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Transcript
今いち推しのバリデーター pydanticの紹介 20200213 Stapy @mizzsugar0424
お前、誰よ • Twitter : @mizzsugar0425 • 仕事:データ分析基盤の開発、運用 ◦ GCP, BigQuery
• 前の仕事はDjango。今でも追いかけてる。 • 趣味:PythonでWeb開発 ◦ Pyramid, Nuxt.js, TypeScript, PostgreSQL • 2/29にPyCon mini Shizuokaに登壇予定 ◦ unittest.mockの話です • ゆるく転職活動 ◦ 面接中に水をガブガブ飲む人間でも良いという企業様、ぜひ
# type: ignore
それは、 静的型付けとPythonの両方を 愛する者たちを苦しめる コメント
従来のバリデーターだとTypingのサポートがない import colander class Item(colander.MappingSchema): # type: ignore name =
colander.SchemaNode(colander.String()) price = colander.SchemaNode(colander.Integer())
Typingサポートのあるバリデーター、pydantic • 2017年爆誕 • Typingサポートあり • Python製WebフレームワークFastAPIでも紹介されている • mypy pluginあり
https://pydantic-docs.helpmanual.io/
基本的な書き方 import pydantic class Item(pydantic.BaseModel): name: str price: int
基本的な書き方 external_data = { 'name': 'chocolate', 'price': 500 } try:
item = Item(**external_data) except ValueError e: e.json() # エラー内容が返される
まるでオブジェクトを扱うよう class User(pydantic.BaseModel): name: str birthday: datetime.date friends: List[int] favorite_color:
Optional[str] int, str, List, Optional, datetime… などわりと使う型はサポートされている
楽々json! user = User.parse_raw('{"id": 123,"signup_ts":1234567890,"name":"John Doe"}') print(user) #> id=123 signup_ts=datetime.datetime(2009,
2, 13, 23, 31, 30, #> tzinfo=datetime.timezone.utc) name='John Doe'
快く使うために: mypy pluginのページは読んでね error: Module 'pydantic' has no attribute 'BaseModel'
[attr-defined] ↑mypy から注意されたけど ドキュメントのmypy pluginの設定を追加したら解消されたので 良く読もう https://pydantic-docs.helpmanual.io/mypy_plugin/
さらば、 # type: ignore
ありがとうございました。