Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

leading underscores and "camel" rename strategy #618

Closed
peterschutt opened this issue Dec 18, 2023 · 2 comments · Fixed by #620
Closed

leading underscores and "camel" rename strategy #618

peterschutt opened this issue Dec 18, 2023 · 2 comments · Fixed by #620

Comments

@peterschutt
Copy link
Contributor

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
Copy link
Owner

jcrist commented Dec 20, 2023

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
Copy link
Contributor Author

Thanks @jcrist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants