Skip to main content

A library for creating, and working with models that can represent incomplete information.

Project description

Wanting is a library for creating, and working with models that can represent incomplete information.

Motivation

Instances of domain models don’t always spring into existence fully formed. They may be partially constructed intially, then filled in over time. Making a model field optional that is not intially available, but eventually required is inaccurate because an optional field may always be optional, so it never has to be filled in. It would be better to make the field a required union of the type it wants, and a placholder type. The wanting types are such placeholders. They can include metadata, such as the source of the update with missing data, and even partial data from that source.

Usage

There are two wanting types that may be unioned with the type of a field. When a field is Unavailable, no information about that field is known. When a field is Unmapped, there is information about that field, but we are unable to map that information to a value that the model will accept.

A domain model may look like this:

from typing import Literal

import pydantic
import wanting


class User(pydantic.BaseModel):
    """A model that can have incomplete information."""

    name: str
    employee_id: str | wanting.Unavailable
    department_code: Literal["TECH", "FO", "BO", "HR"] | wanting.Unmapped

Then there is an onboarding system that creates a User. However, the employee_id is unavailable at this time because it will be generated later. The onboarding system sources the department code from some other system, which uses different values than those in the User model. The onboarding system knows how to map some of the codes from the other system to the User department codes, but not all of them. However, because employee_id, and department_code are unioned with wanting fields, the onboarding system can still create a fully valid model, with the information it knows:

user = User(
    name="Charlotte",
    employee_id=wanting.Unavailable(source="onboarding"),
    department_code=wanting.Unmapped(source="onboarding", value="art"),
)

The model validates, and all the wanting fields serialize to valid JSON:

assert user.model_dump() == {
    "name": "Charlotte",
    "employee_id": {
        "kind": "unavailable",
        "source": "onboarding",
        "value": {"serialized": b"null"},
    },
    "department_code": {
        "kind": "unmapped",
        "source": "onboarding",
        "value": {"serialized": b'"art"'},
    },
}

This user can now be persisted, then queried, and updated later by other systems.

A model class can be queried for its potentially wanting fields:

class Child(pydantic.BaseModel):
    """A model that can have incomplete information."""

    regular: int
    wanting: int | wanting.Unavailable


class Parent(pydantic.BaseModel):
    """A model that can have top-level, and nested incomplete information."""

    regular: int
    wanting: int | wanting.Unavailable
    nested: Child


def reduce_path(path: list[wanting.FieldInfoEx]) -> str:
    """Reduce the FieldInfoEx objects that comprise a path to a readable string."""
    return "->".join(f"{fi.cls.__name__}.{fi.name}" for fi in path)


paths = wanting.wanting_fields(Parent)
summary = [reduce_path(path) for path in paths]
assert summary == ["Parent.wanting", "Parent.nested->Child.wanting"]

A model instance can be queried for its wanting values:

p = Parent(
    regular=1,
    wanting=2,
    nested=Child(regular=3, wanting=wanting.Unavailable(source="doc")),
)
assert wanting.wanting_values(p) == {
    "nested": {"wanting": wanting.Unavailable(source="doc")}
}

A model instance can also be serialized, either including or excluding its wanting values:

incex = wanting.wanting_incex(p)
assert p.model_dump(include=incex) == {
    "nested": {
        "wanting": {
            "kind": "unavailable",
            "source": "doc",
            "value": {"serialized": b"null"},
        }
    }
}
assert p.model_dump(exclude=incex) == {
    "regular": 1,
    "wanting": 2,
    "nested": {"regular": 3},
}

Model serialization with respect to wanting fields is invertible. A model can be serialized, then the result can be deserialized back into an equivalent model.

p2 = Parent.model_validate(p.model_dump())
assert p == p2

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

wanting-0.11.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

wanting-0.11.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file wanting-0.11.0.tar.gz.

File metadata

  • Download URL: wanting-0.11.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for wanting-0.11.0.tar.gz
Algorithm Hash digest
SHA256 5c1149846fe38e621a7c26bbe0ae1c5509f61384d0860dc26ac13c90a633b1c0
MD5 c1784ef979c90a3f18f753f10c12e5ad
BLAKE2b-256 70d551b9f9e67476f64b22221f0b08438539f7217813c95b542eda5e4ae91c10

See more details on using hashes here.

File details

Details for the file wanting-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: wanting-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for wanting-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c12f41ab1480bed8bc87bc3e93152a9e3f9acdc3a33c95bf077185f671c04e6
MD5 fcf380d107f4496fb9ed3b991fae524b
BLAKE2b-256 6901b36da8a03484c0095804c6907c30a05bed4ced0e156a1410e5fd0adaabf5

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