File tree Expand file tree Collapse file tree 2 files changed +10
-37
lines changed Expand file tree Collapse file tree 2 files changed +10
-37
lines changed Original file line number Diff line number Diff line change
1
+ import keyword
1
2
import re
2
3
3
4
# Word delimiters and symbols that will not be preserved when re-casing.
16
17
def safe_snake_case (value : str ) -> str :
17
18
"""Snake case a value taking into account Python keywords."""
18
19
value = snake_case (value )
19
- if value in [
20
- "and" ,
21
- "as" ,
22
- "assert" ,
23
- "async" ,
24
- "await" ,
25
- "break" ,
26
- "class" ,
27
- "continue" ,
28
- "def" ,
29
- "del" ,
30
- "elif" ,
31
- "else" ,
32
- "except" ,
33
- "finally" ,
34
- "for" ,
35
- "from" ,
36
- "global" ,
37
- "if" ,
38
- "import" ,
39
- "in" ,
40
- "is" ,
41
- "lambda" ,
42
- "nonlocal" ,
43
- "not" ,
44
- "or" ,
45
- "pass" ,
46
- "raise" ,
47
- "return" ,
48
- "try" ,
49
- "while" ,
50
- "with" ,
51
- "yield" ,
52
- ]:
53
- # https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles
54
- value += "_"
20
+ value = sanitize_name (value )
55
21
return value
56
22
57
23
@@ -120,3 +86,8 @@ def camel_case(value: str, strict: bool = True):
120
86
121
87
def lowercase_first (value : str ):
122
88
return value [0 :1 ].lower () + value [1 :]
89
+
90
+
91
+ def sanitize_name (value : str ) -> str :
92
+ # https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles
93
+ return f"{ value } _" if keyword .iskeyword (value ) else value
Original file line number Diff line number Diff line change 54
54
pythonize_method_name ,
55
55
)
56
56
57
+ from ..casing import sanitize_name
58
+
57
59
try :
58
60
# betterproto[compiler] specific dependencies
59
61
from google .protobuf .compiler import plugin_pb2 as plugin
@@ -542,7 +544,7 @@ def __post_init__(self):
542
544
# Get entries/allowed values for this Enum
543
545
self .entries = [
544
546
self .EnumEntry (
545
- name = entry_proto_value .name ,
547
+ name = sanitize_name ( entry_proto_value .name ) ,
546
548
value = entry_proto_value .number ,
547
549
comment = get_comment (
548
550
proto_file = self .proto_file , path = self .path + [2 , entry_number ]
You can’t perform that action at this time.
0 commit comments