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
to_snake
alias generator bug -- great first issue
#8315
Comments
to_snake
alias generator bug -- great first issue
Thanks for reporting this. I'd agree, we have a bug in our In the second case, I don't think that the camel case alias generator is doing anything wrong. You wouldn't want |
@sydney-runkle Thanks for taking a look! I made my very first PR to pydantic regarding I'd love to get some eyes on it. In my second case, I was actually expecting it to work both ways with: f = Foo.model_validate({"HTTPResponseCode": "200"})
f = Foo.model_validate({"httpResponseCode": "200"}) since the camel_case equivalent of both I can't really validate that this is the expected behavior though. |
PR looks great - I've approved and merged 🚀. The |
Note that number management has been changed with this issue resolution. def to_snake_old(camel: str) -> str:
snake = re.sub(r'([a-zA-Z])([0-9])', lambda m: f'{m.group(1)}_{m.group(2)}', camel)
snake = re.sub(r'([a-z0-9])([A-Z])', lambda m: f'{m.group(1)}_{m.group(2)}', snake)
return snake.lower()
def to_snake_new(camel: str) -> str:
# Handle the sequence of uppercase letters followed by a lowercase letter
snake = re.sub(r'([A-Z]+)([A-Z][a-z])', lambda m: f'{m.group(1)}_{m.group(2)}', camel)
# Insert an underscore between a lowercase letter and an uppercase letter
snake = re.sub(r'([a-z])([A-Z])', lambda m: f'{m.group(1)}_{m.group(2)}', snake)
# Insert an underscore between a digit and an uppercase letter
snake = re.sub(r'([0-9])([A-Z])', lambda m: f'{m.group(1)}_{m.group(2)}', snake)
# Insert an underscore between a lowercase letter and a digit
snake = re.sub(r'([a-z])([0-9])', lambda m: f'{m.group(1)}_{m.group(2)}', snake)
return snake.lower()
print(to_snake_old('ZONE1_F')) # 'zone_1_f'
print(to_snake_new('ZONE1_F')) # 'zone1_f' I'm not sure if this was intended and which one should be preferred though as literature about snake case does not talk much about numbers. An option could be to add more alias_generators with different policies. @sydney-runkle: any feedback on this? |
Initial Checks
Description
Issue Summary:
When using the to_snake alias generator from pydantic.alias_generators in Pydantic v2, it appears that CamelCase strings are converted to lowercase without the inclusion of underscores between words. This behavior is unexpected and differs from the conventional understanding of snake_case formatting.
Steps to Reproduce:
Import
to_snake
from pydantic.alias_generators.Apply
to_snake
to a CamelCase string, such as "HTTPResponse".Expected Result:
The expected output for
to_snake("HTTPResponse")
should be "http_response", maintaining the standard snake_case convention where words are separated by underscores and converted to lowercase.Actual Result:
The actual output is "httpresponse", which is simply the input string converted to lowercase without any underscores separating the words.
Additional Notes
I also have the same issue when using
to_camel
alias generator when populating by name.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: