fix: quote recursive union member annotations#709
Conversation
There was a problem hiding this comment.
Thanks @Alan4506! Nice fix.
One thought before merge: could we add a small recursive union to the integ test model in codegen/core/src/it/resources/META-INF/smithy/main.smithy? I tried it locally and the :core:integ test fails with the same F821 error. On this branch it passes. That way we'd catch regressions.
Something like:
// In operation ListCities output, after `mutual: MutuallyRecursiveA`:
recursiveUnion: RecursiveUnion
// After structure MutuallyRecursiveB:
union RecursiveUnion {
nested: RecursiveUnionMap
items: RecursiveUnionList
leaf: String
}
map RecursiveUnionMap {
key: String
value: RecursiveUnion
}
list RecursiveUnionList {
member: RecursiveUnion
}
Thanks @alexgromero ! Addressed in 8f62eeb and verified |
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.