Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions .chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-client-python"
---

Fix casing for enums with mix of numbers and letters
6 changes: 2 additions & 4 deletions packages/http-client-python/emitter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function deconstruct(

return `${identifier}`
.replace(/([a-z]+)([A-Z])/g, "$1 $2") // Add a space in between camelCase words(e.g. fooBar => foo Bar)
.replace(/(\d+)/g, " $1 ") // Adds a space after numbers(e.g. foo123 => foo123 bar)
.replace(/(\d{2,})/g, " $1 ") // Adds a space around multi-digit numbers (e.g. foo123 => foo 123), but not single digits to preserve acronyms like A2A
.replace(/\b([A-Z]+)([A-Z])s([^a-z])(.*)/g, "$1$2« $3$4") // Add a space after a plural upper cased word(e.g. MBsFoo => MBs Foo)
.replace(/\b([A-Z]+)([A-Z])([a-z]+)/g, "$1 $2$3") // Add a space between an upper case word(2 char+) and the last capital case.(e.g. SQLConnection -> SQL Connection)
.replace(/«/g, "s")
Expand Down Expand Up @@ -102,9 +102,7 @@ function filterEmptyStrings(arr: string[]): string[] {
export function camelToSnakeCase(name: string): string {
if (!name) return name;
const words = filterEmptyStrings(normalize(name, false, 6));
const result = words.join("_").toLowerCase();
const result_final = result.replace(/([^\d])_(\d+)/g, "$1$2");
return result_final;
return words.join("_").toLowerCase();
}

export function getImplementation(
Expand Down
9 changes: 7 additions & 2 deletions packages/http-client-python/emitter/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ describe("typespec-python: utils", () => {
StandardSSDLRS: "standard_ssdlrs",
QRCode: "qr_code",
MicroQRCode: "micro_qr_code",
detection_01: "detection01",
"v1.1-preview.1": "v1_1_preview1",
detection_01: "detection_01",
"v1.1-preview.1": "v1_1_preview_1",
pointInTimeUTC: "point_in_time_utc",
diskSizeGB: "disk_size_gb",
lastModifiedTS: "last_modified_ts",
// Fix for GitHub issue #10312: preserve underscores before numbers (dates)
WebSearchPreview_2025_03_11: "web_search_preview_2025_03_11",
Default_2024_11_15: "default_2024_11_15",
// Fix for GitHub issue #10312: don't insert underscores around single digits in acronyms
A2a: "a2a",
};
for (const [input, expected] of Object.entries(cases)) {
strictEqual(camelToSnakeCase(input), expected);
Expand Down
Loading