Improve logic to avoid keyword collisions in generated code
Use the standard library keyword module instead of a hard coded list and applying it to enum keys as well.
This commit is contained in:
parent
804805f0f5
commit
80bef7c94f
@ -1,3 +1,4 @@
|
|||||||
|
import keyword
|
||||||
import re
|
import re
|
||||||
|
|
||||||
# Word delimiters and symbols that will not be preserved when re-casing.
|
# Word delimiters and symbols that will not be preserved when re-casing.
|
||||||
@ -16,42 +17,7 @@ WORD_UPPER = "[A-Z]+(?![a-z])[0-9]*"
|
|||||||
def safe_snake_case(value: str) -> str:
|
def safe_snake_case(value: str) -> str:
|
||||||
"""Snake case a value taking into account Python keywords."""
|
"""Snake case a value taking into account Python keywords."""
|
||||||
value = snake_case(value)
|
value = snake_case(value)
|
||||||
if value in [
|
value = sanitize_name(value)
|
||||||
"and",
|
|
||||||
"as",
|
|
||||||
"assert",
|
|
||||||
"async",
|
|
||||||
"await",
|
|
||||||
"break",
|
|
||||||
"class",
|
|
||||||
"continue",
|
|
||||||
"def",
|
|
||||||
"del",
|
|
||||||
"elif",
|
|
||||||
"else",
|
|
||||||
"except",
|
|
||||||
"finally",
|
|
||||||
"for",
|
|
||||||
"from",
|
|
||||||
"global",
|
|
||||||
"if",
|
|
||||||
"import",
|
|
||||||
"in",
|
|
||||||
"is",
|
|
||||||
"lambda",
|
|
||||||
"nonlocal",
|
|
||||||
"not",
|
|
||||||
"or",
|
|
||||||
"pass",
|
|
||||||
"raise",
|
|
||||||
"return",
|
|
||||||
"try",
|
|
||||||
"while",
|
|
||||||
"with",
|
|
||||||
"yield",
|
|
||||||
]:
|
|
||||||
# https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles
|
|
||||||
value += "_"
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
@ -120,3 +86,8 @@ def camel_case(value: str, strict: bool = True):
|
|||||||
|
|
||||||
def lowercase_first(value: str):
|
def lowercase_first(value: str):
|
||||||
return value[0:1].lower() + value[1:]
|
return value[0:1].lower() + value[1:]
|
||||||
|
|
||||||
|
|
||||||
|
def sanitize_name(value: str) -> str:
|
||||||
|
# https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles
|
||||||
|
return f"{value}_" if keyword.iskeyword(value) else value
|
||||||
|
@ -54,6 +54,8 @@ from betterproto.compile.naming import (
|
|||||||
pythonize_method_name,
|
pythonize_method_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from ..casing import sanitize_name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# betterproto[compiler] specific dependencies
|
# betterproto[compiler] specific dependencies
|
||||||
from google.protobuf.compiler import plugin_pb2 as plugin
|
from google.protobuf.compiler import plugin_pb2 as plugin
|
||||||
@ -542,7 +544,7 @@ class EnumDefinitionCompiler(MessageCompiler):
|
|||||||
# Get entries/allowed values for this Enum
|
# Get entries/allowed values for this Enum
|
||||||
self.entries = [
|
self.entries = [
|
||||||
self.EnumEntry(
|
self.EnumEntry(
|
||||||
name=entry_proto_value.name,
|
name=sanitize_name(entry_proto_value.name),
|
||||||
value=entry_proto_value.number,
|
value=entry_proto_value.number,
|
||||||
comment=get_comment(
|
comment=get_comment(
|
||||||
proto_file=self.proto_file, path=self.path + [2, entry_number]
|
proto_file=self.proto_file, path=self.path + [2, entry_number]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user