Skip to content

fix(ctl): handle extensions paths in display_schema_load_errors (#1007)#1008

Open
iddocohen wants to merge 9 commits intostablefrom
ic-fix-display-schema-load-error
Open

fix(ctl): handle extensions paths in display_schema_load_errors (#1007)#1008
iddocohen wants to merge 9 commits intostablefrom
ic-fix-display-schema-load-error

Conversation

@iddocohen
Copy link
Copy Markdown

Why

infrahubctl schema load crashes with an unhandled ValueError when the
server rejects a schema that uses the extensions: block. The traceback replaces the
human-readable rejection message, so users have no idea what the server
actually rejected.

Goal: Render extensions-block rejections in the same one-line format as
top-level rejections.

Non-goals: No new error categories, no changes to infrahubctl schema check or to the server's rejection format, no broader refactor of the CLI
schema commands.

Closes #1007

What changed

Behavioral changes

  • Schema rejections originating inside an extensions: block now render as
    Node: <kind> (extensions/<container>) | <field> (<input>) | <msg> (<type>)
    instead of crashing with ValueError: invalid literal for int() with base 10.
  • Unknown / malformed loc_path shapes (non-int schema index, unrecognised
    container) are skipped silently instead of crashing the renderer.
  • Top-level (non-extension) error rendering is byte-for-byte unchanged —
    preserved by the existing tests.

Implementation notes

  • display_schema_load_errors now branches on loc_path[3] == "extensions",
    computes container / node_index / tail once, and dispatches on len(tail)
    instead of len(loc_path) so the offset shift doesn't have to be repeated.
  • Per-error rendering extracted into _render_schema_error to keep the main
    loop within the project's branch-count limits.
  • valid_error_path now whitelists loc_path[3] in {nodes, generics, extensions} and requires loc_path[2] (and the node-index slot) to be int.
  • get_node gained container and is_extension kwargs (defaults match the
    old behaviour) and uses payload.get(...) so a schema file with only
    extensions: no longer raises KeyError.

What stayed the same

  • API contract of display_schema_load_errors unchanged.
  • get_node signature is backward-compatible (new params have defaults).
  • Output format for non-extension errors is unchanged.

How to review

  • infrahub_sdk/ctl/schema.py — start with _render_schema_error, then
    valid_error_path, then get_node.
  • tests/unit/sdk/test_schema.py — three new tests cover top-level extensions
    errors, nested-attribute extensions errors, and the malformed-path filter.
  • The pre-existing tests for non-extension paths are intentionally untouched
    and still pass — that's the regression guard for the format-stability claim
    above.

The valid_error_path whitelist is the area I'd most want a second opinion
on: if the server later adds a new top-level container, errors against it
will now be silently skipped instead of bubbling up. The trade-off is
intentional for a CLI rendering helper, but worth flagging.

How to test

# Repro from the issue (requires a running Infrahub 1.5+ with DcimGenericDevice
# already loaded, e.g. opsmill/schema-library base/dcim.yml):
cat > /tmp/repro_extension.yml <<'YAML'
---
version: "1.0"
extensions:
  generics:
    - kind: DcimGenericDevice
      include_in_menu: false
YAML
infrahubctl schema load /tmp/repro_extension.yml
# Before: ValueError traceback. After: one-line "Node: DcimGenericDevice (extensions/generics) | ..." rendering.

# Unit tests:
uv run pytest tests/unit/sdk/test_schema.py -k display_schema_load_errors -v

# Lint:
uv run invoke format lint-code

Impact & rollout

  • Backward compatibility: No breaking changes. CLI output for non-extension
    errors is unchanged. get_node gained two optional kwargs with defaults.
  • Performance: N/A — only touched on the error path of schema load.
  • Config/env changes: None.
  • Deployment notes: Safe to deploy. Pure SDK/CLI fix, no server-side change.

Checklist

  • Tests added/updated
  • Changelog entry added (uv run towncrier create +fix-schema-load-extensions.fixed.md)
  • External docs updated (if user-facing or ops-facing change)
  • Internal .md docs updated (internal knowledge and AI code tools knowledge)

@codecov
Copy link
Copy Markdown

codecov Bot commented May 10, 2026

Codecov Report

❌ Patch coverage is 76.08696% with 11 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
infrahub_sdk/ctl/schema.py 76.08% 5 Missing and 6 partials ⚠️
@@            Coverage Diff             @@
##           stable    #1008      +/-   ##
==========================================
+ Coverage   81.41%   81.42%   +0.01%     
==========================================
  Files         134      134              
  Lines       11347    11365      +18     
  Branches     1703     1707       +4     
==========================================
+ Hits         9238     9254      +16     
- Misses       1566     1568       +2     
  Partials      543      543              
Flag Coverage Δ
integration-tests 41.80% <6.52%> (-0.05%) ⬇️
python-3.10 54.42% <76.08%> (+0.07%) ⬆️
python-3.11 54.42% <76.08%> (+0.05%) ⬆️
python-3.12 54.40% <76.08%> (+0.03%) ⬆️
python-3.13 54.42% <76.08%> (+0.05%) ⬆️
python-3.14 54.41% <76.08%> (+0.05%) ⬆️
python-filler-3.12 22.71% <0.00%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
infrahub_sdk/ctl/schema.py 63.87% <76.08%> (+2.15%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@iddocohen iddocohen marked this pull request as ready for review May 10, 2026 05:52
@iddocohen iddocohen requested a review from a team as a code owner May 10, 2026 05:52
@ogenstad
Copy link
Copy Markdown
Contributor

Not a review but just highlighting that we also want a towncrier newsfragment under /changelog so that we include what was fixed in the release notes.

Also in general when we're fixing bugs it's fine to target the stable release directly. That part is more important in the Infrahub repo than it is in the SDK though as in the SDK we can merge develop directly into stable for a patch release but that would never happen within the main Infrahub project.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 10, 2026

Deploying infrahub-sdk-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: 90c534d
Status: ✅  Deploy successful!
Preview URL: https://f1423771.infrahub-sdk-python.pages.dev
Branch Preview URL: https://ic-fix-display-schema-load-e.infrahub-sdk-python.pages.dev

View logs

@iddocohen iddocohen changed the base branch from develop to stable May 10, 2026 09:11
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.

bug: infrahubctl schema load - display_schema_load_errors crashes with ValueError when error path traverses an extensions: block

3 participants