From deb623ed14cea65f0a0d17e9c770426d71198ae0 Mon Sep 17 00:00:00 2001 From: nat Date: Tue, 6 Apr 2021 02:45:57 +0200 Subject: [PATCH] Fix compilation of fields named 'bytes' or 'str' (#226) * 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 --- src/betterproto/casing.py | 12 +++++++++++- tests/inputs/config.py | 1 - 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/betterproto/casing.py b/src/betterproto/casing.py index cd3c34472..ed8299135 100644 --- a/src/betterproto/casing.py +++ b/src/betterproto/casing.py @@ -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 diff --git a/tests/inputs/config.py b/tests/inputs/config.py index 22dcf2ace..6fd27a39e 100644 --- a/tests/inputs/config.py +++ b/tests/inputs/config.py @@ -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",