From 9dff5792017a79f7c9cdf3e9a5a9945c7df41f2d Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Thu, 9 Apr 2026 16:06:11 -0400 Subject: [PATCH 1/4] play around with new casing --- packages/http-client-python/emitter/src/utils.ts | 6 ++---- packages/http-client-python/emitter/test/utils.test.ts | 9 +++++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/http-client-python/emitter/src/utils.ts b/packages/http-client-python/emitter/src/utils.ts index 9b30231a71d..e065b6bcd70 100644 --- a/packages/http-client-python/emitter/src/utils.ts +++ b/packages/http-client-python/emitter/src/utils.ts @@ -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") @@ -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( diff --git a/packages/http-client-python/emitter/test/utils.test.ts b/packages/http-client-python/emitter/test/utils.test.ts index 116b88e985e..1d3593fec33 100644 --- a/packages/http-client-python/emitter/test/utils.test.ts +++ b/packages/http-client-python/emitter/test/utils.test.ts @@ -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); From 02bab07c673b561113a74e4fcd978406860d2c82 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Thu, 9 Apr 2026 16:06:47 -0400 Subject: [PATCH 2/4] add changeset --- .chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md diff --git a/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md b/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md new file mode 100644 index 00000000000..9d028fb4db5 --- /dev/null +++ b/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Fix casing for enums with mix of numbers and letters \ No newline at end of file From ed37a4ea6c049b2324d6639dba2ee574f5314590 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 27 Oct 2026 13:25:39 -0400 Subject: [PATCH 3/4] fix(python): preserve numeric identifier casing Keep digits attached to preceding words and restore word boundaries after digits while preserving date separators and short alphanumeric acronyms. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: a56d520e-33f1-474a-aa6c-7f8d999656d8 --- .../python-fixCasingEnum-2026-3-9-16-6-39.md | 2 +- .../http-client-python/emitter/src/utils.ts | 24 +++++---- .../emitter/test/emitter.test.ts | 49 +++++++++++++++++++ .../emitter/test/utils.test.ts | 33 +++++++++++++ 4 files changed, 98 insertions(+), 10 deletions(-) diff --git a/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md b/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md index 1e5f19f2f1d..c0979d3298d 100644 --- a/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md +++ b/.chronus/changes/python-fixCasingEnum-2026-3-9-16-6-39.md @@ -4,4 +4,4 @@ packages: - "@typespec/http-client-python" --- -Fix casing for enums with mix of numbers and letters +Preserve underscores before numeric enum segments and acronyms such as `A2A` without changing numeric identifiers such as `int32`. diff --git a/packages/http-client-python/emitter/src/utils.ts b/packages/http-client-python/emitter/src/utils.ts index 5c7ba0d49fe..86ea629c1e3 100644 --- a/packages/http-client-python/emitter/src/utils.ts +++ b/packages/http-client-python/emitter/src/utils.ts @@ -44,15 +44,21 @@ function deconstruct( return [...identifier.flatMap((each) => deconstruct(each, maxUppercasePreserve))]; } - return `${identifier}` - .replace(/([a-z]+)([A-Z])/g, "$1 $2") // Add a space in between camelCase words(e.g. fooBar => foo 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") - .trim() - .split(/[\W|_]+/) - .map((each) => (IsFullyUpperCase(each, maxUppercasePreserve) ? each : each.toLowerCase())); + return ( + `${identifier}` + // Keep digits attached to the preceding word and preserve short acronyms like A2A. + .replace( + /(? (digits ? `${digits} ` : match), + ) + .replace(/([a-z]+)([A-Z])/g, "$1 $2") // Add a space in between camelCase words(e.g. fooBar => foo Bar) + .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") + .trim() + .split(/[\W|_]+/) + .map((each) => (IsFullyUpperCase(each, maxUppercasePreserve) ? each : each.toLowerCase())) + ); } function isEqual(s1: string, s2: string): boolean { diff --git a/packages/http-client-python/emitter/test/emitter.test.ts b/packages/http-client-python/emitter/test/emitter.test.ts index 857dc01bc02..772ed45f2a7 100644 --- a/packages/http-client-python/emitter/test/emitter.test.ts +++ b/packages/http-client-python/emitter/test/emitter.test.ts @@ -2,6 +2,55 @@ import { expectDiagnostics, t } from "@typespec/compiler/testing"; import { expect, it } from "vitest"; import { EmitterTester, emitCodeModel } from "./test-host.js"; +it("preserves numeric property names while fixing numeric enum members", async () => { + const { codeModel, diagnostics } = await emitCodeModel(` + import "@typespec/http"; + using TypeSpec.Http; + + @service namespace Test; + enum NumericNames { + WEB_SEARCH_PREVIEW_2025_03_11: "web", + DEFAULT_2024_11_15: "default", + A2A: "a2a", + int32: "int32", + enumMemberV2Preview: "preview", + } + model NumericProperties { + int32Property: int32; + float32Property: float32; + subscriptionResource1Name: string; + names: NumericNames; + } + @get op read(): NumericProperties; + `); + + expectDiagnostics(diagnostics, []); + expect(codeModel.types).toContainEqual( + expect.objectContaining({ + type: "enum", + name: "NumericNames", + values: expect.arrayContaining([ + expect.objectContaining({ name: "WEB_SEARCH_PREVIEW_2025_03_11" }), + expect.objectContaining({ name: "DEFAULT_2024_11_15" }), + expect.objectContaining({ name: "A2A" }), + expect.objectContaining({ name: "INT32" }), + expect.objectContaining({ name: "ENUM_MEMBER_V2_PREVIEW" }), + ]), + }), + ); + expect(codeModel.types).toContainEqual( + expect.objectContaining({ + type: "model", + name: "NumericProperties", + properties: expect.arrayContaining([ + expect.objectContaining({ clientName: "int32_property" }), + expect.objectContaining({ clientName: "float32_property" }), + expect.objectContaining({ clientName: "subscription_resource1_name" }), + ]), + }), + ); +}); + it("preserves an XML response body with the legacy bytes encoding", async () => { const { codeModel, diagnostics } = await emitCodeModel(` import "@typespec/http"; diff --git a/packages/http-client-python/emitter/test/utils.test.ts b/packages/http-client-python/emitter/test/utils.test.ts index ff4aac41221..e3eb3f3e1c8 100644 --- a/packages/http-client-python/emitter/test/utils.test.ts +++ b/packages/http-client-python/emitter/test/utils.test.ts @@ -28,6 +28,39 @@ describe("typespec-python: utils", () => { } }); + it.each([ + ["int32", "int32"], + ["uint8", "uint8"], + ["uint32", "uint32"], + ["float32", "float32"], + ["float64", "float64"], + ["int32Property", "int32_property"], + ["float32Property", "float32_property"], + ["int32MillisecondsArray", "int32_milliseconds_array"], + ["uint32AsStringOptional", "uint32_as_string_optional"], + ["base64", "base64"], + ["base64url", "base64_url"], + ["base64urlArray", "base64_url_array"], + ["iso8601", "iso8601"], + ["rfc3339", "rfc3339"], + ["rfc7231", "rfc7231"], + ["two6k", "two6_k"], + ["subscriptionResource1Name", "subscription_resource1_name"], + ["subscriptionResource2Name", "subscription_resource2_name"], + ["InterfaceV2", "interface_v2"], + ["v2InInterface", "v2_in_interface"], + ["enumMemberV2Preview", "enum_member_v2_preview"], + ["errorResponseStatusCode404", "error_response_status_code404"], + ["A2A", "a2a"], + ["B2B", "b2b"], + ["Protocol_A2A", "protocol_a2a"], + ["A2A_Protocol", "a2a_protocol"], + ["WEB_SEARCH_PREVIEW_2025_03_11", "web_search_preview_2025_03_11"], + ["DEFAULT_2024_11_15", "default_2024_11_15"], + ])("preserves numeric identifier boundaries in %s", (input, expected) => { + strictEqual(camelToSnakeCase(input), expected); + }); + it("md2rst", async () => { const des = "Format: .."; strictEqual(md2Rst(des), "Format: .."); From 444e93de648003781867cd71ee33ad8b3c0d6838 Mon Sep 17 00:00:00 2001 From: iscai-msft <43154838+iscai-msft@users.noreply.github.com> Date: Wed, 28 Oct 2026 14:08:09 -0400 Subject: [PATCH 4/4] Modify regex to handle plural upper case words with digits Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- packages/http-client-python/emitter/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/http-client-python/emitter/src/utils.ts b/packages/http-client-python/emitter/src/utils.ts index 86ea629c1e3..526f8eafb84 100644 --- a/packages/http-client-python/emitter/src/utils.ts +++ b/packages/http-client-python/emitter/src/utils.ts @@ -52,7 +52,7 @@ function deconstruct( (match: string, digits: string | undefined) => (digits ? `${digits} ` : match), ) .replace(/([a-z]+)([A-Z])/g, "$1 $2") // Add a space in between camelCase words(e.g. fooBar => foo Bar) - .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])s(\d*)([^a-z])(.*)/g, "$1$2«$3 $4$5") // 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") .trim()