Skip to content

In generated field name, an undescore is introduced after a number #657

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

Open
3 tasks done
YodaEmbedding opened this issue Jan 27, 2025 · 1 comment · May be fixed by #662
Open
3 tasks done

In generated field name, an undescore is introduced after a number #657

YodaEmbedding opened this issue Jan 27, 2025 · 1 comment · May be fixed by #662
Labels
bug Something isn't working investigation needed

Comments

@YodaEmbedding
Copy link

Summary

Underscore is added after a number in the field name, e.g. c2s becomes c2_s.

Reproduction Steps

syntax = "proto3";

message Example {
  string c2s = 1;
}
protoc --python_betterproto_out=lib example.proto

Expected Results

Expecting c2s.

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: example.proto
# plugin: python-betterproto
# This file has been @generated

from dataclasses import dataclass

import betterproto


@dataclass(eq=False, repr=False)
class Example(betterproto.Message):
    c2s: str = betterproto.string_field(1)

Actual Results

Generates c2_s instead.

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: example.proto
# plugin: python-betterproto
# This file has been @generated

from dataclasses import dataclass

import betterproto


@dataclass(eq=False, repr=False)
class Example(betterproto.Message):
    c2_s: str = betterproto.string_field(1)

System Information

libprotoc 27.1
Python 3.8.20
Name: betterproto
Version: 2.0.0b7
Location: /Users/.../demo/.venv/lib/python3.8/site-packages
Requires: grpclib, python-dateutil, typing-extensions
Required-by: demo

Checklist

  • I have searched the issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have verified this issue occurs on the latest prelease of betterproto which can be installed using pip install -U --pre betterproto, if possible.
@YodaEmbedding YodaEmbedding added bug Something isn't working investigation needed labels Jan 27, 2025
@YodaEmbedding
Copy link
Author

YodaEmbedding commented Mar 16, 2025

Proposed fix:

diff --git a/src/betterproto/casing.py b/src/betterproto/casing.py
index f7d0832..d09c708 100644
--- a/src/betterproto/casing.py
+++ b/src/betterproto/casing.py
@@ -8,11 +8,11 @@ SYMBOLS = "[^a-zA-Z0-9]*"
 
 # Optionally capitalized word.
 # language=PythonRegExp
-WORD = "[A-Z]*[a-z]*[0-9]*"
+WORD = "[A-Z]*([a-z]|[0-9][A-Z]*)*"
 
 # Uppercase word, not followed by lowercase letters.
 # language=PythonRegExp
-WORD_UPPER = "[A-Z]+(?![a-z])[0-9]*"
+WORD_UPPER = "([A-Z]+(?![a-z])[0-9]*)+"
 
 
 def safe_snake_case(value: str) -> str:

Tests:

>>> betterproto.casing.snake_case("C2S2C c2s2c")
'c2s2c_c2s2c'

>>> betterproto.casing.snake_case("Client2Server CLIENT2SERVER client2Server")
'client2server_client2server_client2server'

>>> betterproto.casing.snake_case("clientToServer")
'client_to_server'

Much nicer. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working investigation needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant