Skip to content

Commit

Permalink
Fix import when method name is reserved word (#2690)
Browse files Browse the repository at this point in the history
* optimize

* fix

* changelog
  • Loading branch information
msyyc authored Jul 12, 2024
1 parent 53583e0 commit 23f3aa1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/sdk-regen-optimization-2024-6-11-14-58-58.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@azure-tools/typespec-python"
---

Fix import error when method name is reserved word in Pyhton
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .parameter import Parameter, ParameterMethodLocation
from .lro_operation import LROOperation
from .lro_paging_operation import LROPagingOperation
from ...utils import extract_original_name

ParameterListType = TypeVar(
"ParameterListType",
Expand Down Expand Up @@ -92,7 +93,7 @@ def add_og_request_builder(og: Dict[str, Any]):
if operation_yaml.get("isLroInitialOperation"):
# we want to change the name
request_builder.name = request_builder.get_name(
request_builder.yaml_data["name"][1 : -len("_initial")],
extract_original_name(request_builder.yaml_data["name"]),
request_builder.yaml_data,
request_builder.code_model,
request_builder.client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import copy
from typing import Callable, Dict, Any, List, Optional

from ..utils import to_snake_case
from ..utils import to_snake_case, extract_original_name
from .helpers import (
add_redefined_builtin_info,
pad_builtin_namespaces,
Expand Down Expand Up @@ -362,7 +362,12 @@ def update_operation(
yaml_data["groupName"] = self.pad_reserved_words(yaml_data["groupName"], PadType.OPERATION_GROUP)
yaml_data["groupName"] = to_snake_case(yaml_data["groupName"])
yaml_data["name"] = yaml_data["name"].lower()
yaml_data["name"] = self.pad_reserved_words(yaml_data["name"], PadType.METHOD)
if yaml_data.get("isLroInitialOperation") is True:
yaml_data["name"] = (
"_" + self.pad_reserved_words(extract_original_name(yaml_data["name"]), PadType.METHOD) + "_initial"
)
else:
yaml_data["name"] = self.pad_reserved_words(yaml_data["name"], PadType.METHOD)
yaml_data["description"] = update_description(yaml_data["description"], yaml_data["name"])
yaml_data["summary"] = update_description(yaml_data.get("summary", ""))
body_parameter = yaml_data.get("bodyParameter")
Expand Down
4 changes: 4 additions & 0 deletions packages/typespec-python/generator/pygen/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,7 @@ def build_policies(
"self._config.logging_policy",
]
return [p for p in policies if p]


def extract_original_name(name: str) -> str:
return name[1 : -len("_initial")]

0 comments on commit 23f3aa1

Please sign in to comment.