Skip to content

fix: recognize every spelling of a nullable array element - #1976

Open
shcheklein wants to merge 2 commits into
fix/none-in-arraysfrom
fix/nullable-array-item-spellings
Open

fix: recognize every spelling of a nullable array element#1976
shcheklein wants to merge 2 commits into
fix/none-in-arraysfrom
fix/nullable-array-item-spellings

Conversation

@shcheklein

@shcheklein shcheklein commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Stacked on #1975.

An array's element type was called nullable by testing the annotation against
(int, float, str, bool, bytes, datetime) — which only the plainest spelling
passes. Every one of these resolved to a non-nullable item and then refused
a None that Pydantic had happily accepted into the field:

list[Annotated[int, "meta"] | None]
list[Annotated[int | None, "meta"]]
list[Literal["a", "b"] | None]
list[Annotated[Literal["a", None], "meta"]]
list[str | Literal[None]]
list[Literal[None]]
list[SubclassedInt | None]          # class SubclassedInt(Int64)
tuple[str, Literal[None]]
tuple[int, int | None]

All ten already resolve to the right item type on mainInt64, String,
SubclassedInt — they simply were not marked nullable.

Ask Pydantic instead of taking the annotation apart

TypeAdapter(annotation).validate_python(None)

Pydantic validated the field in the first place, so this cannot disagree with
what a user can actually store, and every nesting is answered without a line of
introspection here: Annotated and Literal either way round, a None among a
Literal's values, a union arm, Literal[None] inside a union.

An earlier revision of this hand-rolled the peeling. It took four review rounds
to reach the cases above, and it was 35 lines against these 8.

Two things worth knowing about the call:

  • It builds a schema (~34 µs), so it belongs where a column's type is
    resolved
    — once per column — and not anywhere a value passes through. There
    is a comment saying so.
  • An SQLType subclass is not a type Pydantic can model at all
    (PydanticSchemaGenerationError), so that case falls back to looking for a
    None among the union arms.

Two smaller corrections

Only args[0] was asked, though a fixed tuple keeps every slot in the one
column — so tuple[int, int | None] missed its own nullable slot.

Nullability is now read from the resolved column type rather than the
annotation, by subclass, so a user's Int64 subclass is recognized as the scalar
it is.

Not covered

list[Annotated[str, "meta"] | Literal[None]] still raises
Cannot recognize type, as on main. That is item-type resolution rather than
nullability — the composition is recognized by neither the scalar table nor the
union handling. Recorded in
#1968.

Stack

  1. fix: keep None in an array wherever it sits #1975None anywhere in an array
  2. this PR — the spellings of a nullable element type
  3. fix: store a list of models that admits a None #1977 — a list of models that admits a None

An element type was called nullable by testing the annotation against
(int, float, str, ...), which only the plainest spelling passes. So these all
resolved to a non-nullable item and refused a None that Pydantic had accepted:

    list[Annotated[int, "meta"] | None]
    list[Annotated[int | None, "meta"]]
    list[Literal["a", "b"] | None]
    list[Annotated[Literal["a", None], "meta"]]
    list[str | Literal[None]]
    list[Literal[None]]
    list[SubclassedInt | None]
    tuple[str, Literal[None]]
    tuple[int, int | None]

Ask Pydantic instead of taking the annotation apart here. It validated the field
in the first place, so every nesting it accepts is answered by construction, and
a TypeAdapter is built where a column's type is resolved rather than anywhere a
value passes through. An SQLType subclass is not a type it can model at all, so
that one falls back to looking for a None among the union arms.

Only the first slot used to be asked, though a fixed tuple keeps all of its
slots in the one column, and nullability is now read from the resolved column
type rather than the annotation, so a subclassed scalar is recognized as the
scalar it is.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Sep 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Asking Pydantic to validate a None runs whatever validators the field declares.
A BeforeValidator that rejects None then failed the schema for a column of
ordinary ints:

    list[Annotated[int, BeforeValidator(rejects_none)]]
    -> TypeError: this field does not take None, while resolving the type

and a coercing validator answered for its own behaviour instead of the declared
type. Catching more exceptions would not have helped; running user code to
resolve a column type is the problem.

Read the annotation instead: a None among a Literal's values, inside Annotated,
or as a union arm, at any nesting. Eleven lines, nothing executed, and it also
covers the two spellings the Pydantic route could not reach at all -- an SQLType
subclass has no Pydantic schema, so list[Annotated[S | None, "meta"]] was
answered no.

A declared Optional whose validator maps None to something else still counts as
nullable. That is now a statement about the annotation rather than a guess from
behaviour.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 2, 2026

Copy link
Copy Markdown

Deploying datachain with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0bfcc59
Status: ✅  Deploy successful!
Preview URL: https://81aed3cb.datachain-2g6.pages.dev
Branch Preview URL: https://fix-nullable-array-item-spel.datachain-2g6.pages.dev

View logs

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