Skip to main content

pydantic_dict

A pydantic model subclass that implements Python's dictionary interface.

Example:

from pydantic_dict import BaseModelDict

class User(BaseModelDict):
    id: int
    name: str = "Jane Doe"

user = User(id=42)

user["session_id"] = "95607c42-250a-4913-9dfb-00eb6535e685"
assert user.session_id == "95607c42-250a-4913-9dfb-00eb6535e685"
assert user["session_id"] == "95607c42-250a-4913-9dfb-00eb6535e685"
user.pop("session_id")
assert "session_id" not in user

assert user.get("last_name", None) is None

user.update({"email": "jane.doe@email.com"})
print(user.json())
# >>> {"id": 42, "name": "Jane Doe", "email": "jane.doe@email.com"}

user.clear()  # fields are NOT removed. only non-fields are removed
print(user.json())
# >>> {"id": 42, "name": "Jane Doe"}

user.setdefault("last_active", "2023-01-01T19:56:10Z")
del user["last_active"]

Unset marker type

The Unset marker type provides a way to "mark" that an optional model field is by default not set and is not required to construct the model. This enables more semantic usage of built-in dict methods like get() and setdefault() that can return or set a default value. Likewise, fields that are Unset are not considered to be members of a BaseModelDict dictionary (e.g. "unset_field" not in model_dict) and are not included in __iter__(), keys(), values(), or len(model_dict). This feature is especially useful when refactoring existing code to use pydantic.

Example:

from pydantic_dict import BaseModelDict, Unset
from typing import Optional

class User(BaseModelDict):
    id: int
    name: str = "Jane Doe"
    email: Optional[str] = Unset

user = User(id=42)

assert "email" not in user
user["email"] # raises KeyError

assert len(user) == 2
assert set(user.keys()) == {"id", "name"}

user.setdefault("email", f"{user.id}@service.com") # returns `42@service.com`
assert "email" in user

Install

pip install pydantic_dict

Metadata

Release files for pydantic-dict 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-dict 0.0.3
File Size Uploaded Provenance
pydantic_dict-0.0.3.tar.gz 13.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-dict 0.0.3
File Interpreter ABI Platform
pydantic_dict-0.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / pydantic_dict-0.0.3.tar.gz

Download URL pydantic_dict-0.0.3.tar.gz
Size 13.7 kB
Tags Source
SHA-256 checksum
How to use checksums
af492397a4fa255aa9e03105a7f57dc69effcd5e7be573b2e31374c3f9d1e429
BLAKE2b-256 checksum
How to use checksums
88d654df197ed4a9d33bb02645004c1aadd3b50760bb03947fb18740625349ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / pydantic_dict-0.0.3-py3-none-any.whl

Download URL pydantic_dict-0.0.3-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1bf99ee97cd314e871f306b79e29de711a4897b34d9c8810c09081b9f9259c74
BLAKE2b-256 checksum
How to use checksums
52f3b2ec888024b29b923a3bf0d86c5a182687bb20712ce3b66149fbdfb94933
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for pydantic-dict 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page