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.45k stars 76 forks source link

leading underscores and `"camel"` rename strategy #618

Closed peterschutt closed 11 months ago

peterschutt commented 11 months ago

Description

Hi there and as always, thanks for the work you put into msgspec!

I'm decoding from a source that uses camelCase names, but also includes a "_links" key.

Decoding into a struct with rename="camel" results in a ValidationError for that field.

Easy as pie workaround is to declare = msgspec.field(name="_links"), so no blocker, but I wanted to check that this is the intended behavior.

Python 3.12.0 (main, Oct  7 2023, 15:26:19) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import msgspec
>>> class MyStruct(msgspec.Struct, rename="camel"):
...   _field: str
... 
>>> msgspec.json.decode(b'{"_field":"value"}', type=MyStruct)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
msgspec.ValidationError: Object missing required field `field`
>>> msgspec.__version__
'0.18.5'

One related data point is that the inflection library doesn't strip leading underscores when camelizing:

Python 3.12.0 (main, Oct  7 2023, 15:26:19) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inflection
>>> inflection.camelize("_links")
'_links'
>>> 
jcrist commented 11 months ago

This was "intended behavior" in that we had a test checking for this behavior, but it also wasn't extremely well thought out :). I agree that preserving leading underscores would be more ergonomic in these cases, and am happy to make a small change to fix this. Should be resolved by #620.

peterschutt commented 11 months ago

Thanks @jcrist!