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

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

Open
jesp1999 opened this issue Feb 12, 2024 · 0 comments · May be fixed by #519
Open

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

jesp1999 opened this issue Feb 12, 2024 · 0 comments · May be fixed by #519
Labels
bug Something isn't working

Comments

@jesp1999
Copy link

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

@jesp1999 jesp1999 added the bug Something isn't working label Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant