Skip to content

Commit

Permalink
Fix compilation of fields named 'bytes' or 'str' (#226)
Browse files Browse the repository at this point in the history
* if you have a field named "bytes" using the bytes type, it doesn't work.
* Enable existing use-case & generalize solution to cover it

Co-authored-by: Spencer <[email protected]>
  • Loading branch information
nat-n and SpencerSharkey authored Apr 6, 2021
1 parent 95339bf commit deb623e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/betterproto/casing.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ def lowercase_first(value: str) -> str:
return value[0:1].lower() + value[1:]


def is_reserved_name(value: str) -> bool:
if keyword.iskeyword(value):
return True

if value in ("bytes", "str"):
return True

return False


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
return f"{value}_" if is_reserved_name(value) else value
1 change: 0 additions & 1 deletion tests/inputs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Remove from list when fixed.
xfail = {
"namespace_keywords", # 70
"namespace_builtin_types", # 53
"googletypes_struct", # 9
"googletypes_value", # 9
"import_capitalized_package",
Expand Down

0 comments on commit deb623e

Please sign in to comment.