Skip to main content

typedload

Load and dump json-like data into typed data structures in Python3, enforcing a schema on the data.

This module provides an API to load dictionaries and lists (usually loaded from json) into Python's NamedTuples, dataclass, sets, enums, and various other typed data structures; respecting all the type-hints and performing type checks or casts when needed.

It can also dump from typed data structures to json-like dictionaries and lists.

It is very useful for projects that use Mypy and deal with untyped data like json, because it guarantees that the data will follow the specified schema.

It is released with a GPLv3 license but it is possible to ask for LGPLv3.

GPLv3 logo

Donate to LtWorf

Example

For example this dictionary, loaded from a json:

data = {
    'users': [
        {
            'username': 'salvo',
            'shell': 'bash',
            'sessions': ['pts/4', 'tty7', 'pts/6']
        },
        {
            'username': 'lop'
        }
    ],
}

Can be treated more easily if loaded into this type:

@dataclasses.dataclass
class User:
    username: str
    shell: str = 'bash'
    sessions: List[str] = dataclasses.field(default_factory=list)

class Logins(NamedTuple):
    users: List[User]

And the data can be loaded into the structure with this:

t_data = typedload.load(data, Logins)

And then converted back:

data = typedload.dump(t_data)

Supported types

Since this is not magic, not all types are supported.

The following things are supported:

  • Basic python types (int, str, bool, float, NoneType)
  • NamedTuple
  • Enum
  • Optional[SomeType]
  • List[SomeType]
  • Dict[TypeA, TypeB]
  • Tuple[TypeA, TypeB, TypeC] and Tuple[SomeType, ...]
  • Set[SomeType]
  • Union[TypeA, TypeB]
  • dataclass
  • attr.s
  • ForwardRef (Refer to the type in its own definition)
  • Literal
  • TypedDict
  • datetime.date, datetime.time, datetime.datetime
  • re.Pattern
  • Path
  • IPv4Address, IPv6Address
  • typing.Any
  • typing.NewType
  • uuid.UUID

Unions

typedload works fine with untagged unions. However using Literal fields to tag them makes it much faster.

Using Mypy

Mypy and similar tools work without requiring any plugins.

# This is treated as Any, no checks done.
data = json.load(f)

# This is treated as Dict[str, int]
# but there will be runtime errors if the data does not
# match the expected format
data = json.load(f)  # type: Dict[str, int]

# This is treated as Dict[str, int] and an exception is
# raised if the actual data is not Dict[str, int]
data = typedload.load(json.load(f), Dict[str, int])

So when using Mypy, it makes sense to make sure that the type is correct, rather than hoping the data will respect the format.

Extending

Type handlers can easily be added, and existing ones can be replaced, so the library is fully cusomizable and can work with any type.

Inheriting a base class is not required.

Install

  • pip install typedload
  • apt install python3-typedload
  • Latest and greatest .deb file is in releases

Documentation

The tests are hard to read but provide more in depth examples of the capabilities of this module.

Used by

As dependency, typedload is used by those entities. Feel free to add to the list.

Release files for typedload 2.29

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

Source distribution (sdist)

Source distribution for typedload 2.29
File Size Uploaded
typedload-2.29.tar.gz 76.2 kB Details

Built distribution (wheel)

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

Total release size: 129.9 kB

Release files / typedload-2.29.tar.gz

Download URL typedload-2.29.tar.gz
Size 76.2 kB
Tags Source
SHA-256 checksum
How to use checksums
b24446b9d8a5002caf3a1a30ce6f231ae7a228bf0326ead9eb59f76fd146bab1
BLAKE2b-256 checksum
How to use checksums
2071734f608d77a6614e491e732affc45e8229131bf293cbd4e394ffa4061630
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.9

Release files / typedload-2.29-py3-none-any.whl

Download URL typedload-2.29-py3-none-any.whl
Size 53.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4b2d0a0c88a9f830d4b1aacef62024f3f66d01d09be9bdafb81eb21c23e021a0
BLAKE2b-256 checksum
How to use checksums
8b2fa12c3d37521c53b6b77ff59eaeb7f618e3a3447c6b6bf7b4e87d60c7c1f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

2.41

2 release files

2.40

2 release files

2.39

2 release files

2.37

2 release files

2.36

2 release files

2.35

2 release files

2.34

2 release files

2.33

2 release files

2.32

2 release files

2.31

2 release files

2.30

2 release files

This release

2.29 This release

2 release files

2.28

2 release files

2.27

2 release files

2.26

2 release files

2.25

2 release files

2.24

2 release files

2.23

2 release files

2.22

2 release files

2.21

2 release files

2.20

1 release file

2.19

1 release file

2.18

1 release file

2.17

1 release file

2.16

1 release file

2.15

1 release file

2.14

1 release file

2.13

1 release file

2.12

1 release file

2.11

1 release file

2.10

1 release file

2.9

1 release file

2.8

1 release file

2.7

1 release file

2.6

1 release file

2.5

1 release file

2.4

1 release file

2.3

1 release file

2.2

1 release file

2.1

1 release file

2.0

1 release file

1.20

1 release file

1.19

1 release file

1.18

1 release file

1.17

1 release file

1.16

1 release file

1.15

1 release file

1.14

1 release file

1.13

1 release file

1.12

1 release file

1.11

1 release file

1.10

1 release file

1.8

1 release file

1.7

1 release file

1.6

1 release file

1.5

1 release file

1.4

1 release file

1.3

1 release file

1.2

1 release file

1.1

1 release file

1.0

1 release file

0.9

1 release file

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