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.

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 (requires Python 3.7)

  • attr.s

  • ForwardRef (Refer to the type in its own definition)

  • Literal (requires Python 3.8)

  • TypedDict (requires Python 3.8)

  • datetime.date, datetime.time, datetime.datetime

  • Path

  • IPv4Address, IPv6Address

  • typing.Any

Using Mypy

>>>
# 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.

Install

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.

  • Several universities around the world

  • United States Air Force

  • Exxonmobil

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.10.tar.gz (14.4 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: typedload-2.10.tar.gz
  • Upload date:
  • Size: 14.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.4.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.7

File hashes

Hashes for typedload-2.10.tar.gz
Algorithm Hash digest
SHA256 ab6b06a7521e7ac0a47a72f290c1586d3236e4e93058f4a9e454f30629125967
MD5 ca642717dd06864be14a1864b94ec075
BLAKE2b-256 fedd651b28cf5b3af4b038f07604c1303c3b7761d4822003ca78e84cfe673358

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