Skip to main content

Objectify

Objectify is a lightweight library designed to convert a dictionary into a typed object based on a provided type argument. It allows developers to work seamlessly with dictionaries using dot notation while benefiting from type safety, type hinting, and IDE autocompletion.

The idea behind objectify was inspired by TypeScript, where you can take any object, define an interface or type for it, and "cast" the object to that type. This approach gives you immediate access to types and autocompletion, making writing code more efficient and less error-prone.

In Python, the closest existing standard solution is TypedDict, which provides type hints and key autocompletion for dictionaries. However, its main drawback is that you must still access values using square brackets and string keys (e.g., data['key']), which is less convenient and takes more time compared to dot notation (data.key).

This functionality is especially useful when working with large nested payloads (e.g., JSON responses from APIs). By leveraging objectify, you can easily map such data to Python objects while maintaining clarity and reducing errors.

Features

  • Type Safety: Objectify ensures that the object's attributes match the specified types.
  • Custom Classes: Objectify supports custom classes as type arguments, allowing for more complex object structures.
  • Dataclasses: Objectify works seamlessly with dataclasses, providing a simple way to convert dictionaries to dataclass objects.
  • Nested Objects: Objectify can handle nested dictionaries and convert them to nested objects.
  • Literal Types: Objectify supports Literal types for specifying exact values that an attribute can take.
  • Default Values: Objectify allows defining default values for attributes in class definitions, making it easier to handle optional fields.
  • Union and Optional: Objectify supports Union and Optional types, allowing for flexible type definitions.
  • Error Handling: Objectify raises TypeError for mismatched types and invalid literal values.

Installation

pip install objectify

Usage

Simple Example

from objectify import dict_to_object


class User:
    name: str
    age: int
    is_active: bool


data = {
    'name': 'Alice',
    'age': 30,
    'is_active': True
}

user = dict_to_object(data, User)
print(user.name)  # 'Alice'
print(user.age)  # 30
print(user.is_active)  # True

Complex Example

from objectify import dict_to_object


class Address:
    street: str
    city: str
    zip_code: int


class User:
    name: str
    age: int
    address: list[Address]


data = {
    'name': 'Alice',
    'age': 30,
    'address': [
        {'street': '123 Main St', 'city': 'Springfield', 'zip_code': 12345},
        {'street': '456 Elm St', 'city': 'Rivertown', 'zip_code': 54321}
    ]
}

user = dict_to_object(data, User)
print(user.name)  # 'Alice'
print(user.age)  # 30
print(user.address[0].street)  # '123 Main St'
print(user.address[1].city)  # 'Rivertown'

Metadata

Release files for objectify 1.1.0

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

Source distribution (sdist)

Source distribution for objectify 1.1.0
File Size Uploaded Provenance Actions
objectify-1.1.0.tar.gz 9.3 kB With provenance Details for objectify-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for objectify 1.1.0
File Interpreter ABI Platform Actions
objectify-1.1.0-py3-none-any.whl Python 3 none any Details for objectify-1.1.0-py3-none-any.whl

Total release size: 14.3 kB

Release files / objectify-1.1.0.tar.gz

Download URL objectify-1.1.0.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
89723c98bf579dd2c479a4db888b7d72ec90fef68615af202ca9192e92392ca4
BLAKE2b-256 checksum
How to use checksums
2214b95418eebb8dfc30d2d8bf4feb394a08c86a139d7766b86ba37dfa939134
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 4, 2025.

Transparency log

Release files / objectify-1.1.0-py3-none-any.whl

Download URL objectify-1.1.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a5a39b62c3e249287a7346c2e15c0e81549653d2dad87956317c7e432a7de3e
BLAKE2b-256 checksum
How to use checksums
bd372a86da46a055284e5fc13d9c101e025a94a3bbbab61d09b7ac1babd2e8f5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 4, 2025.

Transparency log

Security for objectify 1.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for objectify-1.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 4, 2025.

Transparency log

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.

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files Full provenance

0.0.1

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