No project description provided
Project description
Validates Python’s TypedDict
日本語の説明はこちら / Description in Japanese
About
This is a utility to validate a dict object using TypedDict. It recursively checks whether the dict has necessary keys and the values has appropriate type. It will be useful when you deserialize the json or yaml data, such as an API request or any file.
Currently supported types are generic types and Union (including Optional).
Requirements
Python 3.10 or higher
Installation
https://pypi.org/project/typeddict-validator/
pip install typeddict-validator
Usage
>>> from typing import TypedDict
>>> from typeddict_validator import validate_typeddict
>>>
>>> PersonDict = TypedDict("PersonDict", {"name": str, "age": int, "interests": list[str]})
>>>
>>> person = {"name": "Taro Yamada", "age": 30, "interests": ["programming", "painting"]}
>>>
>>> if validate_typeddict(person, PersonDict):
... print("It's a PersonDict!")
It's a PersonDict!
By default, it will raise an error when the dict object does not match.
>>> robot = {"name": "Doraemon"}
>>>
>>> if validate_typeddict(robot, PersonDict):
... print("It's a PersonDict!")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/app/typeddict_validator/validate.py", line 37, in validate_typeddict
raise DictMissingKeyException(key=k)
typeddict_validator.validate.DictMissingKeyException
You can use silent=True option not to raise an error.
>>> if not validate_typeddict(robot, PersonDict, silent=True):
... print("It's not a PersonDict!!")
It's not a PersonDict!!
See typeddict_validator/validate_test.py for more examples.
Tests
docker run --mount source=$(pwd),target=/app,type=bind python:latest python3 -m unittest -v /app/typeddict_validator/validate_test.py
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file typeddict_validator-0.2.0.tar.gz.
File metadata
- Download URL: typeddict_validator-0.2.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d54a0037bda12530ab150caa1362e40219dd46f3789fb1004bbc02db604f4a9c
|
|
| MD5 |
f1d6be314dc5c3919709cca29d486fb2
|
|
| BLAKE2b-256 |
77b0707a7dc809e56f501e08163d61c478f9975aab0b8fc2bd1e1e586230e95e
|
File details
Details for the file typeddict_validator-0.2.0-py3-none-any.whl.
File metadata
- Download URL: typeddict_validator-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3ef7cff8affad872ab97e30f2e420b02a15a8bd7aca2c1e261d46a56c7efc14
|
|
| MD5 |
6a01ccdf9af12d1d20e76c2714606046
|
|
| BLAKE2b-256 |
7c7869463568bcf9f305d6a21ffb9f98eccd5edc72e9b98a3f6166535ee1915c
|