Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 3, 2025

Implements alpha TypedDict support for issue #8800. Enables generating Python TypedDict classes instead of runtime model classes for input-only scenarios.

Changes

TypeScript Emitter

  • Added "typeddict" to models-mode option enum in PythonEmitterOptions
  • Updated emitModel() to dynamically set model base field from options

Python Generator

  • Created TypedDictModelType class - serializes as plain dict, uses isinstance(x, dict) checks
  • Created TypedDictModelSerializer - generates TypedDict classes with total=False for optional properties
  • Added model_typeddict.py.jinja2 template
  • Updated serializer dispatch in __init__.py and builder_serializer.py to handle typeddict mode
  • Unified dpg/typeddict body parameter serialization logic (both use JSON serialization without runtime model base)

Usage

# tspconfig.yaml
options:
  "@typespec/http-client-python":
    models-mode: "typeddict"

Generates:

from typing import TypedDict

class User(TypedDict, total=False):
    name: str
    age: int
    email: str

# Used as input type hints, serialized as plain dict
def create_user(user: User) -> dict: ...

Limitations (Alpha)

  • Input-only (outputs return raw JSON/dict)
  • Supports: boolean, int, string, enum, list, dict
  • No support for: discriminators, readonly, datetime, bytes, record extension

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • telemetry.astro.build

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[python] Add new models-mode "typeddict"</issue_title>
<issue_description>Goal:

Add new models-mode "typeddict" for https://github.com/microsoft/typespec/tree/main/packages/http-client-python

Details:

declared in #8800 (comment)
</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 5 commits November 3, 2025 05:42
- Added "models-mode" option to PythonEmitterOptions with "typeddict" as a valid value
- Created TypedDictModelSerializer class for generating TypedDict-based models
- Added model_typeddict.py.jinja2 template for TypedDict model generation
- Updated model serializer dispatch logic to support "typeddict" mode
- Created TypedDictModelType class for TypedDict model types
- Updated emitter to set model base field based on models-mode option

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
- Updated example_template to skip examples for typeddict mode
- Added typeddict body parameter serialization handling
- Updated need_utils_folder to include typeddict mode

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
- Added TypedDictModelType to imports in builder_serializer.py

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
- Updated declare_model to use 'total=False' to match template
- Changed template to use 'is not None' instead of '!= None'

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
- Updated template to use serializer.declare_model() method
- Changed property check to use {% if model.properties %} for consistency
- Refactored builder_serializer to combine dpg and typeddict logic, reducing duplication

Co-authored-by: msyyc <70930885+msyyc@users.noreply.github.com>
Copilot AI changed the title [WIP] [python] Add new models-mode "typeddict" [python] Add models-mode "typeddict" for input-only TypedDict model generation Nov 3, 2025
Copilot AI requested a review from msyyc November 3, 2025 06:02
msyyc added 3 commits November 5, 2025 11:17
…-41221d5df5b0' and 'main' of https://github.com/microsoft/typespec into copilot/fix-6154722-381857226-169e8ee3-b9f9-4c02-bf29-41221d5df5b0
@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 Nov 5, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2025

❌ There is undocummented changes. Run chronus add to add a changeset or click here.

The following packages have changes but are not documented.

  • @typespec/http-client-python
Show changes

@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 Dec 5, 2025
@microsoft-github-policy-service
Copy link
Contributor

Hi @@copilot. Your PR has had no update for 30 days and it is marked as a stale PR. If it is not updated within 30 days, the PR will automatically be closed. If you want to refresh the PR, please remove the stale label.

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.

[python] Add new models-mode "typeddict"

2 participants