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.44k stars 75 forks source link

Use eval_type_backport on Python 3.9 if it's installed to resolve `int | None` etc. #773

Open ashb opened 1 week ago

ashb commented 1 week ago

This uses the same module that pydantic does, and it allows people to use the new pipe syntax if they have to support Python3.9 too -- very useful for libraries.

(Also it works better with many type checkers which seem to mistakenly think that with from __future__ import annotations means int| None will work, but it doesn't out of the box.)

Fixes #771

ashb commented 1 week ago

@jcrist PTAL. I've put in a place for making it installed or not, but didn't do anything in this PR to run it with or without this.

Let me know if you'd like the structure something else too. Or I'm more than happy for you to push changes to my fork too.

ashb commented 1 week ago

(I thought about adding the test for this in test_common with a decoder, but this seemed to be more directly testing the code)