Skip to content

fix: annotate @default:null members as nullable#713

Open
Alan4506 wants to merge 1 commit into
smithy-lang:developfrom
Alan4506:fix/default-null-annotation
Open

fix: annotate @default:null members as nullable#713
Alan4506 wants to merge 1 commit into
smithy-lang:developfrom
Alan4506:fix/default-null-annotation

Conversation

@Alan4506
Copy link
Copy Markdown
Contributor

@Alan4506 Alan4506 commented Jun 5, 2026

Problem

The Secrets Manager and Cognito Identity models have structure members with smithy.api#default": null. For example, in secrets-manager.json:

  • DeleteSecretRequest$RecoveryWindowInDays (target long), @default: null at line 614.
  • DeleteSecretRequest$ForceDeleteWithoutRecovery (target boolean), @default: null at line 621.

StructureGenerator.writeProperties only added the | None annotation for members with no @default trait, so these members resolved their default to None but kept the bare target annotation:

recovery_window_in_days: int = None
force_delete_without_recovery: bool = None

None is not assignable to int / bool, so pyright (run by PythonFormatter during codegen) fails the build:

src/aws_sdk_secretsmanager/models.py - error: Type "None" is not assignable to declared type "int" (reportAssignmentType)
src/aws_sdk_secretsmanager/models.py - error: Type "None" is not assignable to declared type "bool" (reportAssignmentType)

The fix

In the optional-members loop of StructureGenerator.writeProperties, mark the field nullable when the resolved default is a null node, so it is annotated | None:

if (!target.isDocumentShape()
        && member.expectTrait(DefaultTrait.class).toNode().isNullNode()) {
    writer.putContext("nullable", true);
}

This produces:

recovery_window_in_days: int | None = None
force_delete_without_recovery: bool | None = None

Document shapes are excluded. A document with a null default is a non-None Document holding a null value, generated via default_factory as field(default_factory=lambda: Document(None)). Annotating it | None would be wrong.

Testing

  • Regenerated Secrets Manager and Cognito Identity: both build cleanly, and pyright reports 0 errors on the generated source.
  • Regenerated DynamoDB, SQS and all existing clients in aws-sdk-python repo (no @default: null members): generated source is unchanged.
  • :core:build (unit tests and the weather :core:integ integration test, whose model includes a Document = null member) passes.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@Alan4506 Alan4506 requested a review from a team as a code owner June 5, 2026 03:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant