Skip to content

Commit bd7de20

Browse files
Gobot1234nat-nw4rumabn
authored
Remove Enum prefixes (#187)
Co-authored-by: nat <n@natn.me> Co-authored-by: Tim Schmidt <w4rum@users.noreply.github.com> Co-authored-by: Arun Babu Neelicattu <arun.neelicattu@gmail.com>
1 parent d9b7608 commit bd7de20

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

src/betterproto/casing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,8 @@ def lowercase_first(value: str) -> str:
136136

137137
def sanitize_name(value: str) -> str:
138138
# https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles
139-
return f"{value}_" if keyword.iskeyword(value) else value
139+
if keyword.iskeyword(value):
140+
return f"{value}_"
141+
if not value.isidentifier():
142+
return f"_{value}"
143+
return value

src/betterproto/compile/naming.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ def pythonize_field_name(name: str) -> str:
1111

1212
def pythonize_method_name(name: str) -> str:
1313
return casing.safe_snake_case(name)
14+
15+
16+
def pythonize_enum_member_name(name: str, enum_name: str) -> str:
17+
enum_name = casing.snake_case(enum_name).upper()
18+
find = name.find(enum_name)
19+
if find != -1:
20+
name = name[find + len(enum_name) :].strip("_")
21+
return casing.sanitize_name(name)

src/betterproto/enum.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __new__(
5757
members = {
5858
name: value
5959
for name, value in namespace.items()
60-
if not _is_descriptor(value) and name[0] != "_"
60+
if not _is_descriptor(value) and not name.startswith("__")
6161
}
6262

6363
cls = type.__new__(
@@ -70,9 +70,6 @@ def __new__(
7070
# members become proper class variables
7171

7272
for name, value in members.items():
73-
if _is_descriptor(value) or name[0] == "_":
74-
continue
75-
7673
member = value_map.get(value)
7774
if member is None:
7875
member = cls.__new__(cls, name=name, value=value) # type: ignore

src/betterproto/plugin/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@
7272
)
7373
from betterproto.lib.google.protobuf.compiler import CodeGeneratorRequest
7474

75-
from ..casing import sanitize_name
7675
from ..compile.importing import (
7776
get_type_reference,
7877
parse_source_type_name,
7978
)
8079
from ..compile.naming import (
8180
pythonize_class_name,
81+
pythonize_enum_member_name,
8282
pythonize_field_name,
8383
pythonize_method_name,
8484
)
@@ -673,7 +673,9 @@ def __post_init__(self) -> None:
673673
# Get entries/allowed values for this Enum
674674
self.entries = [
675675
self.EnumEntry(
676-
name=sanitize_name(entry_proto_value.name),
676+
name=pythonize_enum_member_name(
677+
entry_proto_value.name, self.proto_obj.name
678+
),
677679
value=entry_proto_value.number,
678680
comment=get_comment(
679681
proto_file=self.source_file, path=self.path + [2, entry_number]

tests/inputs/enum/enum.proto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ enum Choice {
1515
FOUR = 4;
1616
THREE = 3;
1717
}
18+
19+
// A "C" like enum with the enum name prefixed onto members, these should be stripped
20+
enum ArithmeticOperator {
21+
ARITHMETIC_OPERATOR_NONE = 0;
22+
ARITHMETIC_OPERATOR_PLUS = 1;
23+
ARITHMETIC_OPERATOR_MINUS = 2;
24+
ARITHMETIC_OPERATOR_0_PREFIXED = 3;
25+
}

tests/inputs/enum/test_enum.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from tests.output_betterproto.enum import (
2+
ArithmeticOperator,
23
Choice,
34
Test,
45
)
@@ -102,3 +103,12 @@ def test_enum_mapped_on_parse():
102103

103104
# bonus: defaults after empty init are also mapped
104105
assert Test().choice.name == Choice.ZERO.name
106+
107+
108+
def test_renamed_enum_members():
109+
assert set(ArithmeticOperator.__members__) == {
110+
"NONE",
111+
"PLUS",
112+
"MINUS",
113+
"_0_PREFIXED",
114+
}

0 commit comments

Comments
 (0)