Repository navigation
fix(python): an omittable unit field is annotated None once - #220
Merged
Merged
Conversation
|
📖 Documentation Preview: https://reflectapi-docs-preview-pr-220.partly.workers.dev Updated automatically from commit 9689a2f |
hardbyte
approved these changes
Nov 4, 2026
antonzherdev
marked this pull request as ready for review
November 4, 2026 20:58
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.
Tip: disable this comment in your organization's Code Review settings.
An omittable field whose type renders as `None`, such as `()` or a unit struct, got `| None` appended for its omission default and was annotated `None | None`. Python can't evaluate that, so importing the generated module raised TypeError. The suffix is now skipped when the annotation already admits None. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q7zEtTUpmxtYWDkuq6uwpY
Pydantic 2.12.0-2.12.2 on Python 3.14 refuses `x: None = None` under postponed annotations, reading the evaluated annotation as a clash with the default. The minimum-pydantic CI leg hit it on the omittable unit fields this branch now renders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q7zEtTUpmxtYWDkuq6uwpY
Covers a unit struct, `()` and a newtype of `Option`. Writing it showed the pydantic workaround also wrapped required `None` fields in an empty `Field()`, so it now applies only to fields with a default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Q7zEtTUpmxtYWDkuq6uwpY
antonzherdev
force-pushed
the
fix/python-omittable-unit-fields
branch
from
November 5, 2026 09:08
5b2308c to
90f5e35
Compare
hardbyte
pushed a commit
that referenced
this pull request
Nov 6, 2026
#219 and #220 added fixtures with newtype and unit structs, whose OpenAPI components were one-element arrays; they are now the wrapped value, like every other newtype here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VjkNsfHSd5osRevK95McpQ
hardbyte
added a commit
that referenced
this pull request
Nov 8, 2026
* fix(codegen): read newtype structs as the value they wrap Serde writes a newtype struct as the value it wraps, flattened or not. `Shape::Newtype` now says so in the two places that didn't: - OpenAPI described `struct UserId(Uuid)` as a one-element array (`prefixItems`), so a client generated from the spec sent `["..."]`. It is now the inner schema, as for a transparent struct. Unit structs, a newtype of `()` in the schema, are `null` instead of `[null]`. - `Layout` treated a flattened newtype as having unknown keys, so Python codegen failed and OpenAPI described the group with that array. It now contributes the keys of the type it wraps. The codegen coverage fixture flattens a required and an optional newtype, so CI replays serde's JSON through the generated Python models and checks their JSON schemas against it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VjkNsfHSd5osRevK95McpQ * fix(python): render a flattened wrapper of an enum as the enum Python picks how to render a flattened field from the type the field names. Behind a newtype or transparent struct, an enum was missed, so the model had a nested field (`event: Event`) where serde writes the tag and fields on the parent. Before #217 a newtype failed codegen instead; a transparent wrapper had this bug unless the schema was built with `fold_transparent_types()`. The struct is now rendered with each such field naming the wrapped type, which serde writes identically. * test(openapi): newtype structs in snapshots added since this branch #219 and #220 added fixtures with newtype and unit structs, whose OpenAPI components were one-element arrays; they are now the wrapped value, like every other newtype here. * refactor(codegen): one rule for which structs serde writes as their field `Layout`, OpenAPI's struct schema and Python's flattened-wrapper rewrite each decided for themselves that a newtype, or a transparent struct with one field, is written as the field it wraps. `wrapped_field` in `codegen/schema/shape.rs` now decides it once. Generated output is unchanged.
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.
What changes
Only the generated Python client. A module with an omittable field of a unit type now imports.
marker: None | None = Nonemarker: None = Field(default=None)TypeError:None | Nonedoesn't evaluateNone, so the omission default no longer appends| None.Field(default=None), not= None. Pydantic 2.12.0 to 2.12.2 on Python 3.14 rejectsx: None = Noneunder postponed annotations.Optionreadsstr | Noneinstead ofstr | None | None. Same meaning.Wire behaviour is unchanged:
OmittableUnits()sends{}, and an explicitNonesendsnull.Testing
OmittableUnits: CI's import step for the coverage client fails on main with theTypeErrorand passes here, on the minimum and latest pydantic.omittable_field_of_a_unit_type_is_none_once.struct_with_serde_default_none_types: in the Python output (-5.snap), the unit struct and()fields areNone = Field(default=None)and theOptionnewtype isstr | None = None.🤖 Generated with Claude Code
https://claude.ai/code/session_01Q7zEtTUpmxtYWDkuq6uwpY