fix(codegen/python): disambiguate namespace/root leaf-name collisions (#161)#162
Merged
Conversation
…#161) A root type and a same-leaf type under a sub-namespace each emitted a class plus a bare `<Leaf> = <NsLeaf>` alias that shadowed the `from .._types import <Leaf>` the module also imported, so `_types.<Leaf>` and `<ns>.<Leaf>` resolved to two distinct classes and field annotations bound to the wrong one. Resolve each logical type to a single class: keep the short name bound to the imported root type and reference the namespace-local type by its disambiguated flat name. No-op for schemas without such collisions.
|
📖 Documentation Preview: https://reflectapi-docs-preview-pr-162.partly.workers.dev Updated automatically from commit b261956 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 98d0de222a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
A formatter (ruff/prettier/rustfmt) that rejects its input can exit before draining stdin, making `write_all` fail with `BrokenPipe` and masking the real exit status. This made `python_format_reports_ruff_failures` flaky under CI load. Swallow only `BrokenPipe` on the stdin write so `wait_with_output` reports the actual failure.
module_aliases() emits both a prefix-stripped alias and a Rust-leaf alias, and either can shadow a same-named imported root type. The collision registry only inspected the stripped form, so a namespaced `OrderInsertData` whose flat name de-stutters to `MyapiOrderInsertData` slipped through: its stripped alias is the harmless `InsertData`, but the Rust-leaf alias `OrderInsertData = MyapiOrderInsertData` still shadowed the imported root. Build the registry from the module's actual aliases so both forms are covered, and add a regression test. Reported by Codex review on #162.
Merged
hardbyte
added a commit
that referenced
this pull request
Jun 2, 2026
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.
Fixes #161.
Problem
In the multi-file (package) Python client, a root type and a same-leaf type under a sub-namespace (e.g. a root
IfConflictOnUpdateand anomatches::IfConflictOnUpdate) collide on a bare public name. Each namespace module emitted both:So
_types.IfConflictOnUpdate is not nomatches.IfConflictOnUpdate— two distinct classes sharing a name — and a field annotated against one rejects an instance of the other at runtime with a confusingly self-identical pydanticValidationError. Reproduces identically on0.17.4and currentmain(the recent namespace-import work did not touch the duplicate-definition logic).Fix
Collision-aware naming in
reflectapi/src/codegen/python.rs. A leaf "collides" when a sub-namespace defines a type whose short (prefix-stripped) name equals a root type imported into every module. For collisions only, the generator now:IfConflictOnUpdate) bound to the imported root type — suppresses the colliding bare alias;NomatchesIfConflictOnUpdate), and rewrites references to it — in its own field annotations and in cross-namespace references from other modules — to that name.Result: one Python class per logical type, so
model.<X>resolves consistently. The collision key is the io-qualified type name, so genuinely-distinctinput::/output::projections are never collapsed. It is a strict no-op for schemas without such collisions — the committed demo client is byte-identical.Tests
test_python_namespace_leaf_collision_no_shadowing(multi-file package path): asserts no colliding bare rebind, the namespace field uses the disambiguated name, and a root cross-namespace ref is qualified with the disambiguated name. Fails before the fix, passes after.reflectapi-runtime+ pydantic 2.13:_types.IfConflictOnUpdate is nomatches.IfConflictOnUpdateis nowTrue, the namespace-local type stays distinct, and a request/response round-trip validates without theValidationError.cargo fmt --all,clippy -D warnings,build --workspace, demo + CLI suites pass (Python snapshots requireruffinstalled locally).Docs
docs/src/clients/README.md.