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
24
SQLModel入門〜クエリと型〜
mizzsugar
1
1.1k
フルリモート向いてないと思っていた私が、なんだかんだ健やかに 1年半フルリモート出来ている話
mizzsugar
0
140
Djangoでのプロジェクトだって型ヒントを運用出来る!
mizzsugar
4
8.7k
「動くものは作れる」の一歩先へ 〜「自走プログラマー」の紹介〜
mizzsugar
0
580
pytestの第一歩 〜「テスト駆動Python」の紹介〜
mizzsugar
3
380
データ分析ツール開発でpoetryを使う選択肢
mizzsugar
1
1.1k
unittest.mockを使ってテストを書こう
mizzsugar
5
6.5k
変数に変数を代入したら?
mizzsugar
0
2.6k
Other Decks in Programming
See All in Programming
iOSエンジニア向けの英語学習アプリを作る!
yukawashouhei
0
170
大規模アプリのDIフレームワーク刷新戦略 ~過去最大規模の並行開発を止めずにアプリ全体に導入するまで~
mot_techtalk
0
360
monorepo の Go テストをはやくした〜い!~最小の依存解決への道のり~ / faster-testing-of-monorepos
convto
2
230
(Extension DC 2025) Actor境界を越える技術
teamhimeh
1
210
メモリ不足との戦い〜大量データを扱うアプリでの実践例〜
kwzr
1
770
Breaking Up with Big ViewModels — Without Breaking Your Architecture (droidcon Berlin 2025)
steliosf
PRO
1
290
どの様にAIエージェントと 協業すべきだったのか?
takefumiyoshii
1
580
CSC305 Lecture 02
javiergs
PRO
1
260
開発者への寄付をアプリ内課金として実装する時の気の使いどころ
ski
0
340
詳しくない分野でのVibe Codingで困ったことと学び/vibe-coding-in-unfamiliar-area
shibayu36
3
3.7k
プログラマのための作曲入門
cheebow
0
530
Advance Your Career with Open Source
ivargrimstad
0
270
Featured
See All Featured
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
YesSQL, Process and Tooling at Scale
rocio
173
14k
Become a Pro
speakerdeck
PRO
29
5.5k
For a Future-Friendly Web
brad_frost
180
9.9k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.5k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
2.6k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
61k
Making the Leap to Tech Lead
cromwellryan
135
9.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
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
ありがとうございました。