Created
May 23, 2023 23:26
-
-
Save Kilo59/5c90947a3c5ebef271b5d081570605f7 to your computer and use it in GitHub Desktop.
Pydantic Type Coercion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"id": "dc7ad921-684f-4508-a8e8-8ab0b236a1a6", | |
"metadata": {}, | |
"source": [ | |
"## Dict -> Object Coercion with pydantic and @validate_arguments\n", | |
"\n", | |
"https://docs.pydantic.dev/latest/usage/validation_decorator/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "c1dacf4e-7f38-4913-9deb-c2137a7f01c8", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"from typing import Literal\n", | |
"from pydantic import BaseModel, validate_arguments\n", | |
"\n", | |
"\n", | |
"class MyClass(BaseModel):\n", | |
" foo: Literal[\"bar\"]\n", | |
" fizzbuzz: dict[str, list[int]]\n", | |
"\n", | |
"\n", | |
"@validate_arguments\n", | |
"def add_my_class(dict_or_object: MyClass):\n", | |
" print(repr(dict_or_object))\n", | |
" print()\n", | |
" print(dict_or_object.dict())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "0b81ecf6-defb-4733-b5ed-ade64e4c38d4", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"MyClass(foo='bar', fizzbuzz={'buzz': [3, 6], 'fizz': [5, 10]})\n", | |
"\n", | |
"{'foo': 'bar', 'fizzbuzz': {'buzz': [3, 6], 'fizz': [5, 10]}}\n" | |
] | |
} | |
], | |
"source": [ | |
"add_my_class({\"foo\": \"bar\", \"fizzbuzz\": {\"buzz\": [3, \"6\"], \"fizz\": [\"5\", 10]}})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "9270a74d-660d-4936-9f3e-0cdd2b4e943d", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"MyClass(foo='bar', fizzbuzz={})\n", | |
"\n", | |
"{'foo': 'bar', 'fizzbuzz': {}}\n" | |
] | |
} | |
], | |
"source": [ | |
"my_class = MyClass(foo=\"bar\", fizzbuzz={})\n", | |
"add_my_class(my_class)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "efa38566-ab6f-4ccd-b2b3-fb7a17629ecb", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"ename": "ValidationError", | |
"evalue": "2 validation errors for AddMyClass\ndict_or_object -> foo\n unexpected value; permitted: 'bar' (type=value_error.const; given=BAR; permitted=('bar',))\ndict_or_object -> fizzbuzz\n field required (type=value_error.missing)", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mValidationError\u001b[0m Traceback (most recent call last)", | |
"Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43madd_my_class\u001b[49m\u001b[43m(\u001b[49m\u001b[43m{\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmissing\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfields\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfoo\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m:\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mBAR\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m}\u001b[49m\u001b[43m)\u001b[49m\n", | |
"File \u001b[0;32m~/.local/share/virtualenvs/cloud-dogfood-FpNSVJLB/lib/python3.10/site-packages/pydantic/decorator.py:40\u001b[0m, in \u001b[0;36mpydantic.decorator.validate_arguments.validate.wrapper_function\u001b[0;34m()\u001b[0m\n", | |
"File \u001b[0;32m~/.local/share/virtualenvs/cloud-dogfood-FpNSVJLB/lib/python3.10/site-packages/pydantic/decorator.py:133\u001b[0m, in \u001b[0;36mpydantic.decorator.ValidatedFunction.call\u001b[0;34m()\u001b[0m\n", | |
"File \u001b[0;32m~/.local/share/virtualenvs/cloud-dogfood-FpNSVJLB/lib/python3.10/site-packages/pydantic/decorator.py:130\u001b[0m, in \u001b[0;36mpydantic.decorator.ValidatedFunction.init_model_instance\u001b[0;34m()\u001b[0m\n", | |
"File \u001b[0;32m~/.local/share/virtualenvs/cloud-dogfood-FpNSVJLB/lib/python3.10/site-packages/pydantic/main.py:341\u001b[0m, in \u001b[0;36mpydantic.main.BaseModel.__init__\u001b[0;34m()\u001b[0m\n", | |
"\u001b[0;31mValidationError\u001b[0m: 2 validation errors for AddMyClass\ndict_or_object -> foo\n unexpected value; permitted: 'bar' (type=value_error.const; given=BAR; permitted=('bar',))\ndict_or_object -> fizzbuzz\n field required (type=value_error.missing)" | |
] | |
} | |
], | |
"source": [ | |
"add_my_class({\"missing\": \"fields\", \"foo\": \"BAR\"})" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.10.10" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment