msgpack-types

0.7.0 · active · verified Sun Apr 12

msgpack-types is a Python library that provides type stubs for the popular 'msgpack' serialization library. Its purpose is to enable static type checking and enhance developer tooling like autocompletion for code that interacts with MessagePack data. The current version is 0.7.0, and it is actively maintained with frequent releases, often in sync with or following updates to the core `msgpack` library.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the underlying `msgpack` library, with `msgpack-types` providing static type checking. After installing `msgpack-types`, a type checker like MyPy will analyze the `msgpack.packb` and `msgpack.unpackb` calls, ensuring correct argument types and inferring return types, as shown in the function signatures.

import msgpack

def process_data(data: dict) -> bytes:
    packed = msgpack.packb(data, use_bin_type=True)
    return packed

def decode_data(packed_data: bytes) -> dict:
    # raw=False decodes binary strings to Python str (UTF-8 assumed)
    unpacked = msgpack.unpackb(packed_data, raw=False)
    return unpacked # type: ignore

my_data = {'name': 'Alice', 'age': 30, 'cities': ['New York', 'London']}

# Using the functions with type hints
encoded = process_data(my_data)
decoded = decode_data(encoded)

print(f"Original: {my_data}")
print(f"Encoded: {encoded}")
print(f"Decoded: {decrypted}")

# Example of type checking with MyPy (after installing msgpack-types):
# If you were to do `process_data('not a dict')`, MyPy would flag a type error.

view raw JSON →