Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global keyword reassignment #265

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions generate/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,7 @@ def generateUnionType(
else:
template_info["types"].append(
{
"name": type,
"name": rename_if_keyword(type),
"var0": randletter(),
"var1": randletter(),
"check": "type",
Expand Down Expand Up @@ -1688,7 +1688,7 @@ def generateObjectTypeCode(
template_info: TemplateType = {
"fields": fields,
"description": description,
"name": name,
"name": rename_if_keyword(name),
"imports": imports,
}

Expand Down Expand Up @@ -1943,6 +1943,14 @@ def clean_parameter_name(name: str):
return camel_to_snake(name).replace("from", "from_")


def rename_if_keyword(name: str):
"""Rename a name if it is also a Python keyword."""
KEYWORDS = ["global"] # there are more, but this is the only one we overlap now
if name in KEYWORDS:
return name + "_"
return name


def camel_to_snake(name: str):
name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", name).lower().replace("-", "_")
Expand Down
Loading