Skip to content

feat(typescript) [DRAFT] Add support for enums in Python v2 generator #7554

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 4 commits into from

Conversation

GrantRheingold
Copy link

@GrantRheingold GrantRheingold commented Jun 18, 2025

Description

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

Changes Made

  • Creates Enum and EnumValue AST nodes for structured enum generation
  • Implements proper Python enum class structure with inheritance from enum.Enum
  • Adds visit method generation with type-safe callable parameters for pattern matching
  • Creates a new EnumGenerator with enum-specific generator logic

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[...]
  • Additionally, Enums are probably more naturally written as extensions of a Class, so this implementation is probably best left to extending that

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 TypeVar
from enum import Enum

class WeatherReport(Enum):
    Sunny = "SUNNY"
    Cloudy = "CLOUDY"
    Raining = "RAINING"
    Snowing = "SNOWING"

Comment on lines +8 to +19
def visit(
self,
ApiError = "API_ERROR": typing.Callable[[], T_Result],
AuthenticationError = "AUTHENTICATION_ERROR": typing.Callable[[], T_Result],
InvalidRequestError = "INVALID_REQUEST_ERROR": typing.Callable[[], T_Result]
) -> T_Result:
if self is ErrorCategory.ApiError = "API_ERROR":
return ApiError = "API_ERROR"()
if self is ErrorCategory.AuthenticationError = "AUTHENTICATION_ERROR":
return AuthenticationError = "AUTHENTICATION_ERROR"()
if self is ErrorCategory.InvalidRequestError = "INVALID_REQUEST_ERROR":
return InvalidRequestError = "INVALID_REQUEST_ERROR"()
Copy link
Contributor

Choose a reason for hiding this comment

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

There appears to be a syntax error in the generated visit method. The parameter definitions incorrectly include the enum values as part of the parameter names (e.g., ApiError = "API_ERROR" instead of just ApiError). Similarly, the if statements contain malformed conditions that append the enum values after the enum member references.

The correct Python syntax should look like:

def visit(
    self,
    ApiError: typing.Callable[[], T_Result],
    AuthenticationError: typing.Callable[[], T_Result],
    InvalidRequestError: typing.Callable[[], T_Result]
) -> T_Result:
    if self is ErrorCategory.ApiError:
        return ApiError()
    if self is ErrorCategory.AuthenticationError:
        return AuthenticationError()
    if self is ErrorCategory.InvalidRequestError:
        return InvalidRequestError()

This issue appears to originate in the Enum.ts write method implementation that's generating the Python code.

Suggested change
def visit(
self,
ApiError = "API_ERROR": typing.Callable[[], T_Result],
AuthenticationError = "AUTHENTICATION_ERROR": typing.Callable[[], T_Result],
InvalidRequestError = "INVALID_REQUEST_ERROR": typing.Callable[[], T_Result]
) -> T_Result:
if self is ErrorCategory.ApiError = "API_ERROR":
return ApiError = "API_ERROR"()
if self is ErrorCategory.AuthenticationError = "AUTHENTICATION_ERROR":
return AuthenticationError = "AUTHENTICATION_ERROR"()
if self is ErrorCategory.InvalidRequestError = "INVALID_REQUEST_ERROR":
return InvalidRequestError = "INVALID_REQUEST_ERROR"()
def visit(
self,
ApiError: typing.Callable[[], T_Result],
AuthenticationError: typing.Callable[[], T_Result],
InvalidRequestError: typing.Callable[[], T_Result]
) -> T_Result:
if self is ErrorCategory.ApiError:
return ApiError()
if self is ErrorCategory.AuthenticationError:
return AuthenticationError()
if self is ErrorCategory.InvalidRequestError:
return InvalidRequestError()

Spotted by Diamond

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

@GrantRheingold GrantRheingold changed the title [WIP] Add support for enums in Python v2 generator feat(typescript) [DRAFT] Add support for enums 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