fix: quote recursive union member annotations#709
Open
Alan4506 wants to merge 2 commits into
Open
Conversation
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.
Description of changes:
Generating a client from a model that contains a recursive union currently fails the code generation process. For example, DynamoDB's
AttributeValueis a union where theMvariant targets a map ofAttributeValueand theLvariant targets a list ofAttributeValue. These generate Python members typeddict[str, AttributeValue]andlist[AttributeValue], i.e. they reference the union itself.smithy build --autfails duringruff check --fixwith:The reason is that the
AttributeValueunion alias is emitted after its member dataclasses, so a member annotation likevalue: dict[str, AttributeValue]is a forward reference to a name that isn't defined yet.StructureGeneratoralready handles this: when a member targets a recursive shape, it wraps the type annotation in quotes to make it a forward reference.UnionGeneratoris passed the same set of recursive shapes but never uses it, so union members miss this treatment. This change applies the same logic inUnionGenerator. Only members whose target is a recursive shape are affected; all other unions generate exactly as before.Testing:
Validated against the DynamoDB model which failed the code generation before. Locally, on top of the clean-json-rpc-v1 branch (which adds AWS JSON protocol support), I applied this change together with #707 (strip
\^escape in docstrings). With both changes in place, the DynamoDB client generates successfully:smithy build --autpasses ruff and pyright cleanly, and there are noF821errors.Inspecting the generated
AttributeValueunion members confirms the recursive annotations are now quoted:(The code emits single quotes, matching
StructureGenerator;ruff formatthen normalizes them to double quotes, which is what appears in the generated output.)By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.