fix(dto): codegen backend for a nested structure#4670
Open
abdulhaq-e wants to merge 1 commit intolitestar-org:mainfrom
Open
fix(dto): codegen backend for a nested structure#4670abdulhaq-e wants to merge 1 commit intolitestar-org:mainfrom
abdulhaq-e wants to merge 1 commit intolitestar-org:mainfrom
Conversation
Codegen was generating incorrect syntax for specific data container
nesting. The example shown in the test raises the error:
```
def _make_function(
self,
source_value_name: str,
return_value_name: str,
fn_name: str = "func",
) -> Callable[[Any], Any]:
"""Wrap the current body contents in a function definition and turn it into a callable object"""
source = f"def {fn_name}({source_value_name}):\n{self._body} return {return_value_name}"
ctx: dict[str, Any] = {**self._fn_locals}
# add the function to linecache, to get better stacktraces when an error occurs
# otherwise, the traceback within the generated code will just point
# to '<string>'
file_name = f"dto_transfer_function_{secrets.token_hex(6)}"
linecache.cache[file_name] = (
len(source),
None, # mtime: not applicable
[line + "\n" for line in source.splitlines()],
file_name,
)
> code = compile(source, file_name, "exec")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E File "dto_transfer_function_2714053d47d7", line 38
E source_instance_0['wrapper']_inner_0 = source_instance_0['wrapper'].inner
E ^^^^^^^^
E SyntaxError: invalid syntax
litestar/dto/_codegen_backend.py:233: SyntaxError
```
A similar production use case that raised this error:
```
class CursorPaginationMetadata(Struct):
limit: int = 50
next_cursor: str | None = None
previous_cursor: str | None = None
class PaginatedResponseMetadata(ResponseMetadata):
pagination: CursorPaginationMetadata
class UsersWithPagination(Struct):
data: list[User]
pagination: CursorPaginationMetadata
```
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4670 +/- ##
=======================================
Coverage 67.33% 67.34%
=======================================
Files 292 292
Lines 14941 14942 +1
Branches 1676 1676
=======================================
+ Hits 10061 10062 +1
Misses 4743 4743
Partials 137 137 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Codegen was generating incorrect syntax for specific data container nesting. The example shown in the test raises the error:
A similar production use case that raised this error:
The code and tests have been entirely generated by AI. I don't understand the codegen backend enough to evaluate this properly. There might be a simpler fix. I'm also not sure if DTO classes other than that for
msgspecare affected.