Skip to content

feat(typescript) [DRAFT] Add support for discriminated unions in Python v2 generator #7553

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

Closed
wants to merge 6 commits into from

Conversation

GrantRheingold
Copy link

@GrantRheingold GrantRheingold commented Jun 18, 2025

Description

This PR adds support for generating discriminated unions in python using a typescript generator function.

Changes Made

  • Constructs appropriate class literal from arbitrary number of classes for creating a union
  • Ensures generated code follows Python naming conventions and type safety
  • Creates UndiscriminatedUnionGenerator class that generates simple typing.Union types

Future Work

  • As discussed, the imports should be prefixed by the package to avoid cross-module confusion between imported packages. So the final implementation should have typing.Union[...] instead of from typing import Union ... Union[...]

Testing

  • Manual testing completed

Tested by running

pnpm seed test --generator pydantic-v2 --log-level debug --fixture exhaustive --skip-scripts --exhaustive

and comparing the output to the generated output of the python-native generated file here.

from typing import Union, Literal
from typing_extensions import Literal as TypingExtensionsLiteral
from .dog import Dog
from .cat import Cat

class Animal_Dog(Dog):
    animal: Literal["dog"]
    class Config:
        allow_population_by_field_name = True


class Animal_Cat(Cat):
    animal: Literal["cat"]
    class Config:
        allow_population_by_field_name = True


Animal = Union[Animal_Dog, Animal_Cat]

const file = python.file({ path });

// Add imports
file.addReference(python.reference({ name: "Union", modulePath: ["typing"] }));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code adds a field with a Literal type but is missing the required import. Please add the following import after the Union import:

file.addReference(python.reference({ name: "Literal", modulePath: ["typing"] }));

This will ensure the generated Python code has all necessary imports for the discriminated union implementation.

Suggested change
file.addReference(python.reference({ name: "Union", modulePath: ["typing"] }));
file.addReference(python.reference({ name: "Union", modulePath: ["typing"] }));
file.addReference(python.reference({ name: "Literal", modulePath: ["typing"] }));

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

@GrantRheingold GrantRheingold changed the title [WIP] Add support for discriminated unions in Python v2 generator feat(typescript) [DRAFT] Add support for discriminated unions in Python v2 generator Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants