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 <spencer@sf-n.com>
This commit is contained in:
nat
2021-04-06 10:45:57 +10:00
committed by GitHub
co-authored by Spencer
parent 95339bf74d
commit deb623ed14
2 changed files with 11 additions and 2 deletions
+11 -1
View File
@@ -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