-
Notifications
You must be signed in to change notification settings - Fork 228
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
Conversation
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"() |
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.
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.
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.
Description
This PR adds support for generating enums in python using a typescript generator function.
Changes Made
Future Work
typing.Union[...]
instead offrom typing import Union ... Union[...]
Enum
s are probably more naturally written as extensions of aClass
, so this implementation is probably best left to extending thatTesting
Tested by running
and comparing the output to the generated output of the python-native generated file here.