-
Notifications
You must be signed in to change notification settings - Fork 374
[python] Fix enum member names corrupted by YAML 1.1/1.2 boundary #11162
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
Open
msyyc
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
msyyc:fix-enum-yaml-scalar-roundtrip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+55
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
46c9f0f
[python] Fix enum member names corrupted by YAML 1.1/1.2 boundary
msyyc b096260
Remove now-unnecessary update_enum_value_name pygen workaround
msyyc 9c8ebe0
Revert pygen defensive workarounds; keep emitter root-cause fix only
msyyc 517b59d
Fix prettier formatting
msyyc 6665a32
Update http-client-python-non-string-description-2026-7-2.md
msyyc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.chronus/changes/http-client-python-non-string-description-2026-7-2.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| changeKind: fix | ||
| packages: | ||
| - "@typespec/http-client-python" | ||
| --- | ||
|
|
||
| Fix enum member names derived from date-like TypeSpec labels (e.g. `` `2020-01-01` ``) being corrupted by the js-yaml (YAML 1.2) to PyYAML (YAML 1.1) boundary. String scalars are now force-quoted when serializing the code model so names such as `2020_01_01` round-trip as strings instead of being read back as integers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/http-client-python/emitter/test/external-process.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { ok, strictEqual } from "assert"; | ||
| import { load } from "js-yaml"; | ||
| import { describe, it } from "vitest"; | ||
| import { dumpCodeModelToYaml } from "../src/external-process.js"; | ||
|
|
||
| describe("typespec-python: external-process", () => { | ||
| // The Python generator parses the emitted YAML with PyYAML (YAML 1.1), where a plain | ||
| // scalar such as `2020_01_01` is interpreted as the integer `20200101`. js-yaml dumps | ||
| // using YAML 1.2 rules and would otherwise leave such string scalars unquoted, so we | ||
| // must force-quote strings to keep enum member names (and other string values) intact. | ||
| it("force-quotes string scalars that YAML 1.1 would misinterpret", () => { | ||
| const yaml = dumpCodeModelToYaml({ name: "2020_01_01" }); | ||
| // The scalar must be quoted, otherwise PyYAML reads it back as the integer 20200101. | ||
| ok(yaml.includes('"2020_01_01"'), `expected the underscore scalar to be quoted, got: ${yaml}`); | ||
| ok( | ||
| !/name:\s*2020_01_01\s*$/m.test(yaml), | ||
| `expected no unquoted underscore scalar, got: ${yaml}`, | ||
| ); | ||
| }); | ||
|
|
||
| it("keeps string values as strings after a round-trip", () => { | ||
| const codeModel = { name: "2020_01_01", value: "2020-01-01", plain: "hello" }; | ||
| const roundTripped = load(dumpCodeModelToYaml(codeModel)) as Record<string, unknown>; | ||
| strictEqual(roundTripped.name, "2020_01_01"); | ||
| strictEqual(roundTripped.value, "2020-01-01"); | ||
| strictEqual(roundTripped.plain, "hello"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just wondering here do you actually even need yaml can't you serialize as json, would be faster and less deps
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i feel like yaml is not a great format for data serialization better suited for config