Skip to content

Render Python enums from TCGC via TypeScript/Handlebars - #10899

Closed
Libba Lawrence (l0lawrence) wants to merge 5 commits into
mainfrom
python-ts-enum-codegen
Closed

Libba Lawrence (l0lawrence) wants to merge 5 commits into
mainfrom
python-ts-enum-codegen

Conversation

@l0lawrence

Copy link
Copy Markdown
Member

Summary

First milestone of rewriting the @typespec/http-client-python emitter in TypeScript. Today the emitter goes TCGC → TS code-model → YAML → pygen (Jinja + black). This PR begins replacing that pipeline by rendering enums to Python directly from TCGC in TypeScript, using Handlebars templates instead of pygen's Jinja templates.

Scope is intentionally limited to _enums.py files. pygen still owns everything else — the TS-rendered enum files overwrite pygen's output before black runs, so black normalizes them and the rest of generation is unchanged.

How it works

  • codegen/enums/render.ts — collects enums directly from sdkContext.sdkPackage.enums (SdkEnumType), applies the same filtering/grouping/naming/description transforms as the existing code-model + pygen preprocess steps, and builds view models. No YAML/code-model intermediate is involved for enums.
  • codegen/enums/templates/enum.py.hbs + handlebars.ts — render the _enums.py source (validated byte-identical to pygen+black ground truth for azure and unbranded flavors).
  • codegen/enums/replace.ts — overwrites pygen's _enums.py with the TS-rendered version, guarded by file-existence + class-name-set checks.
  • Wired into all three generation paths in emitter.ts:
    • native (after run_tsp.py, before black),
    • pyodide (after preprocess+codegen, before black),
    • batch (emit-yaml-only embeds rendered files in the config JSON; run_batch.py applies them after codegen, before black).

Validation

  • 11 emitter unit tests pass (naming, wrapping, byte-identical render).
  • npm run build and npm run lint pass.
  • Native + batch paths confirmed applying the TS render end-to-end (marker-verified) and producing output byte-identical to the pygen+black ground truth.

Notes

  • The browser/playground pyodide path intentionally still uses full pygen for this milestone.
  • Adds a handlebars dependency to @typespec/http-client-python.

Draft — opening for early review of the approach before expanding beyond enums.

Begin rewriting the http-client-python emitter in TypeScript: render
`_enums.py` files directly from TCGC (SdkEnumType) using Handlebars
templates, overwriting pygen's output before black runs in the native,
pyodide, and batch generation paths. Replaces the Jinja/codemodel path
for enums only; pygen still owns everything else.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Jun 5, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@10899

commit: 02bef05

@github-actions

github-actions Bot commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • ✅ @typespec/http-client-python
Show changes

@typespec/http-client-python - internal ✏️

Begin rewriting the Python emitter in TypeScript: render _enums.py files directly from TCGC using Handlebars templates, replacing the Python/Jinja path for enums.

@azure-sdk-automation

azure-sdk-automation Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

CodeQL flagged the A-z range in padSpecialChars as an overly permissive
regular expression range. Replace it with the explicit equivalent
character set so the matched characters stay byte-identical to pygen's
re.sub(r"[^A-z0-9_]", "_", name) while removing the suspicious cross-case
range that spans the punctuation between Z and a.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
TypeSpec identifiers only contain ASCII letters, digits, and underscore
(e.g. myEnum), so padSpecialChars only needs to keep [A-Za-z0-9_] and
replace everything else with _. This drops the unnecessary punctuation
that pygen's looser [^A-z0-9_] range happened to preserve; output is
unchanged for every real TypeSpec name and the CodeQL range warning is
resolved.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The root prettier check flagged three files. Add the Handlebars enum
template to .prettierignore (prettier reformats its Python content and
corrupts it), and apply prettier's import/export ordering to the enums
index barrel and emitter.ts.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@l0lawrence

Copy link
Copy Markdown
Member Author

running on network spec generation:
image

@microsoft-github-policy-service microsoft-github-policy-service Bot added the stale Mark a PR that hasn't been recently updated and will be closed. label Jun 20, 2026
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

Hi @Libba Lawrence (@l0lawrence). Your PR has had no update for 14 days and it is marked as a stale PR. If it is not updated within 14 additional days, the PR will automatically be closed. If you want to refresh the PR, please remove the stale label.

@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

Hi @Libba Lawrence (@l0lawrence). The PR will be closed since the PR has no update for 28 days. If this is still relevant please reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:python Issue for the Python client emitter: @typespec/http-client-python stale Mark a PR that hasn't been recently updated and will be closed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants