Repository navigation
feat(types)!: re-emit a received null, and read a None keyword argument as unset - #1681
Conversation
|
|
||
| def __setattr__(self, name: str, value: Any) -> None: | ||
| super().__setattr__(name, value) | ||
| if value is None and name in type(self).model_fields: |
There was a problem hiding this comment.
Medium: __setattr__ discards any field set to None, but __init__ (L729-732, via _is_required_key) deliberately keeps a None argument for a required nullable field — "that is a value, and null on the wire where the schema admits it." The two paths diverge. Required nullable fields are real on load-bearing response models (create_media_buy_response.confirmed_at, reporting_delivery_ready_webhook.data_through). response.confirmed_at = None discards it from model_fields_set; model_dump() (exclude_none) then drops it, and _put_back_explicit_nulls only re-emits fields still in the set — so the dump omits a required property and is schema-invalid. model_copy(update={"confirmed_at": None}) → _unset_none_keys (L749) has the same gap. Gate the discard on not _is_required_key(...) to match __init__.
There was a problem hiding this comment.
Ladon verdict: Approve
Approve — SDK 10 explicit-nulls base-model change (#1615).
What I checked:
- The explicit-null round-trip mechanism is coherent:
null_admitting_fields/_put_back_explicit_nullsre-emit received nulls under the wire name, the base-init marker prevents double-processing during validation,CanonicalBoundaryModel's serializer routes through the factored_finish_wire_dump, andFormatcarries theNoExplicitNullmarker its schema arms declare. - Semver signal is correct for the breaking wire-shape change:
feat(types)!+BREAKING CHANGE:footer + migration row inMIGRATION_v9_to_v10.md. This satisfies the mandatory public-surface semver gate. - Strong test coverage added (explicit-nulls, nested no-explicit-null round-trip, forward-compat format kind, wire round-trip conformance, targeting-overlay compat).
Medium findings (1):
- src/adcp/types/base.py:740 —
__setattr__/model_copyunset required nullable fields set to None, diverging from__init__which keeps them (can produce a schema-invalid dump omitting e.g.create_media_buy_response.confirmed_at).
Decision rationale: No critical/high findings (row 1 N/A). gated_paths is false (row 2 N/A). high_risk is true via src/adcp/server/responses.py (modified), but there is no medium finding on that modified file — the single medium is on base.py — so row 5 does not fire. Rows 3 (deleted), 4 (data-loss/schema/infra category), 6 (sticky), 7 (team gate) do not apply. Only one medium total, so row 8 (≥3) does not fire. Falls through to row 9 → approve.
Worth a quick follow-up on the __setattr__/model_copy divergence, but it is not blocking.
Medium findings
- src/adcp/types/base.py:740 — setattr/model_copy unset required nullable fields set to None, diverging from init
426c2e4 to
b9763d2
Compare
| def __setattr__(self, name: str, value: Any) -> None: | ||
| super().__setattr__(name, value) | ||
| if value is None and name in type(self).model_fields: | ||
| self.__pydantic_fields_set__.discard(name) |
There was a problem hiding this comment.
Medium: __setattr__ discards unconditionally; __init__ (L730) keeps None for required keys via _is_required_key. Same gap in _unset_none_keys (L749, the model_copy path). For a required null-admitting field (generator emits field: T | None with no default, no NoExplicitNull), m.field = None or model_copy(update={"field": None}) drops it from model_fields_set, so _put_back_explicit_nulls never re-emits it and the required field vanishes from the dump — while Model(field=None) keeps it and emits {"field": null}. Constructor and mutators diverge on the same document. test_assigning_none_unsets... and test_model_copy_update_with_none_unsets only cover the optional case.
There was a problem hiding this comment.
Ladon verdict: Approve
Approve — explicit-null round-trip support (#1615) for SDK v10 beta.
What I checked:
- Bulk of diff is generated
NoExplicitNullmarker propagation across ~30 response/asset modules, consistent withpost_generate_fixes.optional_field. No forward-compat regression inasset_union.py(no fallback-arm, discriminator, or vocabulary change). - Core logic in
base.pyis well-tested (newtest_explicit_nulls.py; conformancenull_droppedallowance removed). - Semver signal satisfied: SDK 10 major beta,
MIGRATION_v9_to_v10.mdentry added. review_decision: APPROVED(gated_paths is false regardless).- high_risk true only via
src/adcp/server/responses.py (modified); reviewer found no medium-or-higher concern on that file, so the modification is presumed safe. No(deleted)reason.
Medium findings (1 — non-blocking):
- src/adcp/types/base.py:741 —
__setattr__/model_copy/_unset_none_keysdiscard None unconditionally, dropping required null-admitting fields from the dump, diverging from__init__'s_is_required_keyguard; untested for the required case. This re-surfaces the prior run's medium.
Decision path: No critical/high findings (row 1 n/a). gated_paths false (row 2 n/a). No (deleted) (row 3 n/a). The single medium is category code-correctness, not data-loss/schema/infra (row 4 n/a). high_risk+modified+medium would be row 5 — but the medium is on base.py, not the modified high-risk file responses.py, so row 5 does not fire. Prior decision was approve, not escalate (row 6 n/a). No no-auto-approve team (row 7 n/a). Only 1 medium, fewer than 3 (row 8 n/a). Falls through to row 9 — approve. This is consistent with the prior approve; same single medium carries forward as a known, non-blocking item.
Medium findings
- src/adcp/types/base.py:741 — setattr/model_copy unset required nullable fields set to None, diverging from init
…nt as unset A property whose schema admits null and that arrived as null was dropped by the default dump's exclude_none, so a parsed document lost a value a digest or echo over it includes (#1615). On the Python side None is the SDK's spelling of "not supplied", and the two meanings met in one value. They now meet in model_fields_set. Validation (model_validate, model_validate_json, nested validation) leaves a received null set, with value None; AdCPBaseModel.__init__ leaves an optional None keyword argument out before validation, so a validator that asks model_fields_set whether a field is present gets the answer the wire would, and assigning None or passing None in model_copy(update=...) unsets the field. The dump re-emits null for exactly the set, null-admitting fields that are None, under the wire name, and skips Field(exclude=True). null_admitting_fields(model) names them: an annotation that admits None without the NoExplicitNull marker. CanonicalBoundaryModel's serializer runs the same wire pass. A class with its own __init__ is also constructed by validation, so it keeps pydantic's reading of a None keyword argument and must have nothing to remember; tests/test_explicit_nulls.py guards that. canonical_creative.Format declared its ten optional properties without the never-null marker every generated arm of core/product-format-declaration.json carries; it carries them now. build_creative_response strips None from the dicts it is given, which is its documented contract. The wire round-trip suite no longer accepts the null_dropped class. BREAKING CHANGE: a received null on a null-admitting property is written back as null by model_dump(). Model(x=None), model.x = None and model_copy(update={"x": None}) leave x unset: it is omitted from the dump, from model_dump(exclude_unset=True), and from model_fields_set, so a root required-group rule no longer counts it as supplied. A None inside a dict passed to model_validate is a wire null. canonical_creative.Format refuses a JSON null on its optional properties, as its schema does. Co-Authored-By: Claude Fable 5.1 <[email protected]>
b9763d2 to
e9541e5
Compare
| def __setattr__(self, name: str, value: Any) -> None: | ||
| super().__setattr__(name, value) | ||
| if value is None and name in type(self).model_fields: | ||
| self.__pydantic_fields_set__.discard(name) |
There was a problem hiding this comment.
Medium: __setattr__ (and _unset_none_keys at L749-755, via model_copy) discard any field set to None from model_fields_set, but __init__ at L727-733 keeps None for required keys (_is_required_key). The two paths diverge on a required nullable field. Required(value=None) dumps {"value": None}; m.value = None or m.model_copy(update={"value": None}) drops it from the set, so _put_back_explicit_nulls won't restore it and exclude_none omits it — a dump missing a required field (schema-invalid). rights_constraint: Any in acquire_rights_response is one such required null-admitting field. Gate __setattr__/_unset_none_keys on not _is_required_key(cls, name) to match __init__.
There was a problem hiding this comment.
Ladon verdict: Approve
Approve — SDK 10 explicit-null round-trip support.
This is a subsequent review. The prior decision was approve (head b9763d2) with one medium finding on base.py:741. The developer pushed new commits; the same single medium finding re-surfaced, and no new blocking issues were introduced.
What I checked:
- The new
optional_field/NoExplicitNullemitter in post_generate_fixes.py and its mechanical wrapping of never-null optional fields in generated domain files — this is codegen output, not hand-edits, paired with the explicit-null feature. AdCPBaseModellogic distinguishing a received wirenull(kept set) from a PythonNonekwarg (treated as not supplied) — sound and well-tested by tests/test_explicit_nulls.py.- Changes in base.py, generate_types.py, responses.py, canonical_creative.py — wire-shape fidelity preserved, tests added across the explicit-null / forward-compat / round-trip suites.
- high_risk is true only because src/adcp/server/responses.py was (modified); no medium-or-higher finding lands on that file, so the modification is presumed safe. No deletions in sensitive paths.
- gated_paths is false; review_decision is APPROVED; no no-auto-approve team match.
Medium findings (1):
- src/adcp/types/base.py:741 —
__setattr__/model_copydiscardNone-valued required nullable fields frommodel_fields_set, diverging from__init__'s required-key guard and producing a schema-invalid dump on that mutation path.
Decision table: no critical/high (row 1 n/a), gated_paths false (row 2 n/a), no deletions (row 3 n/a), the single medium is not data-loss/schema/infra category (row 4 n/a), high_risk+(modified) present but requires a medium ON that path — this medium is on types/base.py not the modified high-risk file, and no medium lands on responses.py (row 5 n/a), prior decision was approve not escalate (row 6 n/a), no team gate (row 7 n/a), only 1 medium — fewer than 3 (row 8 n/a). Falls through to row 9 → approve.
Medium findings
- src/adcp/types/base.py:741 — setattr/model_copy unset required nullable fields, diverging from init
Summary
Second of the SDK 10 base-model PRs from
docs/v10-codegen-plan.md§7 (#1615, explicit nulls; memo §3.2 as approved).nullreceived on a property whose schema admits it (model_validate,model_validate_json, nested validation) stays inmodel_fields_setwith valueNone, andmodel_dump()writes it back asnullunder the wire name.Nonekeeps meaning "not supplied" on the Python side:AdCPBaseModel.__init__leaves an optionalNonekeyword argument out before validation (so a root required-group rule sees what the wire would), and assigningNoneormodel_copy(update={"x": None})unsets the field. A required field keeps aNoneargument.Field(exclude=True)fields are never put back.null_admitting_fields(model)names the fields this applies to: an annotation admittingNonewithoutNoExplicitNull. A class with its own__init__is also constructed by validation, so it keeps pydantic's reading and must have no null-admitting field;tests/test_explicit_nulls.pyguards that.canonical_creative.Formatnow carries the never-null marker its schema arms declare on all ten optional properties (it was the one offender).CanonicalBoundaryModel's wrap serializer replaces the base one, so the wire pass is factored into_finish_wire_dumpand both call it.build_creative_responsestripsNonefrom the dicts it is given (its documented contract), since a dictNoneis now a wire null.restore_response_variant_aliases) and the canonical-URL ref mirror (which the asset-union arms resolve through) never carried the never-null marker, so 770 optional fields across 29 modules (every response arm,core/assets/asset_union.py) accepted a JSONnulltheir schema refuses, and under this PR a dictNoneon them became a wirenull(the storyboard runner caught it: the reference seller'sinvoice_recipient: None). The emitter now rendersAnnotated[T | None, NoExplicitNull] = Nonewhere the schema does not admit null, the mirror receives the generator marks node by node, and the tree is regenerated (23 modules;make validate-generatedpasses). Six hand-emitted modules (24 fields:verify_brand_claim(s)_response,registry_feed_response, three single fields) still lack the marker and are noted for a follow-up.tests/conformance/test_wire_round_trip.pyno longer accepts thenull_droppedclass (5 fields in 5 schemas on 3.3.0-beta.0).test_targeting_overlay_compat,test_nested_no_explicit_null_round_trip,test_forward_compat_format_kind,test_wire_types,test_implementation_config) are updated;MIGRATION_v9_to_v10.mdrow added.BREAKING CHANGE: a received null on a null-admitting property is written back as null by model_dump(). Model(x=None), model.x = None and model_copy(update={"x": None}) leave x unset: it is omitted from the dump, from model_dump(exclude_unset=True), and from model_fields_set, so a root required-group rule no longer counts it as supplied. A None inside a dict passed to model_validate is a wire null. canonical_creative.Format refuses a JSON null on its optional properties, as its schema does.
Test plan
make lint✓,make typecheck-all✓,scripts/ci/run_native_tests.py --mode full(running locally; CI is authoritative)tests/test_explicit_nulls.py(16 tests)🤖 Generated with Claude Code