fix: recognize every spelling of a nullable array element - #1976
Open
shcheklein wants to merge 2 commits into
Open
fix: recognize every spelling of a nullable array element#1976shcheklein wants to merge 2 commits into
shcheklein wants to merge 2 commits into
Conversation
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>
This was referenced Sep 2, 2026
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>
Deploying datachain with
|
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 spellingpasses. Every one of these resolved to a non-nullable item and then refused
a
Nonethat Pydantic had happily accepted into the field:All ten already resolve to the right item type on
main—Int64,String,SubclassedInt— they simply were not marked nullable.Ask Pydantic instead of taking the annotation apart
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:
AnnotatedandLiteraleither way round, aNoneamong aLiteral'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:
resolved — once per column — and not anywhere a value passes through. There
is a comment saying so.
SQLTypesubclass is not a type Pydantic can model at all(
PydanticSchemaGenerationError), so that case falls back to looking for aNoneamong the union arms.Two smaller corrections
Only
args[0]was asked, though a fixed tuple keeps every slot in the onecolumn — 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
Int64subclass is recognized as the scalarit is.
Not covered
list[Annotated[str, "meta"] | Literal[None]]still raisesCannot recognize type, as onmain. That is item-type resolution rather thannullability — the composition is recognized by neither the scalar table nor the
union handling. Recorded in
#1968.
Stack
Noneanywhere in an arrayNone