Skip to main content

Python DTO

rogervila/py_dto

PyPI version PyPI - Downloads

Data Transfer Objects (DTO) with Python.

Install

pip install py_dto

Requires Python 3.9 or newer. Tested on Python 3.9 through 3.14.

Usage

Define the object properties with types defined, then pass a dict with data.

Basic example

For type hinting

from py_dto import DTO

# This DTO will be used as a type definition
class UserProfile(DTO):
    avatar: str

# The DTO with the properties defined
class User(DTO):
    profile: UserProfile
    name: str
    email: str
    age: int
    tags: list[str]

# Create the DTO instance
user = User({
    'profile': UserProfile({'avatar': 'https://i.pravatar.cc/300'}),
    'name': 'John',
    'email': 'john@example.com',
    'age': 42,
    'tags': ['developer', 'python']
})

print(user.name) # 'John'
print(user.profile.avatar) # https://i.pravatar.cc/300

The Any type

Even DTO are supposed to specify data types, you can use the Any type to accept literally any type for a property:

from py_dto import DTO
from typing import Any

# The DTO accepts "any" type of data for the "name" property
class User(DTO):
    name: Any

# Create the DTO instance
user = User({
    'name': 'John',
})

print(user.name) # 'John'

user = User({
    'name': 123,
})

print(user.name) # 123

Dealing with None

Imagine you are retrieving data from a database table where a column is empty for some records.

By using python's Optional type on a specific property, the DTO will not raise an exception if a None value is set.

from py_dto import DTO
from typing import Optional

# The DTO "name" property can be a str or a None value
class User(DTO):
    name: Optional[str]

# Create the DTO instance with a "str"
user = User({
    'name': 'John',
})

print(user.name) # 'John'

# Create the DTO instance with a "None"
user = User({
    'name': None,
})

print(user.name) # None

# Any other type will raise an exception
try:
    user = User({
        'name': 123,
    })
except:
    print('123 does not have a "str" nor a "None" type')

License

This project is open-sourced software licensed under the MIT license.

Original idea comes from spatie/data-transfer-object package for PHP.

Icons made by Pixel perfect from www.flaticon.com

Metadata

Release files for py-dto 1.2.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 py-dto 1.2.0
File Size Uploaded Provenance Actions
py_dto-1.2.0.tar.gz 5.5 kB Without provenance Details for py_dto-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-dto 1.2.0
File Interpreter ABI Platform Actions
py_dto-1.2.0-py3-none-any.whl Python 3 none any Details for py_dto-1.2.0-py3-none-any.whl

Total release size: 9.5 kB

Release files / py_dto-1.2.0.tar.gz

Download URL py_dto-1.2.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c8c04e46c85f1c1aa3320e29674ef37aeaf3f148211173448972964759e0208f
BLAKE2b-256 checksum
How to use checksums
a413423d809af2db17a13211a381e1ec9da4203149763bb0439c8cf469dd7261
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / py_dto-1.2.0-py3-none-any.whl

Download URL py_dto-1.2.0-py3-none-any.whl
Size 3.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d04d36ae642c2cab2fab38deae28ede96f95c6a287fb2a7215ca51f9562d5fac
BLAKE2b-256 checksum
How to use checksums
0fe6a754bd587efe9866b08807b47d7e6b8db262815420a40253f7fcdddaef79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Security for py-dto 1.2.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.

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