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.3.0.tar.gz (12.1 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.3.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file typeddict_validator-0.3.0.tar.gz.

File metadata

  • Download URL: typeddict_validator-0.3.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for typeddict_validator-0.3.0.tar.gz
Algorithm Hash digest
SHA256 923865f4a14a6aaf67fef3ce45c2831f92a82f2f082c605a1a03d710006fcf15
MD5 d700cea24e90706e1e2a99a96770ca27
BLAKE2b-256 e0215c2ac6b4a98233c5b87f7149e78803bd3c304c137812fe50d9b9324f6cf8

See more details on using hashes here.

Provenance

The following attestation bundles were made for typeddict_validator-0.3.0.tar.gz:

Publisher: publish.yml on SogoKato/typeddict-validator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for typeddict_validator-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 271f2df6581559b1f037279f788651a07ac41a892d0ea5d37b940b14fbe7e4fd
MD5 941e7e0d2bf65cd0fee76001a2d51c2b
BLAKE2b-256 3b2834d266b6b15e559ecd34c35bab3d51a6105777be03775d358d62ffb7e395

See more details on using hashes here.

Provenance

The following attestation bundles were made for typeddict_validator-0.3.0-py3-none-any.whl:

Publisher: publish.yml on SogoKato/typeddict-validator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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