lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.34k stars 150 forks source link

[BUG] SNAKE LetterCase conversion adding extra underscores for numeric parts #516

Closed jespBE closed 4 months ago

jespBE commented 4 months ago

Description

When declaring a dataclass with @dataclass_json(letter_case=SNAKE), it appears that name conversion is creating extraneous underscores for the output name like so: test_123 maps to test__1_2_3

Specifically, it seems to be double-underscoring between alphabetic and numeric characters separated by a single underscore, and adding an underscore between each numeric character.

I haven't been able to reproduce this behavior for other casings.

Code snippet that reproduces the issue

from dataclasses import (
    dataclass,
    asdict
)
from dataclasses_json import (
    dataclass_json,
    LetterCase
)
@dataclass_json(letter_case=LetterCase.SNAKE)
@dataclass
class Foo:
    test_123: str

@dataclass_json(letter_case=LetterCase.CAMEL)
@dataclass
class Bar:
    test_123: str

@dataclass_json()
@dataclass
class Baz:
    test_123: str

f = Foo(test_123=1)
asdict(f)
# '{"test_123": 1}'
f.to_dict()
# '{"test__1_2_3": 1}'    <--- issue present here

b = Bar(test_123=1)
asdict(b)
# '{"test_123": 1}'
b.to_dict()
# '{"test123": 1}'

z = Baz(test_123=1)
asdict(z)
# '{"test_123": 1}'
z.to_dict()
# '{"test_123": 1}'

Describe the results you expected

# I expect `test_123` to map to `test_123` like so:

from dataclasses import (
    dataclass,
    asdict
)
from dataclasses_json import (
    dataclass_json,
    LetterCase
)
@dataclass_json(letter_case=LetterCase.SNAKE)
@dataclass
class Foo:
    test_123: str

f = Foo(test_123=1)
asdict(f)
# '{"test_123": 1}'
f.to_dict()
# '{"test_123": 1}'   <--- not what actually prints

Python version you are using

Python 3.10.8

Environment description

dataclasses-json==0.6.4 marshmallow==3.20.2 mypy-extensions==1.0.0 packaging==23.2 typing-inspect==0.9.0 typing_extensions==4.9.0

jespBE commented 4 months ago

Closing because uploaded on wrong Github account, oops!