Skip to content

fix!: Derive field nullability only from the field-level | None, for every dtype source - #152

Merged
FBruzzesi merged 9 commits into
mainfrom
fix/field-level-nullability
Nov 22, 2026
Merged

FBruzzesi merged 9 commits into
mainfrom
fix/field-level-nullability

Conversation

@FBruzzesi

Copy link
Copy Markdown
Owner

Summary

A field's nullability now comes only from its field-level type: the Annotated layers, optionally wrapped in one | None, with forward references resolved. A field-level Literal[..., None] counts as | None. The rule is the same for every dtype source: an explicit nullable wins (an explicit None counts as unset), otherwise a field-level | None makes the field nullable.

UnionTypeStep and the Literal branch of PyTypeStep no longer write nullable into the metadata. Because they also parse nested types, those writes marked a whole field nullable when only its elements were. A dtype override skipped parsing, so the write never happened and the field's | None was lost. ParserPipeline.parse_into_field now works out nullability itself, after parsing.

Closes #141
Closes #142
Supersedes #144, which did the same work on top of #143. This PR applies it to main without the WithAnySchema marker changes.

Breaking changes

  • list[T | None], tuple[T | None, ...] and list[Literal[..., None]] are no longer nullable: only their elements are.
  • T | None and Literal[..., None] with a dtype override are now nullable (they used to be non-nullable).
  • No built-in step writes nullable any more. A custom step that unwraps a type into T | None no longer makes the field nullable unless it sets {"anyschema": {"nullable": True}} itself. A value a step writes counts as explicit: it beats the field-level | None and applies to the whole field.
  • AnySchemaMetadata.nullable is now typed bool | None, where None means unset.

Known limit

With a dtype override, the type is never parsed. A field-level forward reference that cannot be resolved (an unknown name, a malformed string, or a pipeline without ForwardRefStep) therefore hides its None, and the field comes out non-nullable. Set nullable explicitly in that case.

@FBruzzesi FBruzzesi added the fix label Nov 22, 2026
@FBruzzesi
FBruzzesi merged commit 4228650 into main Nov 22, 2026
13 checks passed
@FBruzzesi
FBruzzesi deleted the fix/field-level-nullability branch November 22, 2026 10:36
FBruzzesi added a commit that referenced this pull request Nov 22, 2026
…y `dtype` source

After the merge of #152, every `dtype` source takes nullability from the field-level type, so `split_field_markers` no
longer reports `is_optional` and `merge_field_markers` no longer sets `nullable`. A marker `dtype` on
`Literal[..., None]` is now nullable too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

1 participant