Skip to content

fix(export): write nullable items for a list of a union with None - #690

Merged
digalaviz merged 3 commits into
awslabs:devfrom
digalaviz:fix/nullable-union-list-items
Dec 3, 2026
Merged

digalaviz merged 3 commits into
awslabs:devfrom
digalaviz:fix/nullable-union-list-items

Conversation

@digalaviz

@digalaviz digalaviz commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

tl;dr for humans

to_json_schema() wrote List[Union[str, Literal["b"], None]] as string items without null, so the rebuilt model rejected the None items the original accepts. Before #666 that shape at least warned; #666 judges a union by its arms and skips the None arm, so the warning went away and nothing signalled the loss. The list items now carry null, the same way List[Optional[str]] already did. Nothing else exports differently.


for agents

  • intent: close a regression from fix(export): judge a union by its arms in the unmapped-type warning (#662) #666 found while checking the 1.1.0 merges: the warning rule skips a None arm, so the export itself must keep the element nullable.
  • behavior: in to_json_schema(), a list whose element is a union of several arms plus None writes items: {"type": [<type>, "null"]}. Warnings are unchanged: a union that string items cannot hold, such as Union[str, int, None], still warns. A model union (Union[Card, Bank, None]) and to_stickler_config() are untouched.
  • code: StructuredModel.to_json_schema() list branch in src/stickler/structured_object_evaluator/models/structured_model.py: sets element_is_nullable when the unwrapped element is a union with a NoneType arm. _unwrap_optional reads only Optional[T].
  • commits: fix(export): write nullable items for a list of a union with None: patch, corrects an export that the rebuilt model could not load.
  • verification:
    • test_a_list_element_with_a_none_arm_keeps_its_none_items in tests/structured_object_evaluator/test_export_roundtrip_shapes.py: 4 cases; 3 fail without the fix, Optional[str] is the control.
    • A differential over 30 list element types on dev: only the 5 multi-arm unions with None change, each gaining null in its items, with the same warnings as before.
    • uv run --frozen pytest -q: 7122 passed, 13 skipped, 1 xfailed. ruff check passes.
  • not covered: to_stickler_config() writes List[Optional[str]] without nullability, so a rebuilt model rejects None items there too. That was already so before fix(export): judge a union by its arms in the unmapped-type warning (#662) #666 and before this change.

Fixes #697.

- to_json_schema() read only Optional[T] as a nullable list element, so
  List[Union[str, Literal["b"], None]] wrote string items without null
  and the rebuilt model rejected its None items
- before awslabs#666 that shape warned; the union-by-arms rule skips the None
  arm, so the export now has to carry it
- only multi-arm unions with a None arm change; a model union and
  to_stickler_config() are untouched
@digalaviz
digalaviz requested a review from sromoam as a code owner December 1, 2026 04:03
@digalaviz digalaviz added this to the 1.1.0 milestone Dec 1, 2026
@digalaviz
digalaviz requested a review from vawsgit December 1, 2026 04:03
@github-actions

github-actions Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Security Scan Results

ASH Security Scan Report

  • Report generated: 2026-12-03T15:44:17+00:00
  • Time since scan: 3 minutes

Scan Metadata

  • Project: ASH
  • Scan executed: 2026-12-03T15:40:25+00:00
  • ASH version: 3.1.2

Summary

Scanner Results

The table below shows findings by scanner, with status based on severity thresholds and dependencies:

  • Severity levels:
    • Suppressed (S): Findings that have been explicitly suppressed and don't affect scanner status
    • Critical (C): Highest severity findings that require immediate attention
    • High (H): Serious findings that should be addressed soon
    • Medium (M): Moderate risk findings
    • Low (L): Lower risk findings
    • Info (I): Informational findings with minimal risk
  • Duration (Time): Time taken by the scanner to complete its execution
  • Actionable: Number of findings at or above the threshold severity level that require attention
  • Result:
    • PASSED = No findings at or above threshold
    • FAILED = Findings at or above threshold
    • MISSING = Required dependencies not available
    • SKIPPED = Scanner explicitly disabled
    • ERROR = Scanner execution error
  • Threshold: The minimum severity level that will cause a scanner to fail
    • Thresholds: ALL, LOW, MEDIUM, HIGH, CRITICAL
    • Source: Values in parentheses indicate where the threshold is set:
      • global (global_settings section in the ASH_CONFIG used)
      • config (scanner config section in the ASH_CONFIG used)
      • scanner (default configuration in the plugin, if explicitly set)
  • Statistics calculation:
    • All statistics are calculated from the final aggregated SARIF report
    • Suppressed findings are counted separately and do not contribute to actionable findings
    • Scanner status is determined by comparing actionable findings to the threshold
Scanner Suppressed Critical High Medium Low Info Actionable Result Threshold
bandit 0 0 0 0 8083 0 0 PASSED MEDIUM (global)
cdk-nag 0 0 0 0 0 0 0 PASSED MEDIUM (global)
cfn-nag 0 0 0 0 0 0 0 MISSING MEDIUM (global)
checkov 0 0 0 0 0 0 0 PASSED MEDIUM (global)
detect-secrets 0 0 0 0 0 0 0 PASSED MEDIUM (global)
grype 0 0 0 0 0 0 0 MISSING MEDIUM (global)
npm-audit 0 0 0 0 0 0 0 PASSED MEDIUM (global)
opengrep 0 0 0 0 0 0 0 MISSING MEDIUM (global)
semgrep 7 0 0 0 0 0 0 PASSED MEDIUM (global)
syft 0 0 0 0 0 0 0 MISSING MEDIUM (global)

Report generated by Automated Security Helper (ASH) at 2026-12-03T15:44:17+00:00

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran this against dev (33ed6c3). Everything below is from running the branch, not reading it. Not an approval — that's for a maintainer.

Confirmed

  • Full suite on the branch: 7122 passed, 13 skipped, 1 xfailed — matches the description. ruff check passes.
  • The new test has teeth: test_a_list_element_with_a_none_arm_keeps_its_none_items dropped onto dev gives 3 failed, 1 passed — the three multi-arm unions fail and Optional[str] passes, exactly as described.
  • The 7-line fix reads correctly: it only promotes element_is_nullable when the unwrapped element is a union with a NoneType arm, so _unwrap_optional's existing Optional[T] behaviour is untouched.

The user-visible failure, end to end

Validating data that the original model accepts against the exported schema:

dev:    items.type = string            -> ["a", "b", None]  REJECTED: None is not of type 'string'
branch: items.type = ['string', 'null'] -> ["a", "b", None]  ACCEPTED

The differential, independently

I ran a 17-element-type differential on to_json_schema()["properties"]["items"] between dev and the branch. Exactly 3 of 17 changed, each gaining "null" in items and nothing else:

  • Union[str, int, None]
  • Union[str, Literal["b"], None]
  • Annotated[Union[str, int, None], "x"]

The 14 unchanged include the controls that matter: Union[str, int] and Union[int, float] (multi-arm, no None) are byte-identical, Union[str, None] is unchanged (already correct), and Optional[List[str]] keeps its nested ["array", "null"]. The UserWarning for Union[str, int] and Union[str, int, None] fires identically on both, so "warnings are unchanged" holds.

On the to_stickler_config() gap in "not covered"

Confirmed it's pre-existing and not touched here — the diff is confined to to_json_schema(). Since #666 removed the warning that used to catch this, that path is now silent too; worth its own issue so it doesn't get lost, but it's correctly out of scope for this PR.

@sromoam sromoam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Diego. The list fix works: on this head, List[Union[str, Literal["b"], None]] exports ["string", "null"] items and the rebuilt model accepts ["a", None]. One more place has the same bug.

The scalar branch drops the None arm too. A required f: Union[str, Literal["b"], None] = ComparableField(default=...) exports {"type": "string"}, so from_json_schema(schema)(f=None) raises ValidationError. It's the same regression as #697: before #666 this shape warned, and now it exports silently. field_is_nullable comes from _unwrap_optional, which reads only Optional[T].

Could you add one shared helper, something like annotation_is_nullable() (a union with NoneType among its arms), and use it on the scalar branch and the list branch of to_json_schema()? Please also check whether to_stickler_config() needs it. That fixes the class of bug, and it replaces the two overlapping checks this PR adds (_unwrap_optional and then is_union(...) and NoneType in args, with unwrap_annotated(args[0]) computed twice). A test for the required scalar case would pin it.

List[Union[Card, Bank, None]] doesn't need anything here: since #686 merged, it raises on export.

- a required Union[str, Literal["b"], None] field exported as "string",
  so the rebuilt model rejected None; the scalar branch had the same gap
  as the list element
- annotation_is_nullable() in the shared helpers (a union with a None
  arm, seen through Annotated) replaces the two overlapping checks
- to_stickler_config() is unchanged: its list branch already writes
  Optional, and other unions still warn as on dev
@digalaviz

Copy link
Copy Markdown
Contributor Author

Thanks @sromoam. Fixed in ec4e9a89.

  • annotation_is_nullable() is in stickler/utils/optional_annotation.py with its siblings: a union with a None arm, seen through Annotated. Both branches of to_json_schema() use it, and it replaces the two overlapping checks (and the duplicate unwrap_annotated(args[0])).
  • A required f: Union[str, Literal["b"], None] = ComparableField(default="a") now exports ["string", "null"]; the rebuilt model accepts None and still rejects a dict. Pinned by test_a_required_field_with_a_none_arm_keeps_null (4 cases; 3 fail on the previous head, Optional[str] is the control).
  • Differential over 80 fields (40 annotations, required and default=None): only scalar unions with a None arm change, each from "string" to ["string", "null"]. Warning counts are identical. Two existing expectations (Optional[Union[str, Level]], Optional[Union[Letter, Colour]]) moved to ["string", "null"] for the same reason; the CHANGELOG line now covers unions that warn too.
  • to_stickler_config() does not need it. Its list branch already writes Optional for a builtin scalar union (_scalar_union_config_type now calls the helper, same output), and other unions write List[Union] or str with a warning, as on dev. Its output is identical on the previous head and this one for all 80 fields.

One older gap I found while checking, not from this PR and not fixed here: a required Optional[str] or Optional[int] with no default exports as "str"/"int" in to_stickler_config(), so the rebuilt model rejects None. Same on dev. I will open an issue for it.

Full suite: 7129 passed, 13 skipped, 1 xfailed.

…rites nullable

From review: the entry said any field or list element whose union has a None
arm is written nullable, which isn't true for nested lists, type aliases,
NewType, an optional nested model or to_stickler_config(). Those keep 1.0.x
behaviour and are tracked in awslabs#727.

@sromoam sromoam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Diego, approving. The shared annotation_is_nullable fixes both branches. On this head, a required Union[str, Literal["b"], None] exports ["string", "null"] and its list exports nullable items, and both rebuilt models accept None.

I pushed one commit, 12849a03, so this can merge for 1.1.0. It narrows the CHANGELOG line, which said any field or list element whose union has a None arm is now nullable. That isn't true yet for a nested list of one, a type alias or NewType of an optional type, a required Optional nested model, or to_stickler_config(). All of those lose None exactly as 1.0.2 did, so none is a regression. They're in #727 for 1.1.1, with the measurements and a single-predicate fix that could also absorb TypeResolver.is_optional_type.

One small ask if you're in here again: the PR description still says only the list branch changed and "nothing else exports differently". The plain-field export changes too, and that's the right call.

@digalaviz
digalaviz merged commit 1fddd7e into awslabs:dev Dec 3, 2026
11 checks passed
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.

[BUG]: to_json_schema() no longer warns for List[Union[str, Literal, None]], and the rebuilt model rejects None items

3 participants