Skip to main content

Load and dump data from json-like format into typed data structures

Project description

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]
  • 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.

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

typedload-2.39.tar.gz (79.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

typedload-2.39-py3-none-any.whl (56.3 kB view details)

Uploaded Python 3

File details

Details for the file typedload-2.39.tar.gz.

File metadata

  • Download URL: typedload-2.39.tar.gz
  • Upload date:
  • Size: 79.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for typedload-2.39.tar.gz
Algorithm Hash digest
SHA256 bbeb448f4d8f11236bd9471adb53a1c4113a7e235626521dcebc558e3920bb46
MD5 08cb44f8da38aa562f27d99cd4b47f27
BLAKE2b-256 5870fec3f45d83fe5cff2e03c2f1b4ae1d4e0b9c14f046e57a9e0320439b0971

See more details on using hashes here.

File details

Details for the file typedload-2.39-py3-none-any.whl.

File metadata

  • Download URL: typedload-2.39-py3-none-any.whl
  • Upload date:
  • Size: 56.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for typedload-2.39-py3-none-any.whl
Algorithm Hash digest
SHA256 8e4a58a2e00e519b50fc9ac75ab8e6a2c1e5826835ac4ebfe661a909bfa79068
MD5 2e0960282ed99163e9398b1d3e89f3e9
BLAKE2b-256 1db3b21f88a867f81fc98faf7bbc4a67e4be0bc335f5c88a0451c8bf586c6f67

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page