Skip to main content

todo

Project description

Micromodel

Static and runtime dictionary validation.

Install

$ pip install micromodel

Why

We had a HUGE Python code base which was using pydantic to provide a validation layer for MongoDB operations. The code was all messy but it worked fine, until we decided to switch the type checker config from "basic" to "strict", then over a thousand of dictionary-related errors popped up, not to mention the annoying conversions from dict to classes that were going on on every validation and that should be checked everytime.

We then decided to make this validation in-loco using a more vanilla approach with only TypedDicts. Now our dictionaries containing MongoDB documents are consistently dicts that match with the static typing.

Usage

import typing
from micromodel import model

Animal = typing.TypedDict('Animal', {
    'name': str,
    'specie': list[typing.Literal[
        'dog',
        'cat',
        'bird'
    ]]
})

# even another TypedDicts can be used!
Person = typing.TypedDict('Person', {
    'name': str,
    'age': int,
    'animal': Animal
})

m = model(Person, {
    'Animal': Animal
})

old_validate = m.validate
def new_validate(target: Person):
    new = target.copy()
    new['name'] = new['name'].capitalize()
    return validate(Person, typing.cast(typing.Any, new))

# hooks can be implemented using monkeypatching
m.validate = new_validate

result = m.validate({
    'name': 'joao',
    'animal': {
        'name': 'thor',
        'specie': [
            'dog',
            # 'turtle' (this would produce both static and runtime errors)
        ]
    }
})

"""
{
  "name": "Joao",
  "animal": {
    "name": "thor",
    "specie": [
      "dog"
    ]
  }
}
"""
print(result)

License

This library is MIT licensed.

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

micromodel-0.0.0.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

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

micromodel-0.0.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file micromodel-0.0.0.tar.gz.

File metadata

  • Download URL: micromodel-0.0.0.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for micromodel-0.0.0.tar.gz
Algorithm Hash digest
SHA256 3ea146ec99b039208d5a444b189f9981ecc4202c22d537238ab35ec62713b135
MD5 9a56a26f7ecd7988d1f64b73339102ef
BLAKE2b-256 6523b6edccb47454947b76aefd07f6fdc744b8ba54fb618e8df9aa8c27c19d23

See more details on using hashes here.

File details

Details for the file micromodel-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: micromodel-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for micromodel-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 68da5ca267f80a91a7438d3a310c0a4d09ef08b41ac2e50332047dc7a74501f8
MD5 df93b67df880dfe38938f8871c8a4667
BLAKE2b-256 b9fd5f801d6f35d0fbb79b196a65c5aeefd17462d36bd0e25e951e6c7f7a5bf4

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