Skip to main content

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

typeddict_validator-0.2.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typeddict_validator-0.2.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

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

Hashes for typeddict_validator-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d54a0037bda12530ab150caa1362e40219dd46f3789fb1004bbc02db604f4a9c
MD5 f1d6be314dc5c3919709cca29d486fb2
BLAKE2b-256 77b0707a7dc809e56f501e08163d61c478f9975aab0b8fc2bd1e1e586230e95e

See more details on using hashes here.

File details

Details for the file typeddict_validator-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for typeddict_validator-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3ef7cff8affad872ab97e30f2e420b02a15a8bd7aca2c1e261d46a56c7efc14
MD5 6a01ccdf9af12d1d20e76c2714606046
BLAKE2b-256 7c7869463568bcf9f305d6a21ffb9f98eccd5edc72e9b98a3f6166535ee1915c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page