Skip to main content

A package to convert a dictionary to an object to achieve similar syntax and auto-completion as in TypeScript when annotating objects with interfaces.

Project description

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'

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

objectify-1.1.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

objectify-1.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file objectify-1.1.0.tar.gz.

File metadata

  • Download URL: objectify-1.1.0.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for objectify-1.1.0.tar.gz
Algorithm Hash digest
SHA256 89723c98bf579dd2c479a4db888b7d72ec90fef68615af202ca9192e92392ca4
MD5 fe767d542be1b5914aad429c1dca3d1d
BLAKE2b-256 2214b95418eebb8dfc30d2d8bf4feb394a08c86a139d7766b86ba37dfa939134

See more details on using hashes here.

Provenance

The following attestation bundles were made for objectify-1.1.0.tar.gz:

Publisher: release.yml on anviks/objectify

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

File details

Details for the file objectify-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: objectify-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for objectify-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7a5a39b62c3e249287a7346c2e15c0e81549653d2dad87956317c7e432a7de3e
MD5 392750e2e661f10b62d2d21b5d910200
BLAKE2b-256 bd372a86da46a055284e5fc13d9c101e025a94a3bbbab61d09b7ac1babd2e8f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for objectify-1.1.0-py3-none-any.whl:

Publisher: release.yml on anviks/objectify

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 Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page