Skip to content

leading underscores and "camel" rename strategy #618

Closed

Description

@peterschutt

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'
>>> 

Activity

  1. jcrist commented on Dec 20, 2023

    @jcrist
    Member

    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.

  2. peterschutt commented on Dec 21, 2023

    @peterschutt
    ContributorAuthor

    Thanks @jcrist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions