jcrist / msgspec

A fast serialization and validation library, with builtin support for JSON, MessagePack, YAML, and TOML
https://jcristharif.com/msgspec/
BSD 3-Clause "New" or "Revised" License
2.3k stars 67 forks source link

Add support for Python 3.12's `type` aliases #606

Closed jcrist closed 10 months ago

jcrist commented 10 months ago

This adds support for the new syntactic type aliases added in Python 3.12. A few examples:

type NullableStr = str | None

type Pair[T] = tuple[T, T]

type NullableStrPair = Pair[NullableStr]

msgspec now supports these type aliases, except in cases where the type alias is recursive. For example, the following type isn't supported:

type Link[T] = tuple[T, Link[T] | None]

The internal datastructure we use to store type information was not designed to handle recursive types like these; supporting them will require a larger refactor.

Fixes #579.