msgspec icon indicating copy to clipboard operation
msgspec copied to clipboard

Enforcing runtime type validation for Structs (and extending validation to functions)

Open mjkanji opened this issue 11 months ago • 9 comments

Description

Hi there!

Would it be possible to add an optional flag that enforces type validation for Structs at runtime? While I understand your rationale for the current behavior (static type checker + msgspec for external data is more performant), not all teams have the same level of maturity in terms of adopting best practices. More importantly, not all packages are 100% type-annotated, and even if they were, unlike in a statically typed language, there are no guarantees that the upstream implementation is actually accurate/adheres to those type hints.

In addition, having an optional feature like this could also allow msgspec to potentially serve as a replacement for something like typeguard by having a function decorator like Pydantic's validate_call.

Having the option for msgspec to serve as a fully-featured, more performant alternative to Pydantic (+ typeguard) would also allow the project to become more popular -- it would certainly encourage me to make the switch if these features were built-in, even under an optional flag somewhere.

Given that msgspec already has all the logic for type validation built-in, it just feels like a missed opportunity that something that would be relatively easy to add (correct me, if I'm wrong here) and could expand the user base/"target market" of the library quite significantly is not currently supported.

PS: I have also reviewed the only issue I could find with a similar request (which offers a great solution, but it's always nicer when something's built-in).

mjkanji avatar Aug 11 '23 22:08 mjkanji