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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
mizzsugar
February 13, 2020
Programming
2
650
pydanticの紹介
mizzsugar
February 13, 2020
Tweet
Share
More Decks by mizzsugar
See All by mizzsugar
厳しさとゆるさの間で迷う人に捧げる個人開発記
mizzsugar
0
37
SQLModel入門〜クエリと型〜
mizzsugar
2
1.4k
フルリモート向いてないと思っていた私が、なんだかんだ健やかに 1年半フルリモート出来ている話
mizzsugar
1
150
Djangoでのプロジェクトだって型ヒントを運用出来る!
mizzsugar
4
8.9k
「動くものは作れる」の一歩先へ 〜「自走プログラマー」の紹介〜
mizzsugar
0
610
pytestの第一歩 〜「テスト駆動Python」の紹介〜
mizzsugar
3
430
データ分析ツール開発でpoetryを使う選択肢
mizzsugar
1
1.2k
unittest.mockを使ってテストを書こう
mizzsugar
5
6.7k
変数に変数を代入したら?
mizzsugar
1
2.6k
Other Decks in Programming
See All in Programming
React 19でつくる「気持ちいいUI」- 楽観的UIのすすめ
himorishige
11
5.9k
FOSDEM 2026: STUNMESH-go: Building P2P WireGuard Mesh Without Self-Hosted Infrastructure
tjjh89017
0
140
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
290
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
220
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
150
コントリビューターによるDenoのすゝめ / Deno Recommendations by a Contributor
petamoriken
0
200
15年続くIoTサービスのSREエンジニアが挑む分散トレーシング導入
melonps
2
160
AIエージェント、”どう作るか”で差は出るか? / AI Agents: Does the "How" Make a Difference?
rkaga
4
2k
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
220
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
440
今こそ知るべき耐量子計算機暗号(PQC)入門 / PQC: What You Need to Know Now
mackey0225
3
370
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.5k
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
97
6.5k
How GitHub (no longer) Works
holman
316
140k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.1k
How to Talk to Developers About Accessibility
jct
2
120
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
640
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.5k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
180
Reality Check: Gamification 10 Years Later
codingconduct
0
2k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
Automating Front-end Workflow
addyosmani
1371
200k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
196
71k
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
ありがとうございました。