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
andOptional
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
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
Algorithm | Hash digest | |
---|---|---|
SHA256 |
89723c98bf579dd2c479a4db888b7d72ec90fef68615af202ca9192e92392ca4
|
|
MD5 |
fe767d542be1b5914aad429c1dca3d1d
|
|
BLAKE2b-256 |
2214b95418eebb8dfc30d2d8bf4feb394a08c86a139d7766b86ba37dfa939134
|
Provenance
The following attestation bundles were made for objectify-1.1.0.tar.gz
:
Publisher:
release.yml
on anviks/objectify
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
objectify-1.1.0.tar.gz
-
Subject digest:
89723c98bf579dd2c479a4db888b7d72ec90fef68615af202ca9192e92392ca4
- Sigstore transparency entry: 263979880
- Sigstore integration time:
-
Permalink:
anviks/objectify@020b8649d4142ce1a456dcf1306ddda87d50130c
-
Branch / Tag:
refs/tags/v1.1.0
- Owner: https://github.com/anviks
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
release.yml@020b8649d4142ce1a456dcf1306ddda87d50130c
-
Trigger Event:
push
-
Statement type:
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
Algorithm | Hash digest | |
---|---|---|
SHA256 |
7a5a39b62c3e249287a7346c2e15c0e81549653d2dad87956317c7e432a7de3e
|
|
MD5 |
392750e2e661f10b62d2d21b5d910200
|
|
BLAKE2b-256 |
bd372a86da46a055284e5fc13d9c101e025a94a3bbbab61d09b7ac1babd2e8f5
|
Provenance
The following attestation bundles were made for objectify-1.1.0-py3-none-any.whl
:
Publisher:
release.yml
on anviks/objectify
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
objectify-1.1.0-py3-none-any.whl
-
Subject digest:
7a5a39b62c3e249287a7346c2e15c0e81549653d2dad87956317c7e432a7de3e
- Sigstore transparency entry: 263979886
- Sigstore integration time:
-
Permalink:
anviks/objectify@020b8649d4142ce1a456dcf1306ddda87d50130c
-
Branch / Tag:
refs/tags/v1.1.0
- Owner: https://github.com/anviks
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
release.yml@020b8649d4142ce1a456dcf1306ddda87d50130c
-
Trigger Event:
push
-
Statement type: