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
670
2
Share
pydanticの紹介
mizzsugar
February 13, 2020
More Decks by mizzsugar
See All by mizzsugar
厳しさとゆるさの間で迷う人に捧げる個人開発記
mizzsugar
0
52
SQLModel入門〜クエリと型〜
mizzsugar
2
1.5k
フルリモート向いてないと思っていた私が、なんだかんだ健やかに 1年半フルリモート出来ている話
mizzsugar
1
160
Djangoでのプロジェクトだって型ヒントを運用出来る!
mizzsugar
4
9k
「動くものは作れる」の一歩先へ 〜「自走プログラマー」の紹介〜
mizzsugar
0
630
pytestの第一歩 〜「テスト駆動Python」の紹介〜
mizzsugar
3
470
データ分析ツール開発でpoetryを使う選択肢
mizzsugar
1
1.2k
unittest.mockを使ってテストを書こう
mizzsugar
5
6.8k
変数に変数を代入したら?
mizzsugar
1
2.6k
Other Decks in Programming
See All in Programming
Codexに役割を持たせる 他のAIエージェントと組み合わせる実務Tips
o8n
4
1.5k
AI-DLC 入門 〜AIコーディングの本質は「コード」ではなく「構造」〜 / Introduction to AI-DLC: The Essence of AI Coding Is Not “Code” but “Structure”
seike460
PRO
0
160
Redox OS でのネームスペース管理と chroot の実現
isanethen
0
500
生成 AI 時代のスナップショットテストってやつを見せてあげますよ(α版)
ojun9
0
330
ファインチューニングせずメインコンペを解く方法
pokutuna
0
250
条件判定に名前、つけてますか? #phperkaigi #c
77web
2
910
How to stabilize UI tests using XCTest
akkeylab
0
150
Everything Claude Code OSS詳細 — 5層構造の中身と導入方法
targe
0
160
Understanding Apache Lucene - More than just full-text search
spinscale
0
150
野球解説AI Agentを開発してみた - 2026/02/27 LayerX社内LT会資料
shinyorke
PRO
0
380
「速くなった気がする」をデータで疑う
senleaf24
0
120
RSAが破られる前に知っておきたい 耐量子計算機暗号(PQC)入門 / Intro to PQC: Preparing for the Post-RSA Era
mackey0225
3
110
Featured
See All Featured
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.2k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
150
Typedesign – Prime Four
hannesfritz
42
3k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
300
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
340
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
SEO for Brand Visibility & Recognition
aleyda
0
4.4k
Navigating Weather and Climate Data
rabernat
0
150
ラッコキーワード サービス紹介資料
rakko
1
2.8M
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
エンジニアに許された特別な時間の終わり
watany
106
240k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
180
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
ありがとうございました。