Skip to content

fix(python): an omittable unit field is annotated None once - #220

Merged
antonzherdev merged 3 commits into
mainfrom
fix/python-omittable-unit-fields
Nov 5, 2026
Merged

antonzherdev merged 3 commits into
mainfrom
fix/python-omittable-unit-fields

Conversation

@antonzherdev

@antonzherdev antonzherdev commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

What changes

Only the generated Python client. A module with an omittable field of a unit type now imports.

#[serde(default)]
marker: UnitMarker, // struct UnitMarker;
#[serde(default)]
nothing: (),
Before After
Generated marker: None | None = None marker: None = Field(default=None)
Importing the module TypeError: None | None doesn't evaluate works
  • A unit type already renders as None, so the omission default no longer appends | None.
  • The default is written Field(default=None), not = None. Pydantic 2.12.0 to 2.12.2 on Python 3.14 rejects x: None = None under postponed annotations.
  • Side effect: an omittable newtype of Option reads str | None instead of str | None | None. Same meaning.

Wire behaviour is unchanged: OmittableUnits() sends {}, and an explicit None sends null.

Testing

  • Coverage fixture OmittableUnits: CI's import step for the coverage client fails on main with the TypeError and passes here, on the minimum and latest pydantic.
  • Unit test omittable_field_of_a_unit_type_is_none_once.
  • Snapshot struct_with_serde_default_none_types: in the Python output (-5.snap), the unit struct and () fields are None = Field(default=None) and the Option newtype is str | None = None.
  • CI passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Q7zEtTUpmxtYWDkuq6uwpY

@antonzherdev
antonzherdev requested a review from hardbyte November 4, 2026 01:06
@github-actions

github-actions Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

📖 Documentation Preview: https://reflectapi-docs-preview-pr-220.partly.workers.dev

Updated automatically from commit 9689a2f

@antonzherdev
antonzherdev marked this pull request as ready for review November 4, 2026 20:58

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Anton Zherdev and others added 3 commits November 5, 2026 09:06
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
antonzherdev force-pushed the fix/python-omittable-unit-fields branch from 5b2308c to 90f5e35 Compare November 5, 2026 09:08
@antonzherdev
antonzherdev merged commit 4ac7d1a into main Nov 5, 2026
11 checks passed
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.
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.

2 participants