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

Remove enum prefixes #187

Merged
merged 47 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
0d6ced5
Implement Message.__bool__ for #130
Gobot1234 Aug 24, 2020
7746b91
Add a test for it
Gobot1234 Aug 24, 2020
53a7df0
Merge branch 'master' into master
Gobot1234 Sep 2, 2020
9da923d
Blacken
Gobot1234 Sep 2, 2020
17e31f4
Update tests
Gobot1234 Sep 19, 2020
a53d805
Fix bool
Gobot1234 Sep 19, 2020
b3b7c00
Fix failing tests
Gobot1234 Sep 19, 2020
4a4429d
Update docs
Gobot1234 Oct 19, 2020
48e80cf
Merge branch 'master' into master
Gobot1234 Oct 19, 2020
86d7c30
Add __bool__ to special members
Gobot1234 Oct 20, 2020
5c8e926
Update __init__.py
Gobot1234 Oct 20, 2020
f10bec4
Simplify bool
Gobot1234 Oct 27, 2020
e04fcb6
Tweak __bool__ docstring
nat-n Nov 24, 2020
5141d41
Fixes for Python 3.9 (#140)
Gobot1234 Nov 1, 2020
b9a50a7
replace now-disabled set-env command (#172)
w4rum Nov 21, 2020
0869e69
Update dependencies and add ci checks for python 3.9 (#173)
abn Nov 24, 2020
fe3addf
Release v2.0.0b2 (#175)
abn Nov 24, 2020
b82517d
Fix incorrect routes in generated client when service is not in a pac…
nat-n Nov 28, 2020
9fd8917
Remove Enum prefixes
Gobot1234 Dec 18, 2020
264c4b9
Merge remote-tracking branch 'upstream/master' into remove-enum-prefix
Gobot1234 Dec 18, 2020
d965741
Blacken
Gobot1234 Dec 18, 2020
9b9f737
Remove any repeated prefix from enum members
Gobot1234 Dec 30, 2020
60d1880
Remove unused imports
Gobot1234 Dec 30, 2020
ae80862
That was a pain
Gobot1234 Dec 30, 2020
5adc93d
Revert stuff
Gobot1234 Jan 26, 2021
a7df010
Fix stuff
Gobot1234 Feb 1, 2021
d58ce63
Relock
Gobot1234 Apr 2, 2021
17ba889
Merge remote-tracking branch 'upstream/master' into remove-enum-prefix
Gobot1234 Apr 2, 2021
b1ba675
Relock?
Gobot1234 Apr 2, 2021
e2b850e
Update test_enum.py
Gobot1234 Apr 2, 2021
bbe7256
Update naming.py
Gobot1234 Apr 2, 2021
ecc1629
Relock
Gobot1234 Dec 24, 2021
7b6f03d
Merge branch 'remove-enum-prefix' of /~https://github.com/Gobot1234/pyt…
Gobot1234 Dec 24, 2021
6592d4d
Merge branch 'master' into remove-enum-prefix
Gobot1234 Dec 24, 2021
6fe9b5c
Lock again
Gobot1234 Dec 24, 2021
3afffca
Fix for names starting with ints
Gobot1234 Dec 24, 2021
52456c7
Update the lock file
Gobot1234 Apr 20, 2022
ce25f96
Merge branch 'master' into remove-enum-prefix
Gobot1234 Apr 20, 2022
992db51
Running tests is for nerds
Gobot1234 Apr 20, 2022
72a8b89
Add a test for the invalid identifier case
Gobot1234 Apr 21, 2022
a2ba23e
Update casing.py
Gobot1234 Apr 21, 2022
25ef492
Merge branch 'master' into remove-enum-prefix
Gobot1234 Oct 16, 2023
383575f
Check for dunder names
Gobot1234 Oct 16, 2023
ecae7f6
Rejig order
Gobot1234 Oct 16, 2023
0cc1790
Shim get source
Gobot1234 Oct 16, 2023
6392a9d
Remove redundant conditional
Gobot1234 Oct 16, 2023
a70a00d
Remove source shim
Gobot1234 Oct 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/betterproto/compile/naming.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Iterable, List

from betterproto import casing


Expand All @@ -11,3 +13,19 @@ def pythonize_field_name(name: str) -> str:

def pythonize_method_name(name: str) -> str:
return casing.safe_snake_case(name)


def pythonize_enum_member_names(names: Iterable[str]) -> List[str]:
"""Removes any of the same characters from an Enum member's names."""
Gobot1234 marked this conversation as resolved.
Show resolved Hide resolved

def find_max_prefix_len() -> int:
for max_prefix_len, chars in enumerate(tuple(zip(*map(tuple, names)))):
previous_char = chars[0][0]
for char in chars:
if previous_char != char:
return max_prefix_len
return 0

max_prefix_len = find_max_prefix_len()

return [casing.sanitize_name(name[max_prefix_len:]) for name in names]
11 changes: 8 additions & 3 deletions src/betterproto/plugin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@

import betterproto

from ..casing import sanitize_name
from ..compile.importing import get_type_reference, parse_source_type_name
from ..compile.naming import (
pythonize_class_name,
pythonize_enum_member_names,
pythonize_field_name,
pythonize_method_name,
)
Expand Down Expand Up @@ -522,15 +522,20 @@ class EnumEntry:

def __post_init__(self) -> None:
# Get entries/allowed values for this Enum
member_names = pythonize_enum_member_names(
[entry_proto_value.name for entry_proto_value in self.proto_obj.value]
)
self.entries = [
self.EnumEntry(
name=sanitize_name(entry_proto_value.name),
name=member_name,
value=entry_proto_value.number,
comment=get_comment(
proto_file=self.proto_file, path=self.path + [2, entry_number]
),
)
for entry_number, entry_proto_value in enumerate(self.proto_obj.value)
for member_name, (entry_number, entry_proto_value) in zip(
member_names, enumerate(self.proto_obj.value)
)
]
super().__post_init__() # call MessageCompiler __post_init__

Expand Down
7 changes: 7 additions & 0 deletions tests/inputs/enum/enum.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ enum Choice {
FOUR = 4;
THREE = 3;
}

// A "C" like enum with the enum name prefixed onto members, these should be stripped
enum ArithmeticOperator {
ARITHMETIC_OPERATOR_NONE = 0;
ARITHMETIC_OPERATOR_PLUS = 1;
ARITHMETIC_OPERATOR_MINUS = 2;
}
10 changes: 6 additions & 4 deletions tests/inputs/enum/test_enum.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from tests.output_betterproto.enum import (
Test,
Choice,
)
from tests.output_betterproto.enum import ArithmeticOperator, Test, Choice


def test_enum_set_and_get():
Expand Down Expand Up @@ -82,3 +79,8 @@ def enum_generator():
yield Choice.THREE

assert Test(choices=enum_generator()).to_dict()["choices"] == ["ONE", "THREE"]


def test_renamed_enum_members():
for member in ArithmeticOperator:
Gobot1234 marked this conversation as resolved.
Show resolved Hide resolved
assert member.name in ("NONE", "PLUS", "MINUS")